Link: Here!
Ideas: Record all "?", Where they appear, and then $ DFS $ bit, for each position can be filled for $ 9 $ kinds of values, then determine whether to fill a legitimate need for the three marker array to assist record. $ VisR [i ] [num] = 1, i-th row num value has occurred, visC [i] [num] = 1, i-th column of the num value has appeared, visB [i] [num] = 1, represents the i tile has appeared $ num value calculation requires only small numbers of $ x / 3 * 3 + y / 3 $ to.
Analogy: Similarly entitled hihocoder 1321, But doing so will T, so hihocoder needDancing links
Note: This question reads very sick, it is best to check at the finish after the read operation. Also note that if the value has been given (that is, non '?'), You need to mark it.
/*************************************************************************
> File Name: 1426-Sudoku-Killer.cpp
> Author:
> Mail:
> Created Time: 2017 Nian 11 Wednesday, 29 October 17:16:39
************************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 20
struct Point {
int x, y;
} pt[110];
int node_cnt = 0;
int G[MAX_N][MAX_N];
int visR[MAX_N][MAX_N] = {0};
int visC[MAX_N][MAX_N] = {0};
int visB[MAX_N][MAX_N] = {0};
int ok = 0;
void clear() {
node_cnt = 0;
ok = 0;
memset(G, 0, sizeof(G));
memset(visR, 0, sizeof(visR));
memset(visC, 0, sizeof(visC));
memset(visB, 0, sizeof(visB));
}
int cal_block(int x, int y) {
return (x / 3 * 3 + y / 3);
}
bool check(int x, int y, int block, int num) {
return !(visR[x][num] || visC[y][num] || visB[block][num]);
}
void set_pt(int x, int y, int block, int num) {
visR[x][num] = visC[y][num] = visB[block][num] = 1;
}
void move_pt(int x, int y, int block, int num) {
visR[x][num] = visC[y][num] = visB[block][num] = 0;
}
void dfs(int step) {
if (ok) return;
if (step == node_cnt) {
for (int i = 0 ; i < 9 ; ++i) {
for (int j = 0 ; j < 8 ; ++j) {
printf("%d ", G[i][j]);
}
printf("%d\n", G[i][8]);
}
ok = 1;
return;
}
for (int i = 1 ; i <= 9 ; ++i) {
int x = pt[step].x;
int y = pt[step].y;
int block = cal_block(x, y);
if (!check(x, y, block, i)) continue;
set_pt(x, y, block, i);
int t_val = G[x][y];
G[x][y] = i;
dfs(step + 1);
G[x][y] = t_val;
move_pt(x, y, block, i);
}
return;
}
int main() {
int kase = 0;
char ch;
while (scanf("%c", &ch) != EOF) {
if (ch == '\n') continue;
if (kase) printf("\n");
++kase;
G[0][0] = (ch == '?' ? 0 : (ch - '0'));
if (G[0][0] == 0) {
pt[node_cnt].x = 0;
pt[node_cnt].y = 0;
++node_cnt;
} else {
set_pt(0, 0, cal_block(0, 0), G[0][0]);
}
for (int i = 1 ; i < 9 ; ++i) {
scanf("%c", &ch);
if (ch == ' ') {
--i; continue;
}
G[0][i] = (ch == '?' ? 0 : (ch - '0'));
if (G[0][i] == 0) {
pt[node_cnt].x = 0;
pt[node_cnt].y = i;
++node_cnt;
} else {
set_pt(0, i, cal_block(0, i), G[0][i]);
}
}
for (int i = 1 ; i < 9 ; ++i) {
getchar();
for (int j = 0 ; j < 9 ; ++j) {
scanf("%c", &ch);
if (ch == ' ') {
--j; continue;
}
G[i][j] = (ch == '?' ? 0 : (ch - '0'));
if (G[i][j] == 0) {
pt[node_cnt].x = i;
pt[node_cnt].y = j;
++node_cnt;
} else {
set_pt(i, j, cal_block(i, j), G[i][j]);
}
}
}
dfs(0);
clear();
}
return 0;
}