报错了www大佬们看看我的

P1162 填涂颜色

zzt444855895 @ 2022-12-01 14:58:03

大佬们能不能告诉我为啥我的代码dfs函数运行报错啊

#include <stdio.h>
int a[30][30], flag, n;
void dfs(int x, int y)
{
    if (x == 0 || y == 0 || x == n - 1 || y == n - 1)
    {
        flag = -1;
    }
    else
    {
        if (a[x - 1][y] != 1) dfs(x - 1, y);
        if (a[x + 1][y] != 1) dfs(x + 1, y);
        if (a[x][y + 1] != 1) dfs(x, y + 1);
        if (a[x][y - 1] != 1) dfs(x, y - 1);
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] != 1)
            {
                a[i][j] = 2;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] != 1)
            {
                flag = 0;
                dfs(i, j);
                if (flag == -1)
                {
                    a[i][j] = 0;
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d", a[i][j]);
        }
        printf("\n");
    }
}

by DYYqwq @ 2022-12-01 15:17:48

@zzt444855895 我这里没问题呀,成功编译,你要不再试试?(


by DYYqwq @ 2022-12-01 15:18:59

(但是这个程序有很大问题


by zzt444855895 @ 2022-12-02 15:32:06

@DongYUyao 我解决了,dfs里面无限循环了


|