#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; char s[10][10]; int panduan(int row,int cew) { for(int i=0;i<4;i++) { if(s[row][i]==s[row][cew]&&i!=cew) return 0; } for(int j=0;j<4;j++) { if(s[j][cew]==s[row][cew]&&j!=row) return 0; } int mrow=row; int mcew=cew; if(row%2==1) row--; if(cew%2==1) cew--; for(int i=row;i<row+2;i++) { for(int j=cew;j<cew+2;j++) { if(s[i][j]==s[mrow][mcew]&&i!=mrow&&j!=mcew) return 0; } } return 1; } void dfs(int step) { if(step==16) { for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { printf("%c",s[i][j]); } printf("\n"); } } int row=step/4; int cew=step%4; if(s[row][cew]==‘*‘) { for(int j=1;j<=4;j++) { s[row][cew]=j+‘0‘; if(panduan(row,cew)) dfs(step+1); s[row][cew]=‘*‘; } } else dfs(step+1); } int main() { int cas=0; int t; cin>>t; while(t--) { for(int i=0;i<4;i++) scanf("%s",s[i]); printf("Case #%d:\n",++cas); dfs(0); } return 0; }
Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller.
Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×44×4 board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four 2×22×2 pieces, every piece contains 1 to 4.
Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.
Actually, you are seeing this because you‘ve passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
InputThe first line of the input gives the number of test cases, T(1≤T≤100)T(1≤T≤100). TT test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of ‘1‘, ‘2‘, ‘3‘, ‘4‘). ‘*‘ represents that number was removed by Yi Sima.
It‘s guaranteed that there will be exactly one way to recover the board.OutputFor each test case, output one line containing
Case #x:, where xx is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.Sample Input
3 **** 2341 4123 3214 *243 *312 *421 题目:B - Sudoku 思路:这个题目其实就是一个小一点的数独,因为很小,所以可以用枚举去搜索,完全不用担心会超时。方法很简单就是枚举每一个*位置为1,2,3,4;然后再回溯。具体:再main函数里面读入,然后进入搜索函数dfs,有一个step,如果step==16就结束了根据step可以判断出行列,然后搜这个位置,如果是*就枚举,否则就step++,进入下一个dfs注意要写一个数独的判断函数。
原文地址:https://www.cnblogs.com/EchoZQN/p/10337999.html