求助,0分,大佬帮帮忙,广搜
查看原帖
求助,0分,大佬帮帮忙,广搜
544115
1234554321abcd楼主2023/5/28 21:07
#include<bits/stdc++.h>

using namespace std; int dx[]={1,-1,0,0},dy[]={0,0,1,-1},n,m,ans; char a[505][505]; struct node { int xx,yy; }top; queue Q; void bfs(int x,int y) { node tmp={x,y}; Q.push(tmp); while(!Q.empty()) { top=Q.front(); Q.pop(); for(int i=0;i<4;i++) { int nx=dx[i]+top.xx; int ny=dy[i]+top.yy; if(nx>=0 && nx<=n+1 && ny>=0 && ny<=m+1 && a[nx][ny]==0) { node tp={nx,ny}; Q.push(tp); a[nx][ny]=3; } } } } int main() { cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j]; bfs(0,0); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(a[i][j]==0) ans++; cout<<ans<<endl; return 0; }

2023/5/28 21:07
加载中...