Sudoku poj2676 (dfs)

来源:互联网 发布:软件测试包括哪些 编辑:程序博客网 时间:2024/05/16 09:55

Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 19520 Accepted: 9359 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


数独问题 ---

利用了step来代替了传统的四个方向遍历后dfs

简洁 但是转化稍微麻烦一点

详见代码


#include <algorithm>#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>using namespace std;#define For(i,a,b) for(i=a;i<=b;i++)#define _For(i,a,b) for(i=b;i>=a;i--)#define Out(x) cout<<x<<endl#define mset(arr,num) memset(arr,num,sizeof(arr))#define ll long longconst ll inf = 2000; ///#define ok std::ios::sync_with_stdio(0)#ifndef SYMBOL#define SYMBOL value#endif#pragma comment(linker, "/STACK:102400000,102400000")// #define debug#if defined (debug)---check---#endif///////////////////////////////////////char mp[11][11];int shudo[11][11];bool flag;struct node{int dx,dy;}shudostep[110];  ///81 ~ 1 一格子一格子的来 可以用坐标化来上下左右移动的方法代替 只不过比较麻烦bool check(int x,int y,int num)  ///检查有没有重复{ int i,j,k;For(i,1,9) ///check row {if(shudo[x][i] == num){return 0;}}For(i,1,9) ///check line{if(shudo[i][y] == num){return 0;}}int dx = (x-1)/3+1,dy = (y-1)/3+1; ///利用x y相关运算判断在哪个3*3矩阵dx*=3;dy*=3;For(i,dx-2,dx) ///for 3*3{For(j,dy-2,dy){if(shudo[i][j] == num){return 0;}}}return 1;}void dfs(int step){int i;int x,y;if(step > 81){flag = 1;return ;}x=shudostep[step].dx;y=shudostep[step].dy;if(shudo[x][y] != 0)  ///如果不是零 搜索下一个位置{dfs(step+1);}else{For(i,1,9){if(check(x,y,i))  ///如果数字可以用{shudo[x][y] = i;  ///填入数字dfs(step+1);if(flag){return ;}shudo[x][y] = 0;  ///说明前面不符合 重新置为0}}}}int main(){int i,j,t;cin>>t;ok;while(t--){flag = 0;For(i,1,9){For(j,1,9){cin>>mp[i][j];shudo[i][j] = mp[i][j]-'0';  ///用整型数组存起来}}int cnt = 0;For(i,1,9){For(j,1,9){cnt++;shudostep[cnt].dx=i;shudostep[cnt].dy=j;  ///路径}}dfs(1);for(i=1;i<=9;i++){for(j=1;j<=9;j++){cout<<shudo[i][j];}cout<<endl;}}return 0;}


0 0
原创粉丝点击