每日一题(1): poj2676: Sudoku
来源:互联网 发布:贵阳广电网络客服电话 编辑:程序博客网 时间:2024/05/17 08:23
1. 题目的意思很简单。但是原来不知道怎么下手进行深搜。后来看到点击打开链接的思路,使用三个bool二维数组进行标记,才茅塞对开。在此表示感谢。推荐看看。
2. 其实就是怎么判断的问题,因为搜索还是从头开始,按照坐标一步一步,就是判断原来觉得难。但是三个二维数组分别是行,列,小方格的判断,第一个下标就是行,列,和根据行列求出的方格序数。第二个下标就是1-9的数字。感觉一下子就解决。
3. 还有一个问题就是k和i,j的关系。上面那篇文章也讲得很好。就不写了。可以看看或者自己推一些。
4. 这是自己开始用C++做题的第一题。以后就可能不用java了。因为java比较慢,而且网上资源比较少。当然,也有java封装太好的原因。不过个人感觉主要是前两点原因。
下面是自己的代码:
#include <iostream>#include<stdio.h>#include<string.h>using namespace std;int map[10][10];//整个数组bool col[10][10];//依次是列,行,小方格的标记数组bool row[10][10];bool map2[10][10];//深搜bool dfs(int x, int y){//结束条件 if(x>=10||y>=10) { return true; } //下一步搜索坐标 int nexty = y+1; int nextx = x; if(y+1==10) { nexty = 1; nextx = x+1; } //根据此坐标的map值来确定是否略过。因为本来就有的值是不需要填的。 if(map[x][y]==0) { //需要填值,就遍历剪枝 for(int i = 1; i<10; i++) { int k = ((x-1)/3)*3+(y-1)/3+1; if(col[y][i]&&row[x][i]&&map2[k][i]) { map[x][y]=i; col[y][i]=false; row[x][i]=false; map2[k][i]=false; if(dfs(nextx,nexty)) return true; map[x][y]=0; col[y][i]=true; row[x][i]=true; map2[k][i]=true; } } }else { if(dfs(nextx,nexty)) return true; } return false;}int main(){ int t; //freopen("sudoku.txt", "r", stdin); cin >>t; while(t-->0) { char a; memset(col,true,sizeof(col)); memset(row,true,sizeof(row)); memset(map2,true,sizeof(map2)); char MAP[10][10]; for(int i = 1; i<10; i++) { for(int j = 1; j < 10; j++) { cin>>a; int b = a-'0'; map[i][j]=b; row[i][b] = false; col[j][b] = false; int k = ((i-1)/3)*3+(j-1)/3+1; map2[k][b] = false; } } dfs(1,1); for(int i = 1; i < 10; i++) { for(int j =1; j < 10; j++) { cout<<map[i][j]; } cout<<endl; } cout<<endl; }}
0 0
- 每日一题(1): poj2676: Sudoku
- POJ2676:Sudoku(DFS)
- POJ2676 - Sudoku (深搜)
- Sudoku poj2676 (dfs)
- POJ2676--Sudoku(搜索)
- POJ2676 Sudoku(dfs)
- poj2676(数独 sudoku)
- POJ2676-Sudoku(数独)
- poj2676(Sudoku)
- poj2676 - Sudoku
- POJ2676--Sudoku
- poj2676 Sudoku
- poj2676 Sudoku
- POJ2676 Sudoku
- POJ2676 Sudoku
- poj2676 Sudoku
- poj2676 Sudoku
- POJ2676-Sudoku
- 解决连接mongoDB出现问题:MongoDB : Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
- typedef int datatype 的用意
- JAVA常用设计模式(1)单例模式
- firstDay
- leetCode练习(103)
- 每日一题(1): poj2676: Sudoku
- Unity中json的使用方法
- java匿名内部类
- Uva540 Team Queue 【队列】【例题5-6】
- 操纵杆编程
- 机器学习笔记(十八)——HMM的参数估计
- adb进行android 调试(mac)
- 解决LPC1343在Protues8.4不能仿真问题:ode U1_GND not found whilst binding pin V- of U1_U5_R1_ADC#0000!
- LNMP安装过程