建议加强数据

P1162 填涂颜色

Leo_Ye @ 2024-09-29 20:54:52

我的错误代码能AC!

#include <iostream>
using namespace std;
int m[105][105] = {}, n;
int f[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
void bfs(int x, int y) {
    int a[10005][3] = {};
    int head = 0, tail = 0;
    a[tail][0] = x;
    a[tail][1] = y;
    m[x][y] = 2;
    tail++;
    while (head < tail) {
        for (int i = 0; i < 4; i++) 
        {
            int xx = f[i][0] + a[head][0], yy = f[i][1] + a[head][1];
            if (m[xx][yy] == 0 && xx >= 1 && xx <= n && yy >= 1 && yy <= n) 
            {
                a[tail][0] = xx;
                a[tail][1] = yy;
                tail++;
                m[xx][yy] = 2;
            }

        }
        head++;
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= n; j++) 
        {
            cin >> m[i][j];
        }
    }
    bool b = true, c = false, d = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++) 
        {
            if (m[i][j] == 1 && b)
                b = 0;
            else if (m[i][j] == 0 && b == 0) {
                for (int k = j; k <= n; k++) 
                {
                    if (m[i][k] == 1)
                        d = 1;
                }
                if (d && i > 1) 
                {
                    bfs(i, j);
                    c = true;
                    break;
                } 
            else continue;
            }
        }
        if (c)
            break;
        else 
            b = 1;
    }
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= n; j++) 
        {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

|