POJ - 2676 Sudoku 题解
2016-03-22 19:47
375 查看
Sudoku
Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSubmit Status
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
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
题意简述:给定一个数独,求解,如果有多组解,输出任意一组即可
题解:dfs即可,但是要做预处理,博主方法还是很好的
我们把数独分成3*3的9块,编号1—9
re_fang[i][j]=k 代表位置(i,j)所在方块(其实可以用方法直接计算
re_fang[i][j]=i*3+(j-1)/3+1)
lie[i][j]=1/0 代表第i列有没有出现k
hang[i][j]=k 同理
不要忘了初始化就行
代码如下:
#include<iostream> #include<stdio.h> #include<string.h> #include<queue> #include<vector> #include<math.h> using namespace std; int re_fang[10][10]={0,0,0,0,0,0,0,0,0,0, 0,1,1,1,2,2,2,3,3,3, 0,1,1,1,2,2,2,3,3,3, 0,1,1,1,2,2,2,3,3,3, 0,4,4,4,5,5,5,6,6,6, 0,4,4,4,5,5,5,6,6,6, 0,4,4,4,5,5,5,6,6,6, 0,7,7,7,8,8,8,9,9,9, 0,7,7,7,8,8,8,9,9,9, 0,7,7,7,8,8,8,9,9,9,}; bool fang[12][12]; bool hang[12][12]; bool lie[12][12]; int arr[12][12]; int f=0; int dfs(int x,int y) { if (f==1) return 0; if (x==10) { f=1; for (int i=1;i<=9;i++) { for (int j=1;j<=9;j++) cout<<arr[i][j]; cout<<endl; } } if (arr[x][y]!=0) { if (y==9) dfs(x+1,1); else dfs(x,y+1); return 0; } for (int i=1;i<=9;i++) if (fang[re_fang[x][y]][i]==0 && lie[y][i]==0 && hang[x][i]==0) { fang[re_fang[x][y]][i]=1; lie[y][i]=1; hang[x][i]=1; arr[x][y]=i; if (y==9) dfs(x+1,1); else dfs(x,y+1); fang[re_fang[x][y]][i]=0; lie[y][i]=0; hang[x][i]=0; arr[x][y]=0; } } int main() { int t; scanf("%d",&t); char cc; while(t--) { memset(arr,0,sizeof(arr)); memset(fang,0,sizeof(fang)); memset(lie,0,sizeof(lie)); memset(hang,0,sizeof(hang)); for (int i=1;i<=9;i++) for (int j=1;j<=9;j++) { cin>>cc; arr[i][j]=cc-'0'; if (arr[i][j]!=0) { fang[re_fang[i][j]][arr[i][j]]=1; lie[j][arr[i][j]]=1; hang[i][arr[i][j]]=1; } } f=0; dfs(1,1); } } 转载请点赞,谢谢 --------
相关文章推荐
- Exactly-once Spark Streaming from Apache Kafka
- mongodb 备份 mongodump
- python 实现javascript加密思路[转载]
- [转]WampServer localhost 图标不显示解决办法
- 机房收费系统之—如何查询两个日期之间的数据
- eclipse导出jar包的笔记
- [置顶] EventBus代码解读
- 3.5.Android控件架构与自定义控件详解之自定义View(二)
- 第三方框架Xutils3的简单使用
- 第四周项目5————用递归方法求解
- Android中找不到png文件错误
- 《世界是数字的》读书笔记
- leetcode206 Reverse Linked List
- 【转载】iPhone屏幕尺寸、分辨率及适配
- java对String进行MD5加密
- selenium 学习笔记
- 初来乍到
- android开发实现七牛云传输文件
- 学习进度表
- 解决Android Studio的ADB not responding错误