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. JAX-WS(三)构建简单webservice部署到tomcat上

    前言: 虽然构建本地的jax-ws的webservice很简单,但要部署到tomcat上要绕过点弯. tomcat本身和jdk都没有jaw-ws的API,所以部署的时候需要额外做点事情,有两种选择 1 ...

  2. 怎么查找执行比较慢的sql语句-DBA给的建议

    1.使用sql动态视图 如下: b.text,a.total_worker_time,a.total_logical_reads,a.total_elapsed_time,execution_coun ...

  3. JAVA NIO系列(三) Buffer 解读

    缓冲区分类 NIO中的buffer用于和通道交互,数据是从通道读入缓冲区,从缓冲区中写入通道的.Buffer就像一个数组,可以保存多个类型相同的数据.每种基本数据类型都有对应的Buffer类: 缓冲区 ...

  4. JAVA-面向对象-多态

    多态 1.方法重载 2.方法重写 3.对象转型 4.抽象(可以定义类和方法)    (关键字  abstract)   ( 如: public abstract class robot  )(不能修饰 ...

  5. 转:python socket编程详细介绍

    Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...

  6. javascript 一些常用的验证

    只能输入数字          onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onafterpaste="th ...

  7. oracle 新手遇到常见问题的解决办法

    可能照成以下问题的原因也许有很多种,但是就小白而言,我只记录自己学习过程中遇到的所有的问题.希望对一些新手 小白们有所帮助. 原因是 sys 不是sysdba 用户,你要将其作为sysdba 用户登录 ...

  8. 从一简单程序看C语言内存分配

    int main13(){ char buf[20]="aaaa"; char buf2[] = "bbbb"; char *p1 = "111111 ...

  9. java将数组中的零放到末尾

    package com.shb.java; /** * 将数组中的0放到数组的后边,然后原来的非零数的顺序不改变 * @author BIN * */ public class Demo2{ publ ...

  10. 转:Order&Shipping Transactions Status Summary

    详细内容: http://blog.csdn.net/pan_tian/article/details/7696528 WSH_DELIVERY_DETAILS.Release_Status can ...