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
6 9
…#.
…#
…
…
…
…
…
#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.
…
11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13
深度优先搜索代码:
#include <iostream>
using namespace std;
struct pos{//创建点的结构体
int x, y;
}p;
int wa[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//分别代表向上/下/左/右移动,
int num; //第一位为行变化,第二位为列变化
char tiles[23][23];//将区域设置大一点,不用考虑边界情况
void walk(int px, int py) {//移动函数
int x = px, y = py;
num++;//移动一步,可到达的砖数 +1
tiles[px][py] = '#';//将当前所在砖块标记
for (int i = 0; i < 4; i++) {//分别尝试上下左右移动
px += wa[i][0];
py += wa[i][1];
if (tiles[px][py] == '.')
walk(px, py);
px = x; py = y;//切记回复到起始位置
}
}
int main(){
int w, h;
while (cin >> w >> h && (w || h)) {
num = 0;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
cin >> tiles[i][j];
if (tiles[i][j] == '@') {//记录起点
p.x = i; p.y = j;
}
}
walk(p.x, p.y);
cout << num << endl;
for (int i = 1; i <= h; i++)//“将区域设置大一点,不考虑边界情况”的骚操作
for (int j = 1; j <= w; j++)
tiles[i][j] = NULL;
}
}
广度优先搜索代码:
#include <iostream>
#include<queue>
using namespace std;
struct pos{//创建点的结构体
int x, y;
}p;
int wa[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//分别代表向上/下/左/右方向移动
int num;//记录砖块数
char tiles[23][23];//将区域设置大一点,不用考虑边界情况
void walk() {//移动函数
num++;
queue<pos>q;pos start;
q.push(p);//起点进队
while (!q.empty()) {
start = q.front(); q.pop();//队顶元素出队
int x = start.x, y = start.y;
for (int i = 0; i < 4; i++) {//尝试向上下左右四个方向移动
start.x += wa[i][0];
start.y += wa[i][1];
if (tiles[start.x][start.y] == '.') {//相邻砖块入队
tiles[start.x][start.y] = '#';
num++;
q.push(start);
}
start.x = x;start.y = y;//注意回复到起点位置,以便下次尝试其他方向
}
}
}
int main(){
int w, h;
while (cin >> w >> h && (w || h)) {
num = 0;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
cin >> tiles[i][j];
if (tiles[i][j] == '@') {//记录起点位置
p.x = i; p.y = j;
}
}
walk();
cout << num << endl;
for (int i = 1; i <= h; i++)//不考虑边界情况的骚操作
for (int j = 1; j <= w; j++)
tiles[i][j] = NULL;
}
}