当前位置:网站首页>计蒜客:数独(DFS)
计蒜客:数独(DFS)
2022-04-23 01:21:00 【OldLeft】



样例输入
* 2 6 * * * * * *
* * * 5 * 2 * * 4
* * * 1 * * * * 7
* 3 * * 2 * 1 8 *
* * * 3 * 9 * * *
* 5 4 * 1 * * 7 *
5 * * * * 1 * * *
6 * * 9 * 7 * * *
* * * * * * 7 5 *
样例输出
1 2 6 7 3 4 5 9 8
3 7 8 5 9 2 6 1 4
4 9 5 1 6 8 2 3 7
7 3 9 4 2 5 1 8 6
8 6 1 3 7 9 4 2 5
2 5 4 8 1 6 3 7 9
5 4 7 2 8 1 9 6 3
6 1 3 9 5 7 8 4 2
9 8 2 6 4 3 7 5 1
题解:这道题目类似八皇后问题,只不过八皇后是对每一行进行 1−8 的尝试,而这道题目是对每个空进行 1−9 的尝试。而且这道题目搜索到一种可行解就可以结束了。
标记方法为标记某行某个数字是否出现,标记某列某个数字是否出现,标记某个小方格某个数字是否出现。
注意:0-8个宫的标记方法为:行号/3*3+列号/3,scanf的c%前面加上空格,抵消输入空格的影响
#include<bits/stdc++.h>
using namespace std;
char mp[20][20];
bool f;
bool r[20][20],c[20][20],gong[20][20];
void dfs(int x,int y){
if(f){
return;//已经找到一种情况,退出
}
if(x==9){
//到行末了找到一种合法情况打印
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout<<mp[i][j];
if(j!=8) cout<<" ";
else cout<<endl;
}
}
f=true;
return;
}
if(y==9){
//如果一列放置完,放置下一行
dfs(x+1,0);
return;
}
if(mp[x][y]!='*'){
//如果已经有数字了,继续放置下一列
dfs(x,y+1);
return;
}
for(int j=1;j<=9;j++){
//对一个位置枚举放置1-9的情况
if(!r[x][j]&&!c[y][j]&&!gong[x/3*3+y/3][j]){
//如果行,列,宫都没有这个数,则可以放置
r[x][j]=true;
c[y][j]=true;
gong[x/3*3+y/3][j]=true;
mp[x][y]='0'+j;
dfs(x,y+1);//继续放置下一列
r[x][j]=false;//回溯
c[y][j]=false;
gong[x/3*3+y/3][j]=false;
mp[x][y]='*';
}
}
}
int main(){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
scanf(" %c",&mp[i][j]);//注意scanf的c%前面加上空格,抵消输入空格的影响
}
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(mp[i][j]!='*'){
//预处理标记
r[i][mp[i][j]-'0']=true;
c[j][mp[i][j]-'0']=true;
gong[i/3*3+j/3][mp[i][j]-'0']=true;
}
}
}
dfs(0,0);
return 0;
}
版权声明
本文为[OldLeft]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43833610/article/details/115768998
边栏推荐
- Function encapsulation such as addition, deletion, modification and query of linked list (summary)
- GBASE 8s分片表管理操作
- “自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
- Frequently asked questions about recent BSN development
- C language guessing game and trickery game
- What is tooljet and how about it—— Evaluation of low code development platform
- Oplg: new generation cloud primary observable best practices
- Hardware IIC analysis and configuration process of imx6ull bare metal development
- Software maintenance exercises
- Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)
猜你喜欢

Text justify, orientation, combine text attributes

体育训练中心项目电力监控系统的研究与应用
![[actf2020 freshman competition]](/img/0c/4c06112383c0b225c987a499b622a9.png)
[actf2020 freshman competition]

2022 penetration job interview (thinking)

Practice and exploration of knowledge map visualization technology in meituan

世界读书日:18本豆瓣评分9.0以上的IT书值得收藏

Research and application of Acrel-5000 building energy consumption monitoring system in Xixian Airport Garden Project

京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。

Detailed explanation of Milvus 2.0 quality assurance system

Three technical solutions of ant group were selected as "typical solutions for information technology application and innovation in 2021"
随机推荐
JD side: comment un thread enfant obtient - il la valeur de threadlocal du thread parent? Je suis couvert...
那些咸鱼上买来的代码|ssm酒店客房管理系统|买来的源码是否真的可以使用|来自程序员的困惑|玉念聿辉|大丑村吴明辉
Interview eight part essay (disorderly order, no classification)
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Detailed explanation of the usage of C language getchar
GBASE 8s分片表管理操作
Good test data management, in the end how to do?
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Introduction to gbase 8s storage structure and space management
【服务器数据恢复】服务器硬盘进水后服务器崩溃的数据恢复案例
2022 penetration job interview (thinking)
Gbase 8s数据库日志简介及管理
Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
Gbase 8s shared memory segment deletion
How does zhiting connect Xiaomi smart speakers?
gin框架的学习--golang
In the second half of the smart watch, opportunities and challenges coexist
Basic operation of Android local database | multi thread operation database | addition, deletion, modification and query of database | batch insertion into database | basic use of thread pool | Yu nia
The origin explanation and use example of image pre training model
Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system