题目来源:北京大学POJhttp://poj.org/problem?id=1979
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
这个题目可以用回溯法解决,跟我博客里面的“贪吃蛇游戏”有些类似,见代码:
分享到:
相关推荐
acm poj 比较详细的将poj的题目进行了分类,如dp,搜索,数据结构等等
北大acm JudgeOnline 1979号 Red and Black题目的C++语言源代码
acm poj题目分类介绍 包含一个题解文档 acm poj题目分类介绍 包含一个题解文档
题目分类 目前网上最全的 PKU 的 网上所有的 分类总结 祝ACM 一路顺风
ACM Poj Pku 解题报告答案 打包 下载 600多题 史上最全 不是网上乱传的200多题,更不是100多题就挂着10分才能下的题 下了这个 大家也不要浪费分数去下载其它版本的了,基本上都有 共享 一起进步 中国加油 ACMer...
acm acm acm acm acm acm acm acm 题目分类 题目分类 题目分类
ACM poj1737,Connected Graph
北京大学ACM详解poj1001, 内容很充实。
POJ各题算法分类和题目推荐 ACM必看 POJ各题算法分类和题目推荐 ACM必看 POJ各题算法分类和题目推荐 ACM必看
就是一个acm训练的题集吧,其实不想要积分但是最少2积分。。。
pojACM题目分类,便于各类型同学分别做题有所参考
1001 1002 1003 1004 1005 1006 1007 1008 1011 1012 1013 1014 1015 1017 ...1979 1988 2000 2017 2075 2080 2081 2084 2105 2109 2127 2136 2140 2141 2153 2182 2192 2196 2201 2231 2243 2245 2247 2250 2253 2255 ...
poj 3715 Blue and Red.md
Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. ...
总共220题,题号囊括1000-3000多,从最简单到最典型。源码书写清晰优美,适合初学者入门,同样适合中级进阶。 这是我找了很久找到的,非常全,强烈向...在POJ上练习ACM和想实践cpp的朋友都适用,希望大家能学有所成!~
ACM PKU online judge problem 1025
poj两道题的c++实现。已经测试过可以通过oj
acm pku poj 1000 1001 1002 1003 1201
如果你为在poj上找不到解题思路而痛苦,那么这本书可以为你带来惊喜,里面包括了poj上一部分题解题报告~
整理的acm知识分类 ACM-POJ 算法训练指南