BFS(广度优先搜索)简单例题(二)

本篇博客是关于BFS(广度优先搜索)问题的练习。

填涂颜色    洛谷    P1162

题意:由数字00组成的方阵中,有一任意形状闭合圈,闭合圈由数字11构成,围圈时只走上下左右44个方向。现要求把闭合圈内的所有空间都填写成22.

题解:本题如果搜索圈内的1,非常的麻烦但是如果搜索圈外的1,那句比较简单点了,但是在搜索的时候并不是圈外所有的点都是连通的,所以可以暴力搜索圈外的点,不管圈外的点是不是孤立的,一定能和四个边界相连通的所以,从边的范围遍历。

#include
#include
#include
#include
using namespace std;
#define N 100
struct Node{
       int x, y;
} p, tmp;
queueq;
int vis[N][N];
int dis[N][N];
int dx[4] ={1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n;
void Bfs(int st, int en){
     memset(vis, 0, sizeof(vis));
     p.x = st;
     p.y = en;
     if(dis[st][en] != 1 && dis[st][en] != 3){
     if(dis[st][en] == 0)dis[p.x][p.y] = 3;
     vis[p.x][p.y] = 1;
     q.push(p);
     int cn;
     while(!q.empty()){
        p = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            tmp.x = p.x + dx[i];
            tmp.y = p.y + dy[i];
            if(tmp.x >= 0 && tmp.x < n && tmp.y >= 0 && tmp.y < n && !vis[tmp.x][tmp.y] && !dis[tmp.x][tmp.y]){
               q.push(tmp);
               vis[tmp.x][tmp.y] = 1;
               dis[tmp.x][tmp.y] = 3;
            }
        }

     }
     }
}
int main(){
    while(~scanf("%d", &n)){
        while(!q.empty())q.pop();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                cin>>dis[i][j];
            }
        }
        for(int i = 0; i < n; i++){
            Bfs(i, 0);
            Bfs(0, i);//遍历所用边界点
            Bfs(n-1, i);
            Bfs(i, n-1);
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(dis[i][j] == 3)
                cout<<0<<" ";
                else if(dis[i][j] == 0)
                    cout<<2<<" ";
                else cout<

Battle City     POJ   2312

链接:http://poj.org/problem?id=2312

#include
#include
#include
#include
#include
using namespace std;
#define N 305
struct Node{
       int x, y, step;
       friend bool operator<(Node A, Node B){
            return A.step > B.step;
       }
}tmp1, tmp2;
int vis[N][N];
char a[N][N];
int n, m, k, flag;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1,-1, 0,  0};
void bfs(int st, int en){
     memset(vis, 0, sizeof(vis));
     vis[st][en] = 1;
     tmp1.x = st;
     tmp1.y = en;
     tmp1.step = 0;
     priority_queueq;
     q.push(tmp1);
     while(!q.empty()){
         tmp1 = q.top();
         q.pop();
         if(a[tmp1.x][tmp1.y] == 'T'){
            flag = 1;
            k = tmp1.step;
            break;
         }
         //cout<=0 && tmp2.x< n&&tmp2.y >= 0&&tmp2.y < m && !vis[tmp2.x][tmp2.y] && a[tmp2.x][tmp2.y] != 'S'&&a[tmp2.x][tmp2.y] != 'R'){
               if(a[tmp2.x][tmp2.y] == 'B'){//如果遇到可以涉及的砖,回合加2
                  tmp2.step = tmp1.step+2;
               }
               else{
                 tmp2.step = tmp1.step+1;//其他情况回合加1

               }
               vis[tmp2.x][tmp2.y] = 1;
                  q.push(tmp2);
            }
         }
     }
}
int main(){
    int st, en;
    while(scanf("%d %d", &n, &m) && (m != 0 && n != 0)){
        for(int i = 0; i < n; i++){
            scanf("%s", a[i]);
            for(int j = 0; j < m; j++){
                if(a[i][j] == 'Y'){
                   st = i;
                   en = j;
                }
            }
        }
        flag = 0;
        bfs(st, en);
        if(!flag)cout<<-1<

泉水   HRBUST    1143

链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1143

#include
#include
#include
#include
#include
using namespace std;
#define N 1005
struct Node {
       int x, y, pre;
}tmp1, tmp2;
int vis[N][N];
int a[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1,-1, 0,  0};
int n, m, step;
void bfs(int st, int en, int pre){
     memset(vis, 0, sizeof(vis));
     vis[st][en] = 1;
     tmp1.x = st;
     tmp1.y = en;
     step = 0;
     tmp1.pre = pre;
     queueq;
     q.push(tmp1);
     while(!q.empty()){
        tmp1 = q.front();
        q.pop();
        //cout< 0 && tmp2.x <= n && tmp2.y > 0 &&tmp2.y <= m && !vis[tmp2.x][tmp2.y] && a[tmp2.x][tmp2.y] <= tmp1.pre){
                vis[tmp2.x][tmp2.y] = 1;
                step++;
                tmp2.pre = tmp1.pre;
                //cout<>a[i][j];
            }
        }
        pre = a[st][en];
        //cout<

马的遍历    洛谷    P1443

链接:https://www.luogu.org/problemnew/show/P1443

#include
#include
#include
#include
#include
using namespace std;
#define N 405
struct Node{
       int x, y;
}tmp1, tmp2;
queueq;
int dx[8] = {1,-1,2,-2,-1,1,2,-2};
int dy[8] = {2,2,1,1,-2,-2,-1,-1};
int vis[N][N];
int dis[N][N];
int n, m;
void Bfs(int st, int en){
     memset(vis, 0, sizeof(vis));
     memset(dis, 0, sizeof(vis));
     tmp1.x = st;
     tmp1.y = en;
     vis[tmp1.x][tmp1.y] = 1;
     q.push(tmp1);
     while(!q.empty()){
        tmp1 = q.front();
        q.pop();
        for(int i = 0; i < 8; i++){
            tmp2.x = tmp1.x + dx[i];
            tmp2.y = tmp1.y + dy[i];
            if(tmp2.x >= 1 && tmp2.x <= n && tmp2.y >= 1 && tmp2.y <= m && !vis[tmp2.x][tmp2.y]){
                q.push(tmp2);
                dis[tmp2.x][tmp2.y] = dis[tmp1.x][tmp1.y] + 1;
                vis[tmp2.x][tmp2.y] = 1;
            }
        }
     }
}
int main(){
    int st, en;
    cin>>n>>m>>st>>en;
    Bfs(st, en);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(dis[i][j] || (i == st && j == en))
            printf("%-5d",dis[i][j]);
            else printf("%-5d", -1);
        }
        cout<

Rescue   HDU   1242

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

#include
#include
#include
#include
#include
#define N 205
using namespace std;
struct Node{
       int x, y, val;
       friend bool operator<(Node A, Node B){
              return A.val > B.val;
       }
}tmp1, tmp2;
priority_queueq;
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};
int vis[N][N];
char a[N][N];
int n, m, flag, k;
void bfs(int st, int en){
     memset(vis, 0, sizeof(vis));
     vis[st][en] = 1;
     tmp1.x = st;
     tmp1.y = en;
     tmp1.val = 0;
     q.push(tmp1);
     while(!q.empty()){
        tmp1 = q.top();
        q.pop();
        if(a[tmp1.x][tmp1.y] == 'r'){
                k = tmp1.val;
                flag = 1;
                break ;
        }
        // cout<= n || tmp2.x < 0 || tmp2.y < 0 || tmp2.y >= m || vis[tmp2.x][tmp2.y]|| a[tmp2.x][tmp2.y] == '#')
                continue;
            if(a[tmp2.x][tmp2.y] == 'x'){
                tmp2.val = tmp1.val + 2;
                q.push(tmp2);
            }
            else {
                tmp2.val = tmp1.val + 1;
                q.push(tmp2);
            }
            vis[tmp2.x][tmp2.y] = 1;
        }

     }

}
int main(){
    int st, en;
    while(~scanf("%d %d", &n, &m)){
        while(!q.empty())q.pop();
        for(int i = 0; i < n; i++){
            scanf("%s", a[i]);
        }
        flag = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(a[i][j] =='a'){
                    st = i;
                    en = j;
                }
            }
        }
        bfs(st, en);
        if(!flag)cout<<"Poor ANGEL has to stay in the prison all his life."<

 

你可能感兴趣的:(算法)