HDU ACM 1312 Red and Black(深搜DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1312

View Code
 1 /*题意:给出一个n*m的矩阵,矩阵有三种符号 @ # . 
 2 @ - 起点
 3 # - 障碍
 4 . - 通道
 5 求从@出发,可能移动到的位置的数目。
 6 
 7 注意: DFS时要注意第一个点的标记
 8 */
 9 #include "iostream"
10 #include "queue"
11 using namespace std;
12 struct FUN
13 {
14     int x;
15     int y;
16 };
17 int point[4][2]={{0,1},{0,-1},{1,0},{-1,0}};         //重要
18 char map[30][30]={0};
19 int sum;
20 int n;
21 int m;
22 void DFS(FUN f)
23 {
24     FUN a;
25     int i;
26     for(i=0;i<4;i++)
27     {
28         a.x = f.x + point[i][0];
29         a.y = f.y + point[i][1];
30         if( a.x>0 && a.y>0 && a.x<=m && a.y<=n && map[a.x][a.y]!='#')   //注意边界
31         {
32             
33             map[a.x][a.y]='#';
34             DFS(a);
35             sum++;
36         }
37     }
38     return ;
39 }
40 int main()
41 {  
42     int i;
43     int j;
44     char c;
45     FUN a;
46     while(cin>>n>>m,n+m)
47     {
48         for(i=1;i<=m;i++)
49         {
50             for(j=1;j<=n;j++)
51             {
52                 cin>>c;
53                 map[i][j]=c;
54                 if(c=='@')
55                 {
56                     map[i][j] = '#';
57                     a.x=i;
58                     a.y=j;
59                 }
60             }
61         }
62         sum = 1;
63         DFS(a);
64         cout<<sum<<endl;
65     }
66     return 0;
67 }

 

posted @ 2012-05-26 14:45  zx雄  阅读(326)  评论(0编辑  收藏  举报