HDOJ搜索专题之Red and Black

简单搜索题。将能走到的地方走一下即可。

View Code
 1 #include <stdio.h>
 2 #define N 20
 3 using namespace std;
 4 int dx[4]={0,0,1,-1};
 5 int dy[4]={1,-1,0,0};
 6 char g[N][N];
 7 int n,m,cnt;
 8 void dfs(int i,int j)
 9 {
10   int ni,nj,d;
11   for(d=0;d<4;d++)
12   {
13     ni=i+dx[d];
14     nj=j+dy[d];
15     if(ni<0 || nj<0 || ni>=n || nj>=m || g[ni][nj]!='.') continue;
16     cnt++;
17     g[ni][nj]='#';
18     dfs(ni,nj);
19   }
20 }
21 int main()
22 {
23   int i,j,si,sj;
24   while(~scanf("%d%d",&m,&n))
25   {
26     if(n==0 || m==0)  break;
27     for(i=0;i<n;i++)
28     {
29       getchar();
30       for(j=0;j<m;j++)
31       {
32         scanf("%c",&g[i][j]);
33         if(g[i][j]=='@')  si=i,sj=j;
34       }
35     }
36     cnt=1;
37     g[si][sj]='#';
38     dfs(si,sj);
39     printf("%d\n",cnt);
40   }
41   return 0;
42 }
原文地址:https://www.cnblogs.com/algorithms/p/2496732.html