but it solve all critical test cases.
Code: Select all
#include<bits/stdc++.h>
using namespace std;
char grid[105][105];
int x[105],y[105],vis[105][105],m,n,cnt;
int fx[] = {1,-1,0,0,1,1,-1,-1};
int fy[] = {0,0,1,-1,1,-1,1,-1};
void dfs(int a,int b)
{
int i,j,k ;
vis[a][b] = 1;
for( i = 0 ; i < 8 ; i++ )
{
if(cnt == 100) return ;
int nx = a + fx[i];
int ny = b + fy[i];
if(nx >= 1 && nx <= m && ny >= 1 && ny <= n && vis[nx][ny] == 0 && grid[nx][ny] == '@')
{
cnt++;
dfs(nx,ny);
}
}
}
int main()
{
while(scanf("%d%d",&m,&n) == 2)
{
if(m == 0)
return 0;
int l = 0;
for(int i = 1 ; i <= m ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
scanf(" %c",&grid[i][j]);
if(grid[i][j] == '@')
{
x[l] = i;
y[l++] = j;
}
}
}
int t = 0;
for(int k = 0 ; k < l ; k++)
{
cnt = 0;
if(vis[x[k]][y[k]] == 0)
{
cnt = 0;
dfs(x[k],y[k]);
t++;
}
}
printf("%d\n",t);
memset(vis,0,sizeof vis);
}
}