转载:https://blog.youkuaiyun.com/hurmishine/article/details/50927317
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
基本上都用的是DFS,但我的第一直觉用的是BFS,但是不知道如何结束,后来发现一般DFS都是要用到递归,所以要出现return来实现递归时条件不满足的情况,比如说在迷宫中找人,找终点,就是当前访问的坐标等于要找的人或终点的坐标。然而BFS一般都是用队列实现,while(队列非空)来判断,因为把所有的可能的坐标都放在队列里,然后出队入队直到把所有值都找遍了,就无值入队,队列为空时结束。
这里没有设visited数组,是因为本身a数组就可以判断,判断的办法即把所有访问过的置为’#’
用BFS
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
char a[21][21];
int vis[21][21];
int w, h;//w是列,h是行
int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
struct position
{
int x;
int y;
};
int sum;
bool judge(int x, int y)
{
if (x > 0 && x <= h&&y>0 && y <= w)
return true;
return false;
}
void Bfs(position place)
{
queue<position> q;
q.push(place);
position pos;
position temppos;
while (!q.empty())
{
pos = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
temppos.x = pos.x + dir[i][0];
temppos.y = pos.y + dir[i][1];
if (judge(temppos.x,temppos.y) &&a[temppos.x][temppos.y]=='.')
{
a[temppos.x][temppos.y] = '#';
q.push(temppos);
sum++;
}
}
}
}
int main()
{
while (scanf_s("%d%d", &w, &h) && w != 0 && h != 0)
{
memset(a, 0, sizeof(a));
memset(vis, 0, sizeof(vis));
position begin;
for (int i = 1; i <= h; i++)
{
getchar();
for (int j = 1; j <= w; j++)
{
a[i][j] = getchar();
if (a[i][j] == '@')
{
begin.x = i;
begin.y = j;
}
}
}
sum = 1;
Bfs(begin);
printf("%d\n", sum);
}
return 0;
}
用DFS
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
char a[21][21];
int w, h;//w是列,h是行
int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
struct position
{
int x;
int y;
};
int sum;
void Dfs(position place)
{
if (place.x <= 0 || place.x > h || place.y <= 0 || place.y > w||a[place.x][place.y]=='#')
return;
a[place.x][place.y] = '#';
sum++;
for (int i = 0; i < 4; i++)
{
position temp;
temp.x = place.x + dir[i][0];
temp.y = place.y + dir[i][1];
Dfs(temp);
}
}
int main()
{
while (scanf_s("%d%d", &w, &h) && w != 0 && h != 0)
{
memset(a, 0, sizeof(a));
position begin;
for (int i = 1; i <= h; i++)
{
getchar();
for (int j = 1; j <= w; j++)
{
a[i][j] = getchar();
if (a[i][j] == '@')
{
begin.x = i;
begin.y = j;
}
}
}
sum = 0;
Dfs(begin);
printf("%d\n", sum);
}
return 0;
}