Poj 2676
来源:互联网 发布:华云数据面试 编辑:程序博客网 时间:2024/06/02 12:34
Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14480 Accepted: 7153 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
1103000509002109400000704000300502006060000050700803004000401000009205800804000107
Sample Output
143628579572139468986754231391542786468917352725863914237481695619275843854396127
数独;
标记num这个数在第i行,第j列,第k个矩阵中有没有出现,不停地往下Dfs失败了就回溯;
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int Map[10][10];int row[10][10];//标记9行int col[10][10];//标记9列int grid[10][10];//标记9个9宫格int getgrid(int x,int y){ return 3*( x/3 )+(y/3);}int Dfs(int x,int y){ if(x==10) return 1; int flag=0; if(Map[x][y]){ if(y==9) flag=Dfs(x+1,1); else flag=Dfs(x,y+1); if(flag) return 1; else return 0; } int k=getgrid(x-1,y-1); for(int i=1;i<=9;i++){ if(!Map[x][y]&&!col[y][i]&&!row[x][i]&&!grid[k][i]){ Map[x][y]=i; col[y][i]=1; row[x][i]=1; grid[k][i]=1; if(y==9) flag=Dfs(x+1,1); else flag=Dfs(x,y+1); if(!flag){ Map[x][y]=0; col[y][i]=0; row[x][i]=0; grid[k][i]=0; } else return 1; } } return 0;}int main(){ int T; char str[10]; scanf("%d",&T); while(T--){ memset(Map,0,sizeof(Map)); memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); memset(grid,0,sizeof(grid)); for(int i=1;i<=9;i++){ scanf("%*c%s",str); for(int j=0;j<9;j++){ Map[i][j+1]=str[j]-'0'; int x=Map[i][j+1]; if(x){ row[i][x]=1; col[j+1][x]=1; int k=getgrid(i-1,j); grid[k][x]=1; } } } 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
- POJ 2676
- poj-2676
- poj 2676
- poj 2676
- Poj 2676
- poj 2676
- POJ 2676
- POJ 2676
- POJ 2676 && POJ 2918 数独 DFS
- poj 2676 Sudoku
- POJ 2676 Sudoku
- POJ 2676 Sudoku dfs
- POJ 2676 数独
- Poj 2676 Sudoku
- POJ 2676 Sudoku [暴搜]
- POJ 2676 Sudoku
- POJ 2676 Sudoku
- Perl学习笔记(一)文件迁移脚本
- Unity3D架构系列之FSM有限状态机设计(五)
- JAVA io流(2) 节点流和处理流 装饰者模式!!!
- php---get_included_files — 返回被 include 和 require 文件名的 array
- tomcat域名重定向
- Poj 2676
- js实现进度条
- lua学习笔记---值,类型
- C++设计模式
- 数组-12. 简易连连看(20)
- 二叉树路径和
- Unity3D架构系列之- FSM有限状态机设计(六)(总结篇)
- UVa 211 - The Domino Effect [DFS剪枝]
- hdu 5172 线段树