POJ 1979 Red and Black(DFS)

2018-06-17 22:15:03来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

题目网址:http://poj.org/problem?id=1979

题目:

Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 36033   Accepted: 19517

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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

思路:

一道很简单的搜索题,用DFS和BFS都能做。不需要重复走一个点,走过一个点就把它设为红瓷砖,不用再走了。

代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 int n,m;
 5 char tiles[25][25];
 6 int xb,yb;
 7 int dirx[4]={0,0,1,-1};//行方向数组
 8 int diry[4]={1,-1,0,0};//列方向数组
 9 int res;
10 bool check(int x,int y){
11     if(x<0 || x>=n) return false;//判断是否超边界
12     if(y<0 || y>=m) return false;
13     if(tiles[x][y]!='.')    return false;//判断是否为黑瓷砖
14     return true;
15 }
16 void dfs(int x,int y){
17     for(int d=0,xx,yy; d<4; d++){
18         xx=x+dirx[d];
19         yy=y+diry[d];
20         if(check(xx, yy)){
21             res++;
22             tiles[xx][yy]='#';//将走过的点都设为红瓷砖,就不会再走
23             dfs(xx, yy);
24         }
25     }
26 }
27 int main(){
28     int flag;
29     while(scanf("%d%d ",&m,&n)==2 && (n+m)){
30         flag=0;
31         res=1;
32         for (int i=0; i<n; i++) {
33             gets(tiles[i]);
34             for (int j=0; j<m && !flag; j++) {
35                 if(tiles[i][j]=='@'){//寻找起点
36                     xb=i;
37                     yb=j;
38                     flag=1;
39                     break;
40                 }
41             }
42         }
43         tiles[xb][yb]='#';
44         dfs(xb,yb);
45         printf("%d\n",res);
46     }
47     return 0;
48 }

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:POJ 3278 Catch That Cow(BFS)

下一篇:P2216 [HAOI2007]理想的正方形(二维RMQ)