Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int board[9][9];
- int UpDown[9][10];
- int LeRigh[9][10];
- int block[3][3][10];
- int ans[9][9];
- bool found = false;
- void cal(int I,int J){
- if(found)return;
- if(J == 9){
- cal(I+1,0);
- return;
- }
- if(board[I][J] != 0){
- cal(I,J+1);
- return;
- }
- if(I == 9){
- for(int i = 0 ; i < 9 ; i ++){
- for(int j = 0 ; j < 9 ; j ++){
- ans[i][j] = board[i][j];
- }
- }
- found = true;
- return;
- }
- for(int num = 1 ; num <= 9 ; num ++){
- if(UpDown[I][num])continue;
- if(LeRigh[J][num])continue;
- if(block[I/3][J/3][num])continue;
- UpDown[I][num] = true;
- LeRigh[J][num] = true;
- block[I/3][J/3][num] = true;
- board[I][J] = num;
- cal(I,J+1);
- UpDown[I][num] = false;
- LeRigh[J][num] = false;
- block[I/3][J/3][num] = false;
- board[I][J] = 0;
- }
- }
- int main()
- {
- // freopen("input_sudoku.txt","r",stdin);
- for(int t = 1 ; t <= 100 ; t ++){
- char str[7];int b;
- scanf(" %s %d",str,&b);
- int cnt = 0 ;
- for(int i = 0 ; i < 9 ; i ++){
- for(int j = 0 ; j < 9 ; j ++){
- scanf("%d",&board[i][j]);
- UpDown[i][board[i][j]] = true;
- LeRigh[j][board[i][j]] = true;
- block[(i/3)][(j/3)][board[i][j]] = true;
- }
- }
- found = false;
- cal(0,0);
- // freopen("output_sudoku.txt","w",stdout);
- printf("%s %d\n",str,b);
- for(int i = 0 ; i < 9 ; i ++){
- for(int j = 0 ; j < 9 ; j ++){
- printf("%d ",ans[i][j]);
- }
- printf("\n");
- }
- for(int i = 0 ; i < 9 ; i ++)
- {
- for(int j = 0 ; j < 10 ; j ++)
- {
- UpDown[i][j] = false;
- LeRigh[i][j] = false;
- }
- }
- for(int i = 0 ; i < 3 ; i ++)
- {
- for(int j = 0 ; j < 3 ; j ++)
- {
- for(int k = 0 ; k < 10 ; k ++)
- {
- block[i][j][k] = false;
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement