zoj 2165 Red and Black

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2165

解题思路:DFS搜索

  1 ///////////////////////////////////////////////////////////////////////////
  2 //problem_id: zoj 2165
  3 //user_id: SCNU20102200088
  4 ///////////////////////////////////////////////////////////////////////////
  5 
  6 #include <algorithm>
  7 #include <iostream>
  8 #include <iterator>
  9 #include <iomanip>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <string>
 13 #include <vector>
 14 #include <cstdio>
 15 #include <cctype>
 16 #include <cmath>
 17 #include <queue>
 18 #include <stack>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 
 24 ///////////////////////////////////////////////////////////////////////////
 25 typedef long long LL;
 26 const double PI=acos(-1.0);
 27 ///////////////////////////////////////////////////////////////////////////
 28 
 29 ///////////////////////////////////////////////////////////////////////////
 30 //Add Code:
 31 int w,h,cnt;
 32 char tile[25][25];
 33 bool flag[25][25];
 34 const int x[]={-1,0,1,0};
 35 const int y[]={0,1,0,-1};
 36 
 37 void DFS(int i,int j){
 38     for(int p=0;p<4;p++){
 39         int xx=i+x[p],yy=j+y[p];
 40         if(xx<1 || xx>h || yy<1 || yy>w) continue;
 41         if(tile[xx][yy]=='.'){
 42             cnt++;
 43             tile[xx][yy]='#';
 44             DFS(xx,yy);
 45         }
 46     }
 47     return ;
 48 }
 49 ///////////////////////////////////////////////////////////////////////////
 50 
 51 int main(){
 52     ///////////////////////////////////////////////////////////////////////
 53     //Add code:
 54     while(scanf("%d%d",&w,&h)!=EOF){
 55         if(!(w||h)) break;
 56         int i,j,Sx,Sy;
 57         char c;
 58         scanf("%c",&c);
 59         for(i=1;i<=h;i++){
 60             for(j=1;j<=w;j++){
 61                 scanf("%c",&tile[i][j]);
 62                 if(tile[i][j]=='@'){
 63                     Sx=i;
 64                     Sy=j;
 65                     tile[i][j]='#';
 66                 }
 67             }
 68             scanf("%c",&c);
 69         }
 70         cnt=1;
 71         DFS(Sx,Sy);
 72         printf("%d\n",cnt);
 73     }
 74     ///////////////////////////////////////////////////////////////////////
 75     return 0;
 76 }
 77 
 78 ///////////////////////////////////////////////////////////////////////////
 79 /*
 80 Testcase:
 81 Input:
 82 6 9
 83 ....#.
 84 .....#
 85 ......
 86 ......
 87 ......
 88 ......
 89 ......
 90 #@...#
 91 .#..#.
 92 11 9
 93 .#.........
 94 .#.#######.
 95 .#.#.....#.
 96 .#.#.###.#.
 97 .#.#..@#.#.
 98 .#.#####.#.
 99 .#.......#.
100 .#########.
101 ...........
102 11 6
103 ..#..#..#..
104 ..#..#..#..
105 ..#..#..###
106 ..#..#..#@.
107 ..#..#..#..
108 ..#..#..#..
109 7 7
110 ..#.#..
111 ..#.#..
112 ###.###
113 ...@...
114 ###.###
115 ..#.#..
116 ..#.#..
117 0 0
118 Output:
119 45
120 59
121 6
122 13
123 */
124 ///////////////////////////////////////////////////////////////////////////

posted on 2013-08-18 10:59  SCNU20102200088  阅读(181)  评论(0编辑  收藏  举报

导航