POJ 1979 Red and Black

Posted zoom1109

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1979 Red and Black相关的知识,希望对你有一定的参考价值。

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)
The end of the input is indicated by a line consisting of two zeros.

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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0

思路一:(DFS)

技术图片
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int N = 30;
 5 int row, col, sx, sy;
 6 int vis[N][N];
 7 char a[N][N];
 8 int dir[4][2] = 1, 0, -1, 0, 0, -1, 0, 1;
 9 void dfs(int x, int y)
10     for(int i = 0; i < 4; i++)
11         int tx = dir[i][0] + x, ty = dir[i][1] + y;
12         if(tx < 0 || tx > row - 1 || ty < 0 || ty > col - 1)    continue;
13         if(!vis[tx][ty] && a[tx][ty] == .)
14             vis[tx][ty] = 1;
15             dfs(tx, ty);
16         
17     
18 
19 int main()
20     while(cin >> col >> row)
21         if(col == 0 && row == 0)    break;
22         int cnt = 0;
23         memset(vis, 0, sizeof(vis));
24         for(int i = 0; i < row; i++)
25             for(int j = 0; j < col; j++)
26                 cin >> a[i][j];
27                 if(a[i][j] == @)
28                     sx = i;    sy = j;
29                 
30             
31         
32         vis[sx][sy] = 1;
33         dfs(sx, sy);
34         for(int i = 0; i < row; i++)
35             for(int j = 0; j < col; j++)
36                 if(vis[i][j] == 1)    cnt++;
37             
38         
39         cout << cnt << endl;    
40     
41     return 0;
42 
Red and Black

 

思路二:(BFS)

技术图片
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 struct node
 6     int x, y;
 7 que[2003];
 8 char a[23][23];
 9 int book[25][25], cnt, h, w;
10 int pos[4][2] = 1, 0, -1, 0, 0, -1, 0, 1;
11 void bfs(int rx, int ry)
12     int head = 1, tail = 1;
13         que[tail].x = rx;    que[tail].y = ry;
14         book[rx][ry] = 1;
15         tail++;    cnt++;
16         while(head < tail)
17             for(int i = 0; i < 4; i++)
18                 int tx = que[head].x + pos[i][0];
19                 int ty = que[head].y + pos[i][1];
20                 if(tx < 0 || tx > h - 1 || ty < 0 || ty > w - 1)    continue;
21                 if(a[tx][ty] == . && book[tx][ty] == 0)
22                     cnt++;    
23                     book[tx][ty] = 1;
24                     que[tail].x = tx;    que[tail].y = ty;
25                     tail++;
26                 
27             
28             head++;
29         
30 
31 int main()
32     while(cin >> w >> h)
33         if(w == 0 && h == 0)    break;
34         memset(a, 0, sizeof(a));
35         memset(book, 0, sizeof(book));
36         cnt = 0;
37         for(int i = 0; i < h; i++)
38             cin >> a[i];
39         
40         int rx, ry;
41         for(int i = 0; i < h; i++)
42             for(int j = 0; j < w; j++)
43                 if(a[i][j] == @)
44                     rx = i;
45                     ry = j;
46                 
47             
48         
49         bfs(rx, ry);
50         cout << cnt << endl;
51     
52     return 0;
53 
Red and Black

 

以上是关于POJ 1979 Red and Black的主要内容,如果未能解决你的问题,请参考以下文章

Red and Black(poj-1979)

POJ1979 Red and Black

POJ 1979 Red and Black

POJ 1979 Red and Black(DFS)

poj-1979 red and black(搜索)

poj1979 Red and Black