想做一个自动填数独的程序,已经写好C++程序了,哪位大佬能帮忙改成Scr的
#include <bits/stdc++.h>
using namespace std;
int a[21][21] = {0};
int n = 0;
int nulx[101] = {0};
int nuly[101] = {0};
bool p1[21][21] = {0};
bool p2[21][21] = {0};
bool p3[11][11][21] = {0};
int get(int);
void scan();
void save();
void solve(int);
void print();
void scan() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
scanf("%d", &a[i][j]);
if (a[i][j] == 0) {
n++;
nulx[n] = i;
nuly[n] = j;
}
}
}
}
void save() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
p1[i][a[i][j]] = 1;
}
}
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= 9; i++) {
p2[j][a[i][j]] = 1;
}
}
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
for (int x = 1; x <= 3; x++) {
for (int y = 1; y <= 3; y++) {
p3[i][j][a[x + 3 * (i - 1)][y + 3 * (j - 1)]] = 1;
}
}
}
}
}
int get(int k) {
if (k <= 3) return 1;
else if (k <= 6) return 2;
else return 3;
}
void print() {
printf("Answer:\n");
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return ;
}
void solve(int k) {
if (k == n + 1) print();
else for (int i = 1; i <= 9; i++) {
if (!p1[nulx[k]][i] && !p2[nuly[k]][i] && !p3[get(nulx[k])][get(nuly[k])][i]) {
a[nulx[k]][nuly[k]] = i;
p1[nulx[k]][i] = 1;
p2[nuly[k]][i] = 1;
p3[get(nulx[k])][get(nuly[k])][i] = 1;
solve(k + 1);
a[nulx[k]][nuly[k]] = 0;
p1[nulx[k]][i] = 0;
p2[nuly[k]][i] = 0;
p3[get(nulx[k])][get(nuly[k])][i] = 0;
}
}
}
int main() {
scan();
save();
solve(1);
system("pause");
return 0;
}
(不想整理了,应该能跑)
已有账号? 立即登录
内容和封面无关!无关!