#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt;
int dx[]={1,1,1,0,0,-1,-1,-1};
int dy[]={1,0,-1,1,-1,1,0,-1};
ll g[1000][1000];
bool visited[1009][1009];
ll n;
void dfs(ll i,ll j)
{
visited[i][j]=1;
for(ll m=0;m<8;m++)
{
ll p=i+dx[m];
ll q=j+dy[m];
if(p>=0&&p<n&&q>=0&&q<n&&visited[p][q]==0&&g[p][q]==1)
{
dfs(p,q);
}
}
return;
}
int main()
{
ll i,j,k=1,l,m;
while(cin>>n)
{
memset(g,0,sizeof(g));
memset(visited,0,sizeof(visited));
for(i=0;i<n;i++)
{
string s;
cin>>s;
for(j=0;j<s.size();j++)
{
if(s[j]=='0')
{
g[i][j]=0;
}
else
{
g[i][j]=1;
}
}
}
cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(visited[i][j]==0&&g[i][j]==1)
{
cnt++;
dfs(i,j);
}
}
}
cout<<"Image number "<<k<<" contains "<<cnt<<" war eagles."<<endl;
k++;
}
return 0;
}
using namespace std;
typedef long long ll;
ll cnt;
int dx[]={1,1,1,0,0,-1,-1,-1};
int dy[]={1,0,-1,1,-1,1,0,-1};
ll g[1000][1000];
bool visited[1009][1009];
ll n;
void dfs(ll i,ll j)
{
visited[i][j]=1;
for(ll m=0;m<8;m++)
{
ll p=i+dx[m];
ll q=j+dy[m];
if(p>=0&&p<n&&q>=0&&q<n&&visited[p][q]==0&&g[p][q]==1)
{
dfs(p,q);
}
}
return;
}
int main()
{
ll i,j,k=1,l,m;
while(cin>>n)
{
memset(g,0,sizeof(g));
memset(visited,0,sizeof(visited));
for(i=0;i<n;i++)
{
string s;
cin>>s;
for(j=0;j<s.size();j++)
{
if(s[j]=='0')
{
g[i][j]=0;
}
else
{
g[i][j]=1;
}
}
}
cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(visited[i][j]==0&&g[i][j]==1)
{
cnt++;
dfs(i,j);
}
}
}
cout<<"Image number "<<k<<" contains "<<cnt<<" war eagles."<<endl;
k++;
}
return 0;
}
No comments:
Post a Comment