572 WA

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

omorsec
New poster
Posts: 1
Joined: Wed Sep 21, 2016 7:36 am

572 WA

I have got wrong ans for this code
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);
}

}
``````

Fake_Death
New poster
Posts: 1
Joined: Sun Dec 04, 2016 3:08 am

Re: 572 WA

increase the range of x[] and y[] from 105 to 10005. Hope so you get acc