AcWing 1613. 数独简单版
原题链接
简单
作者:
_iNinni
,
2024-03-04 23:59:55
,
所有人可见
,
阅读 16
#include <bits/stdc++.h>
using namespace std;
char s[11][11];
int h[11][11], l[11][11], g[5][5][11];
int dfs(int x, int y) {
if (y == 9) x++, y = 0;
if (x == 9) {
for (int i = 0; i < 9; i++) cout << s[i] << endl;
return 1;
}
if (s[x][y] != '.') return dfs(x, y + 1);
for (int i = 1;i <= 9; i++) {
if (!h[x][i] && !l[y][i] && !g[x / 3][y / 3][i])
{
h[x][i] = l[y][i] = g[x / 3][y / 3][i] = 1;
s[x][y] = i + '0';
if (dfs(x, y + 1)) return 1;
h[x][i] = l[y][i] = g[x / 3][y / 3][i] = 0;
s[x][y] = '.';
}
}
return 0;
}
int main() {
for (int i = 0;i < 9; i++) {
cin >> s[i];
for (int j = 0;j < 9; j++) {
if (s[i][j] != '.')
{
int x = s[i][j] - '0';
h[i][x] = l[j][x] = g[i / 3][j / 3][x] = 1;
}
}
}
dfs(0, 0);
return 0;
}