题目链接 点我
题意:数独,用1 - 4补全4*4的格子, 格子里面的字符*代表空。
思路:DFS暴力就可以了。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <map> #include <string> #include <vector> #define lson o<<1|1, l, mid #define rson o<<1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define INF 0x3f3f3f3f #define eps 1e-8 #define debug printf("1\n") #define MAXN 10000 #define MAXM 100000 #define LL long long #define CLE(a, b) memset(a, (b), sizeof(a)) #define W(a) while(a--) #define Ri(a) scanf("%d", &a) #define Pi(a) printf("%d\n", (a)) #define Rl(a) scanf("%lld", &a) #define Pl(a) printf("%lld\n", (a)) #define Ss(a) scanf("%s", a) #define Ps(a) printf("%s\n", a) using namespace std; int Map[5][5]; bool judge(int x, int y, int val) { for(int i = 0; i < 4; i++) if(Map[x][i] == val) return false; for(int i = 0; i < 4; i++) if(Map[i][y] == val) return false; int row, cul; row = x / 2 * 2; cul = y / 2 * 2; for(int i = row; i < row+2; i++) for(int j = cul; j < cul+2; j++) if(Map[i][j] == val) return false; return true; } void DFS(int x, int y) { if(x == 4) { for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) printf("%d", Map[i][j]); printf("\n"); } } else if(y == 4) DFS(x+1, 0); else if(Map[x][y]) DFS(x, y+1); else { for(int i = 1; i <= 4; i++) { if(judge(x, y, i)) { Map[x][y] = i; DFS(x, y+1); Map[x][y] = 0; } } } } char str[5]; int main() { int t, kcase = 1; scanf("%d", &t); W(t) { getchar(); for(int i = 0; i < 4; i++) { Ss(str); for(int j = 0; j < 4; j++) { if(str[j] == '*') Map[i][j] = 0; else Map[i][j] = str[j] - '0'; } } printf("Case #%d:\n", kcase++); DFS(0, 0); } return 0; }