Red and Black

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char map[21][21];
int M,N,res;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
int fun(int x,int y)
{
 if(x<0||y<0||x>=N||y>=M)
   return 0;
   return 1;
}
void DFS(int x,int y)
{
  int i,j,x1,y1;
  map[x][y]='#';
  for(i=0;i<4;i++)
  {
    x1=x+xx[i];
    y1=y+yy[i];
    if(map[x1][y1]=='.'&&fun(x1,y1))
    {
      res++;
      DFS(x1,y1);
    }
  }   
  
}
int main( )
{
  while(scanf("%d%d",&M,&N)!=EOF)
  {
   int i,j,a,b;
   res=0;
   getchar();
   for(i=0;i<N;i++)
   {
    for(j=0;j<M;j++)
      {
      scanf("%c",&map[i][j]);
      if(map[i][j]=='@')
      {
      a=i;
      b=j;
      }
      }
      getchar();
   }
   res++;
   map[a][b]='#';
   DFS(a,b);
   printf("%d\n",res);
   }
   
   return 0;
}

posted on 2011-05-11 03:19  more think, more gains  阅读(136)  评论(0编辑  收藏  举报

导航