社区讨论

70分TLE求调 QwQ

P1256显示图像参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhjnywmb
此快照首次捕获于
2025/11/04 05:40
4 个月前
此快照最后确认于
2025/11/04 05:40
4 个月前
查看原帖
CPP
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, m;
bool vis[185][185];
int ans[185][185];
char map[185][185];
struct node {
    int x, y;
};
void bfs(int x, int y) {
    queue<node> q;
    q.push({x, y});
    int dx[] = {0, -1, 0, 1};
    int dy[] = {-1, 0, 1, 0};
    while (!q.empty()) {
        node now = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int tx = now.x + dx[i];
            int ty = now.y + dy[i];
            if (!vis[tx][ty] && tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
                vis[tx][ty] = true;
                ans[tx][ty] = min(ans[tx][ty], ans[now.x][now.y] + 1);
                q.push({tx, ty});
            }
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> map[i][j];
            ans[i][j] = (map[i][j] == '0' ? 1e9 : 0);
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (map[i][j] == '1') {
                bfs(i, j);
                memset(vis, false, sizeof vis);
            }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
            cout << ans[i][j] << " ";
        cout << endl;
    }
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...