首页 > 代码库 > HDU 1312 Red and Black (dfs)
HDU 1312 Red and Black (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17773 Accepted Submission(s): 10826
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
题目大意: “.”代表黑地板,“#”代表红地板,“@”代表人的位置,人可以经过黑地板不能经过红地板,问在给定的图中人一共可以经过多少块地板
解题思路:深搜,在每个位置都搜索该位置的上下左右四个位置,将图中能够经过的点全部搜一遍
AC代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <stack> 6 #include <queue> 7 using namespace std; 8 int w,h; 9 int a[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; //位置数组 10 char str[22][22];11 int f[22][22]; //标记该位置是否走过 12 int sum;13 void dfs(int x,int y)14 {15 f[x][y] = 1;16 for (int i = 0; i < 4; i ++)17 {18 int x1=x + a[i][0];19 int y1=y + a[i][1];20 if (x1 >= 0 && x1 < h && y1 >= 0 && y1 < w && str[x1][y1]!=‘#‘ && f[x1][y1] == 0) 21 { //判断边界、是否是不能经过的红地板、该地板是否已经经过 22 sum ++;23 dfs(x1,y1);24 }25 }26 }27 int main ()28 {29 int i,j,x,y;30 while (scanf("%d%d",&w,&h),w&&h)31 {32 for (i = 0; i < h; i ++)33 scanf("%s",str[i]);34 35 memset(f,0,sizeof(f));36 for (i = 0; i < h; i ++)37 for (j = 0; j < w; j ++)38 if (str[i][j] == ‘@‘) //找出人的位置 39 {40 x = i;41 y = j;42 break;43 }44 sum = 1;45 dfs(x,y);46 printf("%d\n",sum);47 }48 return 0;49 }
HDU 1312 Red and Black (dfs)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。