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).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 

Sample Output

45
59
6
13

PS:W and H are the numbers of tiles in the x- and y- directions一定要注意这句话,因为这是写出这个题目的关键。题目本身是一个很简单的搜索题目,一般都是初学者学习搜索最好的题目之一,言归正传这句话的意思是在w的方向上有x个瓦片,在h方向上面有y个瓦片,这就说明w是作为列,h是作为行,这在遍历的时候一定不要写错了。不要问我为什么
BFS

#include <bits/stdc++.h>
using namespace std;
char a[100][100];
int d[4][2] = {
   -1, 0, 1, 0, 0, 1, 0, -1};
int w, h, ans, vis[100][100];
struct node
{
   
    int x, y;
};
void bfs(int x, int y)
{
   
    ans = 1;
    queue<node> q;
    node s, e;
    s.x = x;
    s.y = y;
    q.push(s);
    while (!q.empty())
    {
   
        s = q.front();
        q.pop();
        for (int i = 0; i < 4; ++i)
        {
   
            e.x = s.x + d[i][0];
            e.y = s.y + d[i][1];
            if (e.x >= 1 && e.x <= h && e.y >= 1 && e.y <= w && a[e.x][e.y] == '.')
            {
   
                a[e.x][e.y] = '#';
                ++ans;
                q.push(e);
            }
        }
    }
}
int main()
{
   
    int xx, yy;
    while (~scanf("%d%d", &w, &h))
    {
   
        ans = 1;
        if (w == 0 && h == 0)
            return 0;
        for (int i = 1; i <= h; ++i)
            for (int j = 1; j <= w; ++j)
            {
   
                scanf(" %c", &a[i][j]);
                if (a[i][j] == '@')
                {
   
                    xx = i;
                    yy = j;
                }
            }
        bfs(xx, yy);
        printf("%d\n", ans);
    }
    return 0;
}

DFS
dfs最大的特点就是当前遍历的点只要有路就会一直走到最深处,直到无路可走,再退回一步,看在上一步的位置能不能换个方向继续走下去,这样就遍历了所有的可能性

#include <bits/stdc++.h>
using namespace std;
char a[100][100];
int d[4][2] = {
   -1, 0, 1, 0, 0, 1, 0, -1};
int w, h, ans, vis[100][100];
struct node
{
   
    int x, y;
};
void dfs(int x, int y)
{
   
    a[x][y] = '#';//标记起点
    ans++;
    for (int i = 0; i < 4; ++i)
    {
   
        int xx = x + d[i][0];
        int yy = y + d[i][1];
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && a[xx][yy] == '.')
            dfs(xx, yy);
    }
}
int main()
{
   
    int xx, yy;
    while (~scanf("%d%d", &w, &h))
    {
   
        ans = 0;
        if (w == 0 && h == 0)
            return 0;
        for (int i = 1; i <= h; ++i)
            for (int j = 1; j <= w; ++j)
            {
   
                scanf(" %c", &a[i][j]);
                if (a[i][j] == '@')
                {
   
                    xx = i;
                    yy = j;
                }
            }
        dfs(xx, yy);
        printf("%d\n", ans);
    }
    return 0;
}
晚上在杏花树下喝酒,聊天,直到月色和露水一般清凉