题解:主线任务 2

· · 题解

题解:P1451 求细胞数量

思路

for循环寻找细胞的一部分,然后从这里开始bfs,把整个细胞消除。

参考代码

#include <bits/stdc++.h>
using namespace std;
char maps[101][101];//地图
int ans=0;//答案
int n,m;//同题意
struct node{//结构体
    int x,y;//同题意
};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//四个方向
void bfs(int x,int y){//有参
    node now;//起点
    now.x=x;
    now.y=y;//获取起点坐标
    queue<node> q;//定义队列
    q.push(now);//把起点扔进队列
    while(!q.empty()){//如果队列不为空
        now=q.front();//当前位置获取头
        q.pop();//弹出头
        node nt;//下一个位置
        for(int i=0;i<4;i++){//四个方向
            nt.x=now.x+dir[i][0];
            nt.y=now.y+dir[i][1];//获取下一个位置的坐标
            if(nt.x>=0&&nt.x<n&&
            nt.y>=0&&nt.y<m&&//没有出界
            maps[nt.x][nt.y]!='0'){//是细胞
                maps[nt.x][nt.y]='0';//标记
                q.push(nt);//从这里开始继续走,将这个位置推入队列
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>maps[i][j];
        }
        getchar();//扔掉换行符
    }
    //输入
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){//找起点
            if(maps[i][j]!='0'){//如果这里是细胞
                ans++;//细胞数量加一
                maps[i][j]='0';//标记起点
                bfs(i,j);//通过bfs标记整个细胞
            }
        }
    }
    cout<<ans<<endl;//输出结果
    return 0; 
}

题解:B3625 迷宫寻路

思路

最基础的 BFS 跑图。

参考代码

#include <bits/stdc++.h>
using namespace std;
int n,m;
char maps[101][101];
string ans;
struct node{
    int x,y;
};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(){
    node now;
    now.x=1;
    now.y=1;
    queue<node> q;
    q.push(now);
    while(!q.empty()){
        now=q.front();
        q.pop();
        if(now.x==n&&now.y==m){//到达目的地
            ans="Yes";
            return;
        }
        node nt;
        for(int i=0;i<4;i++){
            nt.x=now.x+dir[i][0];
            nt.y=now.y+dir[i][1];
            if(nt.x>=1&&nt.x<=n&&
            nt.y>=1&&nt.y<=m&&
            maps[nt.x][nt.y]=='.'){//没有出界且能走
                maps[nt.x][nt.y]='#';
                q.push(nt);
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>maps[i][j];
        }
        getchar();
    }
    maps[1][1]='#';//标记起点被走过
    ans="No";
    bfs();
    cout<<ans<<endl;
    return 0;
}

题解:P1162 填涂颜色

思路

我们很难找到四周都被围住的部分,但是,我们可以轻而易举地找到没被围住的部分,因此,我们只需先把所有地方都刷成 2,再从 (0,0) 处出发,将没有被围住的地方刷成 0,就可达到题目要求。

但是,为了防止从 0,0 出发无法扫到全部闭合圈外的部分,我们应在整张地图的最外侧再加一层 2。

参考代码

#include<bits/stdc++.h>
using namespace std;
int maps[101][101];
struct node{
    int x,y;
};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n;
void bfs(){
    node now;
    now.x=0;
    now.y=0;
    queue<node> q;
    q.push(now);
    while(!q.empty()){
        now=q.front();
        q.pop();
        //没有目的地
        node nt;
        for(int i=0;i<4;i++){
            nt.x=now.x+dir[i][0];
            nt.y=now.y+dir[i][1];
            if(nt.x>=0&&nt.x<=n+1&&
            nt.y>=0&&nt.y<=n+1&&
            maps[nt.x][nt.y]==2){
                maps[nt.x][nt.y]=0;
                q.push(nt);
            }
        }
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>maps[i][j];
        }
    }
    for(int i=0;i<=n+1;i++){
        for(int j=0;j<=n+1;j++){
            if(maps[i][j]==0){
                maps[i][j]=2;
            }
        }
    }//全都刷成 2
    bfs();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<maps[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}