廣搜2 —— Red and Black

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
題意:從“@”(黑磚)開始走,找能走多少塊黑磚(“.”表示可以走的黑磚),只能走黑磚,“#”表示紅磚,以0 0 結束,簡單的搜索題。
給一下代碼:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
using namespace std;
char ss[100][100];
int dis[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m, vis[100][100], t1, t2;
struct xx
{
    int x, y;
}vv;
queue <xx> qq;
int check(int x, int y)
{
    if((x >= 0 && x < n) && (y >= 0 && y < m)) return 1;
    return 0;
}
void bfs()
{
    int ans = 1;
    while(!qq.empty())
    {
        vv = qq.front();
        qq.pop();
        int x1 = vv.x;
        int y1 = vv.y;
        for(int i = 0; i < 4; i++)
        {
            int x0 = x1 + dis[i][0];
            int y0 = y1 + dis[i][1];
            if(check(x0, y0) && ss[x0][y0] != '#' && !vis[x0][y0])
            {
                ans++;
                vis[x0][y0] = -1;
                vv.x = x0;
                vv.y = y0;
                qq.push(vv);
            }
        }
    }
    printf("%d\n", ans);
}
int main()
{
    while(~scanf("%d %d", &m, &n))
    {
        if(!n && !m) break;
        memset(vis, 0, sizeof(vis));
        int flag = 0;
        for(int i = 0; i < n; i++)
            scanf("%s", ss[i]);
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(ss[i][j] == '@')
                {
                    vis[i][j] = -1;
                    vv.x = i;
                    vv.y = j;
                    qq.push(vv);
                    flag = 2;
                    break;
                }
            }
            if(flag) break;
        }
        bfs();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章