Sudoku
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的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- ACM: ICPC/CCPC Sudoku DFS - 数独
Sudoku Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Submis ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- Sudoku 数独游戏
#include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...
随机推荐
- sublime text常用插件
这个比较重要,不会装插件的时候找了好久 sublime text常用插件 1.插件的安装方法 第一种:用package control 这个是用来管理插件的,必备啊,安装package control ...
- WinForm利用 WinApi实现 淡入淡出 弹出 效果 仿QQ消息
消息框: using System.Runtime.InteropServices; namespace Windows_API_实现屏幕右下角_消息框_ { public partial class ...
- 代码实现sql数据库的附加(通常在安装的时候)
判断数据库是否已经存在 SqlConnection judgeConn = new SqlConnection("server=.;database=master;uid="+us ...
- Demo12SimpleAdapter
/Users/alamps/AndroidStudioProjects/Demo12SimpleAdapter/Demo12SimpleAdapter/src/main/res/layout/data ...
- Power Gating的设计(架构)
switching network的层次: 一般选择flatted的形式,hierarchy的结构对voltage drop和performance delay有影响. Power network的结 ...
- 【RoR win32】提高rails new时bundle install运行速度
在新建rails项目时,rails new老是卡在bundle install那里,少则五分钟,多则几十分.这是因为rails new时自动会运行bundle install,而bundle inst ...
- 电脑配置 eclipse 环境变量
先找到电脑环境变量 我的电脑——属性——高级系统设置——环境变量 path里面需要两个环境 第一个Java 环境 C:\Program Files\Java\jdk1.8.0_40\bin; 第二个a ...
- django models使用学习记录
如何更新单个数据 example = User.objects.get(id=1) example.is_acitve=1 example.save() 如何更新多个数据 examples = Use ...
- linux下网卡启动、配置
步骤1.配置/etc/sysconfig/network-scripts/ifcfg-eth0 里的文件. ifcfg-eth0的配置详情:[root@localhost ~]# vim /etc/s ...
- python 去除字符串中连续的空格,并使用其他的分隔符替代连续的空格
例:1: filt函数用法及匿名函数lamda用法,详见http://www.cnblogs.com/apple2016/p/5657698.html join()用法详见http://www.cnb ...