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

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

填涂颜色    洛谷    P1162

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

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

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define N 100
struct Node{
       int x, y;
} p, tmp;
queue<Node>q;
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<<dis[i][j]<<" ";
            }
            cout<<endl;
        }
    }
return 0;
}

Battle City     POJ   2312

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

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
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_queue<Node>q;
     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<<tmp1.x<<" "<<tmp1.y<<" "<<tmp1.step<<endl;
         for(int i = 0; i < 4; i++){
            tmp2.x = tmp1.x + dx[i];
            tmp2.y = tmp1.y + dy[i];
            if(tmp2.x >=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<<endl;
        else cout<<k<<endl;
    }
return 0;
}

泉水   HRBUST    1143

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

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
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;
     queue<Node>q;
     q.push(tmp1);
     while(!q.empty()){
        tmp1 = q.front();
        q.pop();
        //cout<<tmp1.x<<" "<<tmp1.y<<endl;
        for(int i = 0; i < 4; i++){
            tmp2.x = tmp1.x + dx[i];
            tmp2.y = tmp1.y + dy[i];
            if(tmp2.x > 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<<tmp2.x<<" "<<tmp2.y<<" "<<a[tmp2.x][tmp2.y]<<endl;
                q.push(tmp2);
            }
        }
     }
}
int main(){
    int st, en;
    int pre;
    while(~scanf("%d %d %d %d", &n, &m, &st, &en)){
        for(int i = 1; i <= n; i++ ){
            for(int j = 1; j <= m; j++){
                cin>>a[i][j];
            }
        }
        pre = a[st][en];
        //cout<<pre<<endl;
        bfs(st, en, pre);
        cout<<step+1<<endl;


    }
return 0;
}

马的遍历    洛谷    P1443

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define N 405
struct Node{
       int x, y;
}tmp1, tmp2;
queue<Node>q;
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<<endl;
    }

return 0;
}

Rescue   HDU   1242

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#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_queue<Node>q;
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<<tmp1.x<<" "<<tmp1.y<<" "<<tmp1.val<<endl;
        for(int i = 0; i < 4; i++){
            tmp2.x = tmp1.x + dx[i];
            tmp2.y = tmp1.y + dy[i];
            if(tmp2.x >= 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."<<endl;
        else cout<<k<<endl;
    }
return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值