POJ 2676 Sudoku

来源:互联网 发布:小班防火知多少课件 编辑:程序博客网 时间:2024/06/05 00:46

                                                                      Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 20638 Accepted: 9873 Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127
题目大意就是给你一个数独,让你写。

这道题很有意思,毕竟我就很爱玩数独~,数独的规则就是每一行每一列不能出现同样的数字,每一个小的九宫格里面也不能出现同样的数字。这道题目用深搜写,搜一下每一行每一列每一个小九宫格就行了。听很多大佬说倒着搜只要十几ms,有空再试试吧,大佬们真是太强了

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int map[10][10];int row[10][10],column[10][10],square[10][10],count=0;struct Node{int x,y;};Node zero[100];int computer(int x,int y){//算3*3小格子 return (((x-1)/3)*3+(y-1)/3)+1;}int dfs(int num){if(num>count) return 1;for(int i=1;i<=9;i++){if(!row[zero[num].x][i]&&!column[zero[num].y][i]&&!square[computer(zero[num].x,zero[num].y)][i]){row[zero[num].x][i]=1;column[zero[num].y][i]=1;square[computer(zero[num].x,zero[num].y)][i]=1;map[zero[num].x][zero[num].y]=i;if(dfs(num+1)) return 1;row[zero[num].x][i]=0;column[zero[num].y][i]=0;square[computer(zero[num].x,zero[num].y)][i]=0;}}return 0;}int main(){int t;scanf("%d",&t);while(t--){count=0;memset(row,0,sizeof(row));memset(column,0,sizeof(column));memset(square,0,sizeof(square));for(int i=1;i<=9;i++){for(int j=1;j<=9;j++){scanf("%1d",&map[i][j]);if(map[i][j]){row[i][map[i][j]]=1;column[j][map[i][j]]=1;square[computer(i,j)][map[i][j]]=1;}else{count++;zero[count].x=i;zero[count].y=j;}}}dfs(1);for(int i=1;i<=9;i++){for(int j=1;j<=9;j++){cout<<map[i][j];}cout<<endl;}}}


原创粉丝点击