当前位置:网站首页>Counting garlic customers: Sudoku (DFS)
Counting garlic customers: Sudoku (DFS)
2022-04-23 01:25:00 【OldLeft】



The sample input
* 2 6 * * * * * *
* * * 5 * 2 * * 4
* * * 1 * * * * 7
* 3 * * 2 * 1 8 *
* * * 3 * 9 * * *
* 5 4 * 1 * * 7 *
5 * * * * 1 * * *
6 * * 9 * 7 * * *
* * * * * * 7 5 *
Sample output
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
Answer key : This topic is similar to the eight queens problem , It's just that the eight queens are for each line 1−8 Attempts to , And this topic is about each empty space 1−9 Attempts to . And the problem can be ended when a feasible solution is found .
The marking method is to mark whether a line or a number appears , Mark whether a number in a column appears , Mark whether a small square or a number appears .
Be careful :0-8 The marking method of a palace is : Line number /3*3+ Column number /3,scanf Of c% Put a space in front , Counteract the effect of input spaces
#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;// A situation has been found , sign out
}
if(x==9){
// At the end of the line, find a legal situation to print
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){
// If a column is placed , Place the next line
dfs(x+1,0);
return;
}
if(mp[x][y]!='*'){
// If there are already numbers , Continue to place the next column
dfs(x,y+1);
return;
}
for(int j=1;j<=9;j++){
// Enumerate and place... On a location 1-9 The situation of
if(!r[x][j]&&!c[y][j]&&!gong[x/3*3+y/3][j]){
// If you can , Column , There is no such number in the palace , You can put
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);// Continue to place the next column
r[x][j]=false;// to flash back
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]);// Be careful scanf Of c% Put a space in front , Counteract the effect of input spaces
}
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(mp[i][j]!='*'){
// Preprocessing mark
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://yzsam.com/2022/04/202204230120462372.html
边栏推荐
- gin--hello
- 8GBs communication between client and server
- 无关联进程间通信 —— 命名管道的创建和使用
- Blocking type of gbase 8s concurrency control
- . net (c) MySQL conn.open() reports an error: solution to SSL connection error
- Gbase 8t index
- 2n皇后问题
- .NET(C#) MySQL conn.Open()报错:SSL Connection error的解决方法
- Slow response of analysis API
- 世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
猜你喜欢

On regular expression matching cryptography

京东一面:子线程如何获取父线程 ThreadLocal 的值?我蒙了。。。

Slow response of analysis API

Unrelated interprocess communication -- creation and use of named pipes

Detailed explanation of Milvus 2.0 quality assurance system

In the second half of the smart watch, opportunities and challenges coexist

Earth day collection: Microsoft and Intel invite you to get the green Ambassador badge and give you negative carbon emission!

Huawei CDN is fast everywhere

C language guessing game and trickery game

VSCODE + PHP Debug + 名字空间指引
随机推荐
Introduction to granularity locking of gbase 8s concurrency control
深入解析Linux下的磁盘缓存机制与SSD的写入放大问题
Cloud native Virtualization: building edge computing instances based on kubevirt
2n皇后问题
"Self abuse artifact" exploded overnight: control your face with a handle, take your own code, and bear the consequences
engine.POST()处理POST请求
[interview skills] how to face an interview without a leading group
8GBs communication between client and server
GBase 8s 备份介绍
GBase 8t索引
In depth analysis of disk cache mechanism and SSD write amplification under Linux
计蒜客:方程的解数
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
Huawei CDN is fast everywhere
Detailed explanation of the usage of C language getchar
Interface automation session authentication solution
Introduction à la structure de stockage de gbase 8S et à la gestion de l'espace
无关联进程间通信 —— 命名管道的创建和使用
Itextsharp page setup
[the first contact between Android engineers and smart home products ③] the specific implementation of smartconfig one key distribution network on the hardware side | the specific implementation of es