poj 2676 深搜之sudo
2016-03-09 21:42
316 查看
Sudoku
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
Sample Output
输入:char输入;
输入完成后:数字保存到board中,空位记录下来;
然后对所有空位进行深搜,搜索中注意每次搜索失败后的清零
//////////////////////////////////////////
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 16919 | Accepted: 8242 | Special Judge |
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
输入:char输入;
输入完成后:数字保存到board中,空位记录下来;
然后对所有空位进行深搜,搜索中注意每次搜索失败后的清零
//////////////////////////////////////////
<span style="font-size:14px;color:#6666cc;background-color: rgb(204, 204, 204);">#include <vector> #include <algorithm> #include <cstdio> #include <iostream> #include <cstring> using namespace std; int hang[9][10]; int lie[9][10]; int block[9][10]; int board[9][10]; struct pos{ int a,b; pos(int aa,int bb):a(aa),b(bb){ } };//类的构造函数 vector<pos> blankpos; int countblock(int i,int j) { int re; re = (int)(i/3)*3+j/3; return re; } void setall(int i,int j,int num,int n) { if(n) { board[i][j] = num; } else { board[i][j] = 0; } hang[i][num] = n; lie[j][num] = n; block[countblock(i,j)][num] = n; } bool ok(int i,int j,int num) { if(hang[i][num]!=1) { if(lie[j][num]!=1) { if(block[countblock(i,j)][num]!=1) return true; } } return false; } bool dfs(int n) { if(n<0) return true; int i = blankpos .a; int j = blankpos .b; for(int k = 1; k < 10; k++) { if(ok(i,j,k)) { setall(i,j,k,1); if(dfs(n-1)) return true; else setall(i,j,k,0); } } return false; } int main() { int n; cin >> n; while(n--) { memset(hang,0,sizeof(hang)); memset(lie,0,sizeof(lie)); memset(block,0,sizeof(block)); blankpos.clear(); // for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { char c; int d; cin >> c; d=(int)(c-'0');//输入字符转化为整数储存 if(d == 0) { blankpos.push_back(pos(i,j));//注意返回值的用法 } else { setall(i,j,d,1); } } } if(dfs(blankpos.size()-1)) { for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { cout << board[i][j]; } cout << endl; } } } return 0; }</span>
相关文章推荐
- Ubuntu的sudo与su命令使用与开启root帐户 2012-04-30
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法
- mysql 模糊搜索的方法介绍