[每日一题] 85. 红与黑(图+BFS)

1. 题目来源

链接:红与黑
来源:牛客网

2. 题目说明

有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向相邻的(上下左右四个方向)黑色瓷砖移动。请写一个程序,计算你总共能够到达多少块黑色的瓷砖。

输入描述:

输入包含多组数据。
每组数据第一行是两个整数 m 和 n(1≤m, n≤20)。紧接着 m 行,每行包括 n 个字符。每个字符表示一块瓷砖的颜色,规则如下:

  1. “.”:黑色的瓷砖;
  2. “#”:白色的瓷砖;
  3. “@”:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。

输出描述:

对应每组数据,输出总共能够到达多少块黑色的瓷砖。

示例:

输入

9 6
....#.
.....#
......
......
......
......
......
#@...#
.#..#.

输出
45

3. 题目解析

  1. 输入的m和n就是代表输入后续会输入几行几列字符
  2. 第二行开始,输入的字符就是我们的“行走矩阵”,其中“.”->黑色的瓷砖, “#”->白色的瓷砖, “@”->黑色的瓷
    砖,并且你站在这块瓷砖上
  3. 这道题的核心问题是,从你站的位置开始,向周边任意位置走,你能直接走过的黑色瓷砖的总数是多少

该问题可以采用深度优先遍历的方式,统计所有的可能性,

4. 代码展示

// write your code here cpp
#include <bits/stdc++.h>

using namespace std;

struct pos {
    int x, y;
};

int bfs(vector<vector<char> > & map, vector<vector<bool> > & visit, pos & start) {
    const int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
    queue<pos> que;
    int count = 0;
    int m = map.size(), n = map[0].size();
 
    que.push(start);
    visit[start.x][start.y] = true; ++count;
    while (!que.empty()) {
        pos cur = que.front(), next;
        que.pop();
        for (int i = 0; i < 4; ++i) {
            next.x = cur.x + dir[i][0];
            next.y = cur.y + dir[i][1];
            if (next.x >= 0 && next.x < m && next.y >= 0 && next.y < n && \
                !visit[next.x][next.y] && map[next.x][next.y] == '.') {
                que.push(next);
                visit[next.x][next.y] = true;
                ++count;
            }
        }
    }
    return count;
}

int main() {
    int m, n;
    while (cin >> m >> n && (m * n)) {
        pos start;
        vector<vector<char>> map(m, vector<char>(n));
        vector<vector<bool>> visit(m, vector<bool>(n));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                visit[i][j] = false;
                cin >> map[i][j];
                if (map[i][j] == '@')
                    start.x = i, start.y = j;
            }
        }
        cout << bfs(map, visit, start) << endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
理这个问题的思路大致如下: 1. 首先读取输入的数据集合,直到遇到两个零为止。 2. 对于每个数据集合,找到初始位置的黑色瓷砖的坐标。 3. 使用深度优先搜索(DFS)或广度优先搜索(BFS算法,从初始位置开始遍历相邻的黑色瓷砖,并标记已访问过的瓷砖。 4. 统计访问过的黑色瓷砖数量,并输出结果。 以下是一个使用C++实现的示例代码: ```cpp #include <iostream> #include <vector> #include <queue> using namespace std; // 定义瓷砖颜色 const char BLACK_TILE = '.'; const char RED_TILE = '#'; const char START_TILE = '@'; // 定义相邻瓷砖的四个方向 const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; // 使用广度优先搜索算法遍历相邻的黑色瓷砖 int bfs(vector<vector<char>>& tiles, int startX, int startY) { int count = 0; int w = tiles[0].size(); int h = tiles.size(); // 创建一个队列用于保存待访问的黑色瓷砖 queue<pair<int, int>> q; // 标记已访问过的黑色瓷砖 vector<vector<bool>> visited(h, vector<bool>(w, false)); // 将初始位置加入队列并标记为已访问 q.push(make_pair(startX, startY)); visited[startY][startX] = true; while (!q.empty()) { pair<int, int> curr = q.front(); q.pop(); int x = curr.first; int y = curr.second; // 统计访问过的黑色瓷砖数量 count++; // 遍历相邻的黑色瓷砖 for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; // 判断相邻坐标是否合法且为黑色瓷砖 if (nx >= 0 && nx < w && ny >= 0 && ny < h && tiles[ny][nx] == BLACK_TILE && !visited[ny][nx]) { // 将相邻黑色瓷砖加入队列并标记为已访问 q.push(make_pair(nx, ny)); visited[ny][nx] = true; } } } return count; } int main() { int w, h; while (cin >> w >> h && (w != 0 || h != 0)) { // 读取瓷砖数据 vector<vector<char>> tiles(h, vector<char>(w)); int startX, startY; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> tiles[i][j]; // 找到初始位置的黑色瓷砖的坐标 if (tiles[i][j] == START_TILE) { startX = j; startY = i; } } } // 使用广度优先搜索算法计算能到达的黑色瓷砖数量 int count = bfs(tiles, startX, startY); // 输出结果 cout << count << endl; } return 0; } ``` 这段代码使用了广度优先搜索(BFS算法来遍历相邻的黑色瓷砖,并统计能够到达的黑色瓷砖数量。注意,这只是一个示例实现,具体的实现方式可能因问题要求的细节而有所不同。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值