Gym 100646 Problem E: Su-Su-Sudoku 水题
Problem E: Su-Su-Sudoku/center>
题目连接:
http://codeforces.com/gym/100646/attachments
Description
By now, everyone has played Sudoku: you’re given a 9-by-9 grid of boxes which you are to fill in with
the digits 1 through 9 so that 1) every row has all nine digits, 2) every column has all nine digits, and
3) all nine 3-by-3 subgrids have all nine digits. To start the game you are given a partially completed
grid and are asked to fill in the remainder of the boxes. One such puzzle is shown below.
4 8 1 2 5 3 6 9 7
2 6 7 9 4 8 1 5
5 3 9 6 7 1 2 4
6 5 4 3 8 9 7 1 2
9 8 7 4 5 6 3
1 7 3 5 6 2 8 4 9
7 2 1 3 6 9 5 8
3 1 5 8 9 7 4 2 6
8 9 6 4 2 5 3 7 1
In this problem, you will be given Sudoku grids which you have nearly completed; indeed you’ve filled
in every box except five. You are asked to complete the grid, or determine that it’s impossible. (You
might have already made an error!)
Input
The first line of input will contain a positive integer indicating the number of test cases to follow. Each
test case will be a nearly completed Sudoku grid consisting of 9 lines, each containing 9 characters from
the set of digits 0 through 9. There will be exactly five 0’s in each test case, indicating the five unfilled
boxes.
Output
Output for each test case should be either
Could not complete this grid.
if it is impossible to complete the grid according to the rules of the game, or the completed grid, in the
form given below. (There are no blank spaces in the output.) If there is a way to complete the grid, it
will be unique. Separate test cases with a blank line.
Sample Input
2
481253697
267948105
539671204
654389712
908704563
173562849
702136958
315897426
896425371
481253697
267948105
539671284
654289710
908704562
173562849
702136958
315897426
896425371
Sample Output
481253697
267948135
539671284
654389712
928714563
173562849
742136958
315897426
896425371
Could not complete this grid.
Hint
题意
给你个数独,问你有没有解。
但是这个数独最多只有5个空位。
题解:
智障题,只有五个空,那就XJBdfs就好了
代码
#include<bits/stdc++.h>
using namespace std;
string s[9];
struct node{
int x,y;
node(int X,int Y):x(X),y(Y){};
};
vector<node> V;
int flag = 0;
int vis[10];
bool check(){
for(int i=0;i<9;i++){
for(int j=0;j<10;j++)vis[j]=0;
for(int j=0;j<9;j++){
vis[s[i][j]-'0']=1;
}
for(int j=1;j<=9;j++)if(!vis[j])return false;
}
for(int i=0;i<9;i++){
for(int j=0;j<10;j++)vis[j]=0;
for(int j=0;j<9;j++){
vis[s[j][i]-'0']=1;
}
for(int j=1;j<=9;j++)if(!vis[j])return false;
}
for(int i=0;i<10;i++)vis[i]=0;
vis[s[0][0]-'0']=1;
vis[s[0][1]-'0']=1;
vis[s[0][2]-'0']=1;
vis[s[1][0]-'0']=1;
vis[s[1][1]-'0']=1;
vis[s[1][2]-'0']=1;
vis[s[2][0]-'0']=1;
vis[s[2][1]-'0']=1;
vis[s[2][2]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[0][3]-'0']=1;
vis[s[0][4]-'0']=1;
vis[s[0][5]-'0']=1;
vis[s[1][3]-'0']=1;
vis[s[1][4]-'0']=1;
vis[s[1][5]-'0']=1;
vis[s[2][3]-'0']=1;
vis[s[2][4]-'0']=1;
vis[s[2][5]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[0][6]-'0']=1;
vis[s[0][7]-'0']=1;
vis[s[0][8]-'0']=1;
vis[s[1][6]-'0']=1;
vis[s[1][7]-'0']=1;
vis[s[1][8]-'0']=1;
vis[s[2][6]-'0']=1;
vis[s[2][7]-'0']=1;
vis[s[2][8]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[3][0]-'0']=1;
vis[s[3][1]-'0']=1;
vis[s[3][2]-'0']=1;
vis[s[4][0]-'0']=1;
vis[s[4][1]-'0']=1;
vis[s[4][2]-'0']=1;
vis[s[5][0]-'0']=1;
vis[s[5][1]-'0']=1;
vis[s[5][2]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[3][3]-'0']=1;
vis[s[3][4]-'0']=1;
vis[s[3][5]-'0']=1;
vis[s[4][3]-'0']=1;
vis[s[4][4]-'0']=1;
vis[s[4][5]-'0']=1;
vis[s[5][3]-'0']=1;
vis[s[5][4]-'0']=1;
vis[s[5][5]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[3][6]-'0']=1;
vis[s[3][7]-'0']=1;
vis[s[3][8]-'0']=1;
vis[s[4][6]-'0']=1;
vis[s[4][7]-'0']=1;
vis[s[4][8]-'0']=1;
vis[s[5][6]-'0']=1;
vis[s[5][7]-'0']=1;
vis[s[5][8]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[6][0]-'0']=1;
vis[s[6][1]-'0']=1;
vis[s[6][2]-'0']=1;
vis[s[7][0]-'0']=1;
vis[s[7][1]-'0']=1;
vis[s[7][2]-'0']=1;
vis[s[8][0]-'0']=1;
vis[s[8][1]-'0']=1;
vis[s[8][2]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[6][3]-'0']=1;
vis[s[6][4]-'0']=1;
vis[s[6][5]-'0']=1;
vis[s[7][3]-'0']=1;
vis[s[7][4]-'0']=1;
vis[s[7][5]-'0']=1;
vis[s[8][3]-'0']=1;
vis[s[8][4]-'0']=1;
vis[s[8][5]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
for(int i=0;i<10;i++)vis[i]=0;
vis[s[6][6]-'0']=1;
vis[s[6][7]-'0']=1;
vis[s[6][8]-'0']=1;
vis[s[7][6]-'0']=1;
vis[s[7][7]-'0']=1;
vis[s[7][8]-'0']=1;
vis[s[8][6]-'0']=1;
vis[s[8][7]-'0']=1;
vis[s[8][8]-'0']=1;
for(int j=1;j<=9;j++)if(!vis[j])return false;
return true;
}
void dfs(int x){
if(flag)return;
if(x==V.size()){
if(check()){
for(int i=0;i<9;i++)
cout<<s[i]<<endl;
flag = 1;
return;
}
return;
}
for(int i=1;i<=9;i++){
s[V[x].x][V[x].y]=i+'0';
dfs(x+1);
if(flag)return;
s[V[x].x][V[x].y]='0';
}
}
void solve(){
flag = 0;
V.clear();
for(int i=0;i<9;i++)
cin>>s[i];
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(s[i][j]=='0'){
V.push_back(node(i,j));
}
}
}
dfs(0);
if(flag==0){
printf("Could not complete this grid.\n");
}
printf("\n");
}
int main(){
//freopen("1.in","r",stdin);
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
Gym 100646 Problem E: Su-Su-Sudoku 水题的更多相关文章
- Gym 100646 Problem C: LCR 模拟题
Problem C: LCR 题目连接: http://codeforces.com/gym/100646/attachments Description LCR is a simple game f ...
- 烟大 Contest1025 - 《挑战编程》第二章:数据结构 Problem A: Jolly Jumpers(水题)
Problem A: Jolly Jumpers Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 10 Solved: 4[Submit][Status] ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem L. Stock Trading Robot 水题
Problem L. Stock Trading Robot 题目连接: http://www.codeforces.com/gym/100253 Description CyberTrader is ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)
Problem C: The Trip Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 19 Solved: 3[Submit][Status][Web ...
- Gym 100507L Donald is a postman (水题)
Donald is a postman 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/L Description Donald ...
- Bracket Sequences Concatenation Problem CodeForces - 990C(括号匹配水题)
明确一下 一个字符串有x左括号不匹配 和 另一个字符串有x个右括号不匹配 这俩是一定能够匹配的 脑子有点迷 emm... 所以统计就好了 统计x个左括号的有几个,x个右括号的有几个 然后 乘一 ...
- 【BZOJ】1603: [Usaco2008 Oct]打谷机(水题+dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1603 这种水题... dfs没话说.. #include <cstdio> #inclu ...
- Codeforces Gym 102392F Game on a Tree (SEERC2019 F题) 题解
题目链接:https://codeforces.com/gym/102392/problem/F 题意:被这题题意坑了很久,大意是说有一棵根为 \(1\) 的树,每个节点初始都是白色, \(Alice ...
- Codeforces Gym 100531G Grave 水题
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
随机推荐
- HDU 3595 every-sg模型
多个子游戏同时进行,每个子游戏给出两个数a,b,可以将大的数减去k倍小的数,不能操作者输. 策略就是对于一个必胜的游戏要使得步数更长,对于一个必败的游戏使得步数最短. 以下都来自贾志豪的论文.. 对于 ...
- AngularJs -- ngMessages(1.3+)
ngMessages(1.3+) 表单和验证是AngularJS中复杂的组件之一.用AngularJS默认的方式来写,不是特别好,不简洁. 在AngualrJS1.3发布前,表单验证必须以这种方式编写 ...
- MySQL索引背后的数据结构及算法原理 (转)
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- 产品排序(2015 年北大自招夏令营) (与栈相关的区间DP)
题面: \(solution:\) 又是一道\(DP\)的好题啊!状态并不明显,需要仔细分析,而且还结合了栈的特性! 做这一类题,只要出题人有点理想,一定会在栈的性质上做点文章,所以我们尽量围绕栈的性 ...
- SocketServer源码学习补充
在前两个文章中整理了关于BaseServer部分以及BaseRequestHandler,以及通过对TCP的处理的流程的整理,这次整理的是剩下的关于用于扩展的部分,这里通过对线程扩展进行整理 Thre ...
- JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
1.前言 遇到Trojan[Downloader]:JS/Nemucod需要分析,这款病毒主要为js运行.从网上各种找js调试方法.发现52的帖子还挺沾边的. TrojanDownloader:JS/ ...
- Mysql导入脚本失败,提示需要SUPER权限
1.删除: /*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ 2.查看增删函数有没有重复 3.删除: set GLOBAL log ...
- javascript设计模式----桥接模式、组合模式、装饰者模式、享元模式
http://blog.csdn.net/painsonline/article/details/7215087 桥接模式:http://www.cnblogs.com/TomXu/archiv ...
- elasticsearch6.3.2之x-pack6.3.2破解安装并配合kibana使用
原文链接:https://www.plaza4me.com/article/20180825223826278 由于在elasticsearch在6.3版本之后x-pack是默认安装好的,所以不再需要 ...
- 面试经典---数据库索引B+、B-树
大型数据库数据都是存在硬盘中的,为了操作的速度,需要设计针对外存的数据结构.而数据库索引技术就是在面试中反复被问到的一个问题:数据库索引是怎么实现的?数据库索引越大越好吗? 需要详细了解下这方面的知识 ...