这道题从名称来看看不出什么。
所以我们先读一下题干
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).
Sample Input:
一个人在长方形的房间里,房间里铺着红色或黑色的方形的地砖。他只能走到黑色地砖上,他每次可以移动到他周边四块地砖中的一块、但他不能踩在红色地砖上、只能踩在黑色地砖上。写个程序,数出他可以走到多少个黑色的地砖。
输入:
两个正整数W和H,分别依次对应X方向和Y方向,W和H都不超过20,H行中包含W个数,“.”代表黑色地砖,“#”代表红色地砖,“@”代表那个人站在黑色地砖上(每个数据表格中仅出现一次)。
输出:
把他能到达(包括他自己)的黑色地砖数量在一行输出。
很明显这是一道广搜题,我们不妨先定义一个队列,把初始位置入队。
然后判断,如果是则sum++(sum初始为0),接着把周围的四个数入队,然后出队。否则直接出队;
这里注意,入过队的一定要做标记,直到队列为空为止。
最后输出sum的值,便是结果。
原文:https://www.cnblogs.com/yichenjing/p/12248043.html