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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Sample Output 45 59 6 13
题意: n*m的方阵有红格或是黑格,只能走黑格 每次只能走上下左右四个紧邻方向的格子,求 这个人最后能走多少个黑格子。
分析: dfs水题。从第一个黑格子开始递归的搜索, 每次搜索一个黑格子后为了以后不再重复走 这个黑格子,就把当前搜索的这个黑格子换 成红格子,然后继续dfs。。。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312%EF%BC%9B 题目大意:一个长方形空间,上面铺红色和黑色瓦片,一个人起初站在黑色瓦片上,每次可以走到相邻的4个黑色瓦片上,输入数据,求其能走过多少瓦片 题意:某人在@处为起点(也包括@点)#为墙,点(.)为通路,问最多能走多远统计能走几个点(加上@这个点) 思路:用dfs; 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
本文章由[谙忆]编写, 所有权利保留。 欢迎转载,分享是进步的源泉。
转载请注明出处:http://chenhaoxiang.cn
本文源自【人生之旅_谙忆的博客】