Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24108   Accepted: 11259   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

 
 
 
题意:数独游戏,   规则------->>>>>    1.每行每列都包含1到9,且数字不重复。    每个3*3的小矩阵中也包含数字1到9,数字不重复。
 
 
思路:  做标记,    3个标记,分别行,列和小矩阵,                              数字1到9,用过标记,true,否则   false。
 
ac代码:
 
  

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
const int MAX=15;
int map[MAX][MAX];
char temp[MAX];
bool row[MAX][MAX],line[MAX][MAX],rl[MAX][MAX],flag=false;
void dfs(int x,int y)
{
if (x==10){
flag=true;
return ;
}
if (map[x][y]){
if (y==9)
dfs(x+1,1);
else
dfs(x,y+1);
if (flag)
return ;
}
else{
int k=3*((x-1)/3)+(y-1)/3+1;
for (int i=1;i<10;i++){
if (!row[x][i]&&!line[y][i]&&!rl[k][i]){
map[x][y]=i;
row[x][i]=line[y][i]=rl[k][i]=true;
if (y==9)
dfs(x+1,1);
else
dfs(x,y+1);
if(flag)
return ;
map[x][y]=0;
row[x][i]=line[y][i]=rl[k][i]=false;
}
}
}
}
int main()
{
int t;
cin>>t;
while (t--){
flag=false;
memset(map,0,sizeof(map));
memset(row,false,sizeof(row));
memset(line,false,sizeof(line));
memset(rl,false,sizeof(rl));
for (int i=1;i<10;i++){
cin>>temp+1;
for (int j=1;j<10;j++){
map[i][j]=temp[j]-'0';
if (map[i][j]){
int k=3*((i-1)/3)+(j-1)/3+1;
row[i][map[i][j]]=line[j][map[i][j]]=rl[k][map[i][j]]=true;
}
}
}
dfs(1,1);
for (int i=1;i<10;i++){
for (int j=1;j<10;j++){
printf("%d",map[i][j]);
}
printf("\n");
}
}
return 0;
}

poj2676 (dfs+回溯)的更多相关文章

  1. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  2. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  7. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  9. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

随机推荐

  1. JQuery获取和设置Select选项的常用方法总结

    1.获取select 选中的 text:  $("#cusChildTypeId").find("option:selected").text();  $(&q ...

  2. 5.spring:注解配置 Bean

    在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...

  3. 用python实现ping

    #!/usr/bin/env python #coding=utf-8 import os import argparse import socket import struct import sel ...

  4. spring加载属性(properties)文件

    一.注解方式加载 jdbc.driver=org.mariadb.jdbc.Driver jdbc.url=jdbc:mariadb://localhost:3306/kt jdbc.user=roo ...

  5. java中StringBuffer与String、StringBuilder的区别

    在java中我们经常可以看到StringBuffer和String的用法,但是我自己在使用过程中,经常会将两者弄混淆,今天我们就来了解一下两者的区别: 我们首先来看一下我们的官方API中的简单介绍: ...

  6. 屏蔽JS代码错误

    用来屏蔽IE的错误的JS代码,也能屏蔽弹出框错误! <SCRIPT language=JavaScript> function killErrors() { return true; } ...

  7. input和div模仿select,带输入提示

    有时候我们需要select和input的结合体,即可以使用下拉框,同时也可以用来输入,输入的同时显示可选的下拉选项 先上html代码 <div class="input-group i ...

  8. JSON.stringify()和JSON.parse()的区别

    JSON.stringify()此方法用于将一个对象解析成字符串并返回. JSON.parse()此方法刚好相反是将一个字符串对象解析成一个JSON对象.

  9. MySQL---正确使用索引、limit分页、执行计划、慢日志查询

    正确使用索引 数据库表中添加索引后确实会让查询速度起飞,但前提必须是正确的使用索引来查询,如果以错误的方式使用,则即使建立索引也会不奏效.即使建立索引,索引也不会生效: - like '%xx' se ...

  10. TinyMCE插件:RESPONSIVE filemanager 9 图片自动添加水印

    跟踪function() 搜索(filemanager/upload.php) 在代码中发现,上传成功后,会传回JSON信息数据,于是最后找到方法是 $upload_handler = new Upl ...