# POJ-1979(BFS)

题目链接:http://poj.org/problem?id=1979;
Red and Black
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 44311 Accepted: 24002
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
题意理解:典型的BFS板子题,因为自己对BFS的题不是很熟练,所以特意找了这道题来练手。题目大概意思就是与@相连通的" . "有多少个,只能走4个方向。被#隔断的不算 。最后要注意@也算一个。把最终结果加一。解题思路很简单,用BFS的板子往上面套就可以了。那就说说自己从这道题里悟出的东西。讲讲BFS的思路。
在这里插入图片描述
我们需要一个队列,从起点开始将起点放入队列中,然后进入一个循环,将队列中的起点取出来看他是否有子节点。

在这里插入图片描述
如果有又将节点放队列里,把自己pop掉
在这里插入图片描述
对二三节点依然重复上面的操作。直到队列为空,或者找到要找的数就终止。这道题里就是将节点换成了四个方向,大体思路没变。

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int Move[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char str[25][25];
int vis[25][25];
int n, m, num;
struct node{
    int x, y;
};
void bfs(int a, int b)
{
    //printf("%d %d??\n", a, b);
    queue <node> q;
    node s, e;
    s.x = a;
    s.y = b;
    q.push(s);
    while(!q.empty())
    {
        s = q.front();
        q.pop();
        int x = s.x;
        int y = s.y;
        for(int i = 0; i < 4; ++i)
        {
            x = s.x + Move[i][0];
            y = s.y + Move[i][1];
      //      printf("%d %d\n", Move[i][0], Move[i][1]);
            if(x >= 0 && x < m && y >= 0 && y < n && str[x][y] == '.' && !vis[x][y])
            {
                //printf("%d %d %d\n", x, y, num);
                num++;
                e.x = x;
                e.y = y;
                vis[x][y] = 1;
                q.push(e);
            }
        }
    }

}
int main()
{
    while(scanf("%d%d", &n, &m), n, m)
    {
        memset(vis, 0, sizeof(vis));
        num = 0;
        for(int i = 0; i < m; ++i)
            scanf("%s", str[i]);

        for(int i = 0; i < m; ++i)
            for(int j = 0; j < n; ++j)
            {
                if(str[i][j] == '@')
                {
                    bfs(i, j);
                }
            }
        printf("%d\n", num + 1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40739219/article/details/83268715