hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8435 Accepted Submission(s): 5248
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.
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)
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
DFS搜索,入门题。
规定地图中有可通行的位置,也有不可通行的位置,已知起点,求一个连通分量。说白了就是求一个点的与它相连的部分。在这道题里输出相连的位置的数目。
思路是从起点开始,遍历每一个到达的点的四个方向,到达一个位置就将这个位置的字符变成不可走的'#',并且计数+1。其实就是计数将可走变成不可走的操作进行了多少次。
代码一:
1 #include <iostream>
2 using namespace std;
3 int cnt;
4 char a[22][22];
5 int n,m;
6 int dx[4] = {0,1,0,-1}; //方向
7 int dy[4] = {1,0,-1,0};
8 bool judge(int x,int y)
9 {
10 if(x<1 || x>n || y<1 || y>m)
11 return 1;
12 if(a[x][y]=='#')
13 return 1;
14 return 0;
15 }
16 void dfs(int cx,int cy)
17 {
18 cnt++;
19 a[cx][cy] = '#';
20 int i;
21 for(i=0;i<4;i++){
22 int nx = cx + dx[i];
23 int ny = cy + dy[i];
24 if(judge(nx,ny))
25 continue;
26 //可以走
27 dfs(nx,ny);
28 }
29 }
30 int main()
31 {
32 while(cin>>m>>n){
33 if(n==0 && m==0) break;
34 int i,j;
35 int x,y;
36 for(i=1;i<=n;i++)
37 for(j=1;j<=m;j++){
38 cin>>a[i][j];
39 if(a[i][j]=='@') //记录开始的位置
40 x=i,y=j;
41 }
42 cnt = 0;
43 dfs(x,y);
44 cout<<cnt<<endl;
45 }
46 return 0;
47 }
这种方法直接返回结果,两种不同的写法,代码二:
1 #include <iostream>
2 using namespace std;
3 char a[22][22];
4 int n,m;
5 int dx[4] = {0,1,0,-1}; //方向
6 int dy[4] = {1,0,-1,0};
7 bool judge(int x,int y)
8 {
9 if(x<1 || x>n || y<1 || y>m)
10 return 1;
11 if(a[x][y]=='#')
12 return 1;
13 return 0;
14 }
15 int dfs(int cx,int cy)
16 {
17 int i,sum=0;
18 a[cx][cy] = '#'; //走过的这一步覆盖
19 for(i=0;i<4;i++){
20 int nx = cx + dx[i];
21 int ny = cy + dy[i];
22 if(judge(nx,ny))
23 continue;
24 //可以走
25 sum+=dfs(nx,ny);
26 }
27 return sum==0?1:sum+1;
28 }
29 int main()
30 {
31 while(cin>>m>>n){
32 if(n==0 && m==0) break;
33 int i,j;
34 int x,y;
35 for(i=1;i<=n;i++)
36 for(j=1;j<=m;j++){
37 cin>>a[i][j];
38 if(a[i][j]=='@') //记录开始的位置
39 x=i,y=j;
40 }
41 cout<<dfs(x,y)<<endl;
42 }
43 return 0;
44 }
Freecode : www.cnblogs.com/yym2013