hdu 1312 Red and Black (BFS)

Problem - 1312 (hdu.edu.cn)

BFS模板题

复制代码
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int wx,hy,num;
char room[25][25];
#define CHECK(x,y) (x>=0&&x<wx&&y>=0&&y<hy)
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node{int x,y;};
void BFS(int dx,int dy){
    num=1;
    queue<node> q;
    node start,next;
    start.x=dx;
    start.y=dy;
    q.push(start);
    while(!q.empty()){
        start=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            next.x=start.x+dir[i][0];
            next.y=start.y+dir[i][1];
            if(CHECK(next.x,next.y)&&room[next.x][next.y]=='.'){
                room[next.x][next.y]='#';
                num++;
                q.push(next);
            }
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int x,y,dx,dy;
    while(cin>>wx>>hy){
        if(wx==0&&hy==0) break;
        for(y=0;y<hy;y++){
            for(x=0;x<wx;x++){
                cin>>room[x][y];
                if(room[x][y]=='@'){
                    dx=x;
                    dy=y;
                }
            }
        }
        num=0;
        BFS(dx,dy);
        cout<<num<<endl;
    }
    return 0;
}
复制代码

 

posted @   ACCbulb  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示