奇异错误求助

P1162 填涂颜色

Midnight_szx @ 2023-01-09 21:11:57

一直挂第四个点,请大佬指正错误

我也不知道为什么非要写BFS折磨自己qwq

#include<iostream>
#include<queue>
using namespace std;
int n, map[35][35];
bool vis[35][35];
int to[5][2] = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct node {
    int x, y;
};
queue<node> q;
inline void bfs(int a, int b) {
    node t = {a, b};
    q.push(t);
    vis[a][b] = 1;
    while(!q.empty()) {
        node k = q.front();
        int xx = k.x, yy = k.y;
        q.pop();
        for(int i = 1; i <= 4; i++) {
            int kx = xx + to[i][0], ky = yy + to[i][1];
            if(!vis[kx][ky] and kx > 0 and kx <= n and ky > 0 and ky <= n) {
                vis[kx][ky] = 1;
                node tmp = {kx, ky};
                q.push(tmp);
            }
        }
    }
}
int main() {
    cin>>n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin>>map[i][j];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(map[i][j] == 1)
                vis[i][j] = 1;
    for(int i = 1; i <= n; i++)
        if(map[i][1] == 0 and !vis[i][1])
            bfs(i, 1);
    for(int i = 1; i <= n; i++)
        if(map[i][n] == 0 and !vis[i][n])
            bfs(i, n);
    for(int i = 1; i <= n; i++)
        if(map[1][i] == 0 and !vis[1][i])
            bfs(1, i);
    for(int i = 1; i <= n; i++)
        if(map[1][n] == 0 and !vis[1][n])
            bfs(n, i);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++) {
            if(!vis[i][j] and map[i][j] == 0)
                map[i][j] = 2;
        }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++)
            cout<<map[i][j]<<" ";
        cout<<'\n';
    }
    return 0;
}

by Midnight_szx @ 2023-01-09 21:38:36

@OIer_zhez 但怎么调呢


by MinCat @ 2023-01-09 21:38:37

建议您以四条边界为起始,对没有被围着的点进行标记。


by _Glassy_Sky_ @ 2023-01-09 21:38:51

@OIer_zhez 我看不出来,有密集恐惧症


by Midnight_szx @ 2023-01-09 21:39:02

i hate bfs forever


by Midnight_szx @ 2023-01-09 21:39:32

@OIer_zhez 确实是这么标记的


by MinCat @ 2023-01-09 21:39:54

@Midnight_szx 用染色法


by MinCat @ 2023-01-09 21:41:45

润了,您再调调,明天您去找廖子吧,他更牛


上一页 |