hdu -1312 Red and Black

 

 

Red and Black

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3704 Accepted Submission(s): 2394


Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
 

 

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
 

 

Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
 

 

Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 

 

Sample Output
45
59
6
13
 

 

Source
 

 

Recommend
Eddy
 
 
题意:
     以'@'为起点,计算可以走多少步。条件:'*'可走,'#'不能走,所走过的'*'必须连成一块,中间不能有'#'隔断。
 
解题方法:
     可以利用广度遍历或者深度遍历都可以做出这道题。
 
 
View Code
 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 
 5 const int Max = 30;
 6 char used[Max][Max];
 7 int to[4][2]={-1,0,1,0,0,-1,0,1};
 8 int h, w; //行、列
 9 
10 typedef struct 
11 {
12     int x, y;
13 }Node;
14 
15 int BFS(int x, int y)
16 {
17     int count = 1;
18     queue<Node>Q;
19     Node go;
20     go.x = x;
21     go.y = y;
22     used[x][y] = '#';
23     Q.push(go);
24     while(!Q.empty())
25     {
26         go=Q.front();
27         Q.pop();
28         x = go.x;
29         y = go.y;
30         for(int i=0; i<4; i++)
31         {
32             go.x = x+to[i][0];
33             go.y = y+to[i][1];
34             if(used[go.x][go.y]=='.' && go.x>=0 && go.x<h && go.y>=0 && go.y<w )
35             {
36                 count++;
37                 used[go.x][go.y] = '#';
38                 Q.push(go);
39             }
40         }
41     }
42     return count;
43 }
44 
45 int main()
46 {
47     int x, y;
48     while(cin>>w>>h && (w+h)>0)
49     {
50         for(int i=0; i<h; i++)
51         {
52             scanf("%s", &used[i]);
53             for(int j=0; j<w; j++)
54             {
55                 if(used[i][j] == '@')
56                 {
57                     x = i;
58                     y = j;
59                 }
60             }
61         }
62         cout<<BFS(x, y)<<endl;
63     }
64     return 0; 
65 }

 

 
 1 /*
 2    HDUACM  diy  zhbit_BFS & DFS
 3    1001 Red and Black 
 4    2012.7.03  
 5    16:23
 6 */
 7 #include<iostream>
 8 #include<queue>
 9 using namespace std;
10 
11 int x,y;
12 int dd[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
13 char point[100][100];
14 struct pp 
15 {
16     int x;
17     int y;
18 };
19 
20 
21 int BFS(int h,int w)
22 {
23     pp front,a,go;
24     a.x=h;
25     a.y=w;
26     queue<pp>q;
27     q.push(a);
28     int sum=1;
29     while(!q.empty())
30     {
31         front = q.front();
32         q.pop();
33         for(int i=0; i<4; i++)
34         {
35             go.x=front.x+dd[i][0];
36             go.y=front.y+dd[i][1];
37             if(go.x>=0 && go.x <y && go.y>=0 && go.y<x)
38             {
39                 if(point[go.x][go.y]!='#')
40                 {
41                     sum++;
42                     q.push(go);
43                     point[go.x][go.y]='#';
44                 }
45             }
46         }
47     }
48     return sum;
49 }
50 
51 
52 int main()
53 {
54     
55     int h,w;
56     while(cin>>x>>y && x+y)
57     {
58         for(int i=0; i<y; i++)
59         {
60             cin>>point[i];
61             for(int j=0; j<x; j++)
62             {
63                 if(point[i][j] == '@')
64                 {
65                     point[i][j]='#';
66                     h=i;
67                     w=j;
68                 }
69             }
70         }
71         cout<<BFS(h,w)<<endl;
72     }
73     return 0;
74 }

 

 DFS

View Code
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 const int Max = 105;
 5 char map[Max][Max];
 6 bool used[Max][Max];
 7 int n,  m;
 8 int flag;
 9 int sum;
10 int to[8][2] = {0,-1, 0,1, 1,0, -1,0};
11 void DFS(int x, int y)
12 {
13     int a, b;
14     for(int i=0; i<4; i++)
15     {
16         a = x+to[i][0];
17         b = y+to[i][1];
18         if(a>=0 && a<m && b>=0 && b<n && !used[a][b] && map[a][b]=='.')
19         {
20             used[a][b] = true;
21             sum ++;
22             DFS(a, b);
23         }
24     }
25 }
26 int main()
27 {
28     while(cin>>n>>m && n+m)
29     {
30         int i, j;
31         int x, y;
32         memset(used, false, sizeof(used));
33         memset(map, 0, sizeof(map));
34         for(i=0; i<m; i++)
35         {
36             scanf("%s", map[i]);
37             for(j=0; j<n; j++)
38             {
39                 if(map[i][j] == '@')
40                 {
41                     x = i; 
42                     y = j;
43                     used[x][y] = true;
44                 }
45             }
46         }
47         sum = 1;
48         DFS(x, y);
49         cout<<sum<<endl;
50     }
51     return 0;
52 }

 

posted @ 2012-07-03 18:26  另Ⅰ中Feel▂  阅读(215)  评论(0编辑  收藏  举报