#include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; int main() { // freopen("input.txt","r",stdin); int dimension,test=1; while(scanf("%d\n",&dimension)==1) { int number[dimension+1][dimension+1]; for(int i=1;i<=dimension;i++) { for(int j=1;j<=dimension;j++) { char ch; ch=getchar(); number[i][j]=ch-'0'; } getchar(); } int count=0; for(int i=1;i<=dimension;i++) { for(int j=1;j<=dimension;j++) { if(number[i][j]==1) { count++; queue<int>r,c; r.push(i); c.push(j); while(!r.empty()) { int x=r.front(),y=c.front(); r.pop();c.pop(); if(x-1>0&&number[x-1][y]==1) { r.push(x-1);c.push(y);number[x-1][y]=0; } if(y-1>0&&number[x][y-1]==1) { r.push(x);c.push(y-1);number[x][y-1]=0; } if(x+1<=dimension&&number[x+1][y]==1) { r.push(x+1);c.push(y);number[x+1][y]=0; } if(y+1<=dimension&&number[x][y+1]==1) { r.push(x);c.push(y+1);number[x][y+1]=0; } if(x-1>0&&y-1>0&&number[x-1][y-1]==1) { r.push(x-1);c.push(y-1);number[x-1][y-1]=0; } if(x-1>0&&y+1<=dimension&&number[x-1][y+1]==1) { r.push(x-1);c.push(y+1);number[x-1][y+1]=0; } if(x+1<=dimension&&y-1>0&&number[x+1][y-1]==1) { r.push(x+1);c.push(y-1);number[x+1][y-1]=0; } if(x+1<=dimension&&y+1<=dimension&&number[x+1][y+1]==1) { r.push(x+1);c.push(y+1);number[x+1][y+1]=0; } } } } } printf("Image number %d contains %d war eagles.\n",test++,count); } return 0; }
Sunday, August 17, 2014
uva : 352 - The Seasonal War
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment