Sudoku

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 15952 Accepted: 7791 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 <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF =0x3f3f3f3f;
const int Max =120;
char str[11][11];
bool a[11][11];
bool b[11][11];
bool c[11][11];
int Map[11][11];
bool DFS(int x,int y)
{
if(x==9)
{
return true;
}
bool flag=false;
if(Map[x][y])
{
if(y==8)
{
flag=DFS(x+1,0);
}
else
{
flag=DFS(x,y+1);
}
if(flag)
{
return true;
}
else
{
return false;
}
}
else
{
int k=3*(x/3)+y/3; for(int i=1; i<=9; i++)
{
if(!a[x][i]&&!b[y][i]&&!c[k][i])
{
Map[x][y]=i;
a[x][i]=true;
b[y][i]=true;
c[k][i]=true;
if(y==8)
{
flag=DFS(x+1,0);
}
else
{
flag=DFS(x,y+1);
}
if(!flag)
{
Map[x][y]=0;
a[x][i]=false;
b[y][i]=false;
c[k][i]=false;
}
else
{
return true;
}
}
}
}
return false;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=0; i<9; i++)
{
scanf("%s",str[i]);
}
memset(a,false,sizeof(a));
memset(b,false,sizeof(b));
memset(c,false,sizeof(c));
memset(Map,0,sizeof(Map));
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
Map[i][j]=str[i][j]-'0';
if(Map[i][j])
{
a[i][Map[i][j]]=true;
b[j][Map[i][j]]=true;
int k=3*(i/3)+j/3;
c[k][Map[i][j]]=true;
}
}
}
DFS(0,0);
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
cout<<Map[i][j];
cout<<endl;
}
}
return 0;
}

Sudoku的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  2. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  5. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  6. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  7. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  8. Leetcode: Sudoku Solver

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  10. Sudoku 数独游戏

    #include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...

随机推荐

  1. 通过xib加载textfield的时候 发生 this class is not key value coding-compliant for the key textField. 情况怎么解决

    连线的时候不要选files’owner 要选xib自己的class

  2. Redis分布式

    昨天公司技术大牛做了一个Redis分布式的技术分享: Redis分布式资源: http://redis.io/topics/cluster-tutorialhttp://redis.io/topics ...

  3. 创建一个web Test Plan

    1.添加ThreadGroup (1).线程组界面解析: 线程数:虚拟用户的个数 Ramp-up Period:开启每个用户的延迟时间,如果有5个虚拟用户,Ramp-up Period值是5,Jmet ...

  4. JDBC工作模块

    jdbc-->java数据库连接   的简称 JDBC工作模块 加载jdbc驱动 与数据库连接 发送sql语句,得到返回结果 处理返回结果 释放资源

  5. windows系统调用 线程 启动与挂起

    #include "iostream" #include "windows.h" using namespace std; class CWorkerThrea ...

  6. JS 实现中英文翻译

    缺点就是还是会闪出中文,但是效果还行. var langPackage = { "主题":"Title", "下一页":"Next ...

  7. scala2.10.x case classes cannot have more than 22 parameters

    问题 这个错误出现在case class参数超出22个的时候. case classes cannot have more than 22 parameters 在scala 2.11.x版本以下时c ...

  8. 浅谈JavaScript计时器

    JavaScript计时器 1.什么是JavaScript计时器? 在JavaScript中,我们可以在设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行. 2.计时器类型 一次性计时器:仅在 ...

  9. mysql 导出过长的数字列时变科学计数法问题解决办法

    --mysql 导出数据时,  数字类型的列如果位数过长,变为科学技术发问题  concat('\t',a.IDCARD_NO)     例子: select   concat('\t',a.IDCA ...

  10. Java魔法堂:注释和注释模板 (转)

    http://www.cnblogs.com/fsjohnhuang/p/3988883.html 一.注释   1. 注释类型 [a]. 单行注释 // 单行注释 String type = &qu ...