【POJ 2676】Sudoku数独(DFS)
来源:互联网 发布:淘宝镁光内存条 编辑:程序博客网 时间:2024/05/21 12:51
Sudoku
Time Limit: 2000MS Memory Limit: 65536K
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
思路:DFS遍历解数独,很有意思的题目,由于行、列、每一个3*3小方格都不能重复,故需要三个数组row,col,dig来标记某1~9数字是否出现。DFS遍历,具体思路见注释。
代码:
#include<iostream> #include<cstring>using namespace std;int Map[10][10]; //未填充的九宫格 bool row[10][10]; //row[i][x]标记第i行数字x是否出现 bool col[10][10]; //col[j][x]标记第j列数字x是否出现 bool dig[10][10]; //dig[k][x]标记一个3*3格子中 ,第k个位置数字x是否出现bool DFS(int x,int y){if(x==10) return true; //表示遍历完九宫格 bool flag=false;//九宫格某格已有数字,进行下列操作 if(Map[x][y]) {if(y==9) flag= DFS(x+1,1); //从下一行第一列开始 else flag= DFS(x,y+1); //依次下一列 //回溯 if(flag) return true;else return false; } //九宫格某格没有数字(待填) else { int k=3*((x-1)/3)+(y-1)/3+1;//将3*3格子依次(横->竖)从1~9编号 for(int i=1;i<=9;i++){ //从1~9遍历填数 if(!row[x][i]&&!col[y][i]&&!dig[k][i]) //此列,此行,此3*3格子中都没有出现过此数 { //填入并将行、列、格子标记 Map[x][y]=i; row[x][i]=true; col[y][i]=true; dig[k][i]=true; if(y==9) flag= DFS(x+1,1); //从下一行第一列开始else flag= DFS(x,y+1); //依次下一列 if(!flag) { //填数失败,回溯,循环继续填 Map[x][y]=0;row[x][i]=false; col[y][i]=false; dig[k][i]=false;}else return true; } } } return false;}int main(){int n;cin>>n;while(n--){memset(row,false,sizeof(row));memset(col,false,sizeof(col));memset(dig,false,sizeof(dig));//将三个数组初始化 char x;for(int i=1;i<=9;i++){for(int j=1;j<=9;j++){cin>>x;Map[i][j]=x-'0';//由于输入只能按字符读入if(Map[i][j]) {int k=3*((i-1)/3)+(j-1)/3+1;//将3*3格子依次(横->竖)从1~9编号 row[i][Map[i][j]]=Map[i][j];col[j][Map[i][j]]=Map[i][j];dig[k][Map[i][j]]=Map[i][j];} }}DFS(1,1);for(int i=1;i<=9;i++){for(int j=1;j<=9;j++)cout<<Map[i][j];cout<<endl; } }return 0;}
阅读全文
0 0
- POJ 2676-Sudoku(DFS-数独)
- 【POJ 2676】Sudoku数独(DFS)
- POJ 2676 Sudoku (数独 DFS)
- [DFS]poj 2676 Sudoku 数独问题
- POJ 2676 Sudoku (数独求解器 DFS)
- POJ 2676 Sudoku (数独 搜索)
- POJ 2676Sudoku(数独)
- poj 2676 Sudoku(数独)
- POJ 2676 Sudoku 数独
- POJ 2676 Sudoku (数独)
- 数独 Sudoku poj 2676
- POJ 2676:Sudoku 数独
- hdu 3111 Sudoku(DFS数独)
- poj 2676 dfs/DLX(数独)
- POJ 2676/2918 数独(dfs)
- (水)POJ-2676 DFS,数独
- POj 2676 数独 (dfs)
- poj 2676 Sudoku(dfs填数)
- ViewPager滑动切换界面
- JAVA流程控制 循环结构
- python opencv入门 形态学转换(13)
- 【VS开发】 Windows平台下管道的使用
- error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(
- 【POJ 2676】Sudoku数独(DFS)
- 不可以
- Retrofit详解
- 面向切面的Spring
- jQuery的属性与样式之.val()
- Kotlin Reference (十五) 函数和lambda表达式:普通函数和高阶函数
- 负载均衡详解
- Spring之创建切点,切面
- ORACLE触发器学习