#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int dx[5] = {0, -1, 0, 1, 0};
int dy[5] = {0, 0, 1, 0, -1};
int n, a[35][35];
bool vis[35][35];
int read()
{
int sum = 0, w = 1;
char ch = getchar();
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) sum = (sum << 3) + (sum << 1) + ch - '0', ch = getchar();
return sum * w;
}
bool check(int x, int y, bool f)
{
if (a[x][y] == 1) return 0;
for (int i = 1; i <= 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 0 || ty < 0 || tx > n || ty > n) return 0;
if (!vis[tx][ty] && a[tx][ty] == 0)
{
vis[tx][ty] = 1;
return check(tx, ty, 1);
vis[tx][ty] = 0;
}
}
if (!f) return 1;
}
void fi(int x, int y)
{
a[x][y] = 2;
for (int i = 1; i <= 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (a[tx][ty] == 0)
fi(tx, ty);
}
}
int main()
{
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (check(i, j, 0))
{
fi(i, j);
for (int k = 1; k <= n; k++)
{
for (int m = 1; m <= n; m++)
cout << a[k][m] << " ";
cout << endl;
}
exit(0);
}
return 0;
}
调试了半个小时还是没有发现什么错误,请求dalao们帮助