杭电 1312 Red and Black(BFS/DFS)

8 篇文章 0 订阅
7 篇文章 0 订阅
该问题描述了一个矩形房间,由红黑相间的方格瓷砖覆盖。一个人站在黑色瓷砖上,只能在黑色瓷砖间向四个相邻瓷砖移动。程序需计算从起点能到达的黑色瓷砖数量。输入包含多组数据,每组数据先给出房间的宽W和高H,接着H行每行W个字符表示瓷砖颜色,'.'为黑色,'#'为红色,'@'表示人的初始位置。输出每组数据的可达黑色瓷砖数。注意W为列,H为行。此题适合初学者练习BFS或DFS搜索算法。
摘要由CSDN通过智能技术生成

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;
}
晚上在杏花树下喝酒,聊天,直到月色和露水一般清凉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值