Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用bfs把相邻的1改成0并且统计数量,为什么re了0.0#include<iostream> #include<queue> #include<cstring> using namespace std; const int maxn=2000+10; int a[maxn][maxn]; int vis[maxn][maxn]; int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; struct node{ int x,y; node(int a,int b):x(a),y(b){} }; int bfs(int x,int y){ int s=1; queue<node>st; vis[x][y]=1; st.push(node(x,y)); while(!st.empty()){ node now=st.front(); st.pop(); for(int i=0;i<4;i++){ int fx=now.x+dir[i][0]; int fy=now.y+dir[i][1]; if(a[fx][fy]==1&&!vis[fx][fy]){ vis[fx][fy]=1; s++; //cout<<fx<<" :"<<fy<<endl; a[fx][fy]=0; st.push(node(fx,fy)); } } } return s; } int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ memset(a,0,sizeof(a)); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ scanf("%d",&a[i][j]); } } int _max1=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i][j]==1){ _max1=max(_max1,bfs(i,j)); } } } printf("%d\n",_max1); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator