View Code of Problem 1064

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits> 
#include <queue>
#define M 10000
using namespace std;
char map[25][25];
bool vis[25][25];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
int ans,w,h;
void f(int x,int y)
{
	for(int i=0;i<4;i++)
	{
		if(map[x+fx[i]][y+fy[i]]=='.'&&vis[x+fx[i]][y+fy[i]]==false&&x+fx[i]>=0&&x+fx[i]<h&&y+fy[i]>=0&&y+fy[i]<w)
		{
			vis[x+fx[i]][y+fy[i]]=true;
			ans++;
			f(x+fx[i],y+fy[i]);
		}
	}
}
int main()
{
	while(~scanf("%d%d",&w,&h)&&w||h)
	{
		int x,y;
		memset(vis,false,sizeof(vis));
		for(int i=0;i<h;i++)
		{
			getchar();
		    for(int j=0;j<w;j++)
		    {
		    	scanf("%c",&map[i][j]);
		    	if(map[i][j]=='@')
				{
		    		vis[i][j]=true;
		    		x=i;y=j;
				}
			}
		}
		ans=0;
		f(x,y);
		printf("%d\n",ans+1);
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 1064