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; }
标签:hdu,hy,1312,int,BFS,start,dx From: https://www.cnblogs.com/accbulb/p/18005025