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 ...
随机推荐
- HTML语言的一些元素(四)
以下资料整理自网路 1.锚点是网页制作中超级链接的一种,又叫命名锚记.命名锚记像一个迅速定位器一样是一种页面内的超级链接,运用相当普遍. 英文名:anchor 使用命名锚记可以在文档中设置标记,这些标 ...
- 证明 logX < X 对所有 X > 0 成立
题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss 练习1.5(a) 证明下列公式: logX < X 对所有 X > ...
- window.cookie
本地测试cookie用火狐来测试 首先cookie是document上的一个属性. 先弹出一个cookie alert(document.cookie); //弹出是空的 设置cookie,格式是有一 ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- 数dp多少个0-n多少0-9
#include <bits/stdc++.h> using namespace std; const int N = 15; int n; int dp[N][N][N]; int a[ ...
- table创建固定表头
布局:两个div,上部内容将表头复制,高度固定,下部div内部将table设置为margin:-**px; 隐藏掉表头,下部div设置overflow,即可. 代码:
- jQuery中处理事件冒泡的方法和取消后续内容的方法
一:事件冒泡的意思是:一个大的容器已经设置了事件,如果这个容器里还包容着一个小的容器也设置了自己的事件,那么因为小容器是在大容器里面的,触发小容器的事件同时也等于触发了大容器的事件,有时这并不是我们想 ...
- 使用streaming window函数统计用户不同时间段平均消费金额等指标
场景 现在餐厅老板已经不满足仅仅统计历史用户消费金额总数了,他想知道每个用户半年,每个月,每天,或者一小时消费的总额,来店消费的次数以及平均金额. 给出的例子计算的是每5秒,每30秒,每1分钟的用户消 ...
- sql存储过程传入ID集合,和临时表的使用
方式1: Declare @SQL NVarChar(max) set @SQL='select *from Loanee as a ApplicationID in ('+@Application ...
- 夺命雷公狗ThinkPHP项目之----企业网站18之网站配置列表页的完成
我们点击下配置列表即可查看我们列表页的配置信息了: 其实这个最简单了,首先我们先来完成他控制器的代码: public function lists(){ $mod = M('Conf')->se ...