POJ 2676 dfs
2015-11-17 16:25
211 查看
Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16403 Accepted: 8013 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
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
Source
Southeastern Europe 2005
题意:
给一个数独题,然后用程序填空,如果无法完成(完成要求横行,竖行,每个小九宫格内都没有重复数字)则输出原来的数独题;
解法:
搜索回溯就可以过。用了一个小剪枝就是如果已经找到一个可行解就立马结束,递归返回时其余的枝丫全部结束返回。
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16403 Accepted: 8013 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
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
Source
Southeastern Europe 2005
题意:
给一个数独题,然后用程序填空,如果无法完成(完成要求横行,竖行,每个小九宫格内都没有重复数字)则输出原来的数独题;
解法:
搜索回溯就可以过。用了一个小剪枝就是如果已经找到一个可行解就立马结束,递归返回时其余的枝丫全部结束返回。
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef struct{ int st,en; }Po; char G[12][12]; Po box[] = {{0,2},{3,5},{6,8}}; Po Get(void){ int i,j,ok = 1; Po tem; for(i = 0;i<9;i++) for(j = 0;j<9;j++){ if('0' == G[i][j]){ tem.st = i; tem.en= j; return tem; } } tem.st = tem.en = -1; return tem; } int dfs(int x,int y){ int a[12] = {0}; int i,j; for(i = 0;i<9;i++){ a[G[x][i] - '0'] = 1; a[G[i][y] - '0'] = 1; } int xx = x/3; int yy = y/3; for(i = box[xx].st;i<=box[xx].en;i++) for(j = box[yy].st;j<=box[yy].en;j++) a[G[i][j] - '0'] = 1; for(i = 1;i<=9;i++){ if(a[i] == 0){ G[x][y] = '0' + i; Po tem = Get(); if(tem.st == -1&&tem.en == -1) return 1; if(dfs(tem.st,tem.en)) return 1; G[x][y] = '0'; } } return 0; } int main(){ // freopen("data.in","r",stdin); int N; scanf("%d",&N); getchar(); while(N--){ int i,j; for(i = 0;i<9;i++){ scanf("%s",G[i]); getchar(); } Po S = Get(); dfs(S.st,S.en); for(i = 0;i<9;i++) printf("%s\n",G[i]); } return 0; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链