当前位置:网站首页>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
边栏推荐
- Error: permissionerror: [winerror 32] this file is in use by another program and cannot be accessed by the process. Solution of "+ file path"
- 找数字(DFS)
- GBase 8s查询处理和优化
- Qingyan environment and Shenzhen Stock Exchange listing: annual revenue of 180 million and market value of 4.1 billion
- Linked list dynamic header insertion
- 8GBs communication between client and server
- [蓝桥杯][2019年第十届真题]外卖店优先级
- [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
- Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
- 修改数组(并查集)
猜你喜欢

CVPR | 2022 | expressed by transformer learning multiple hypotheses! A new framework for 3D human pose estimation!

Project manager's thinking mode worth trying: project success equation

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

Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)

gin -get请求的小示例2-Handle处理post请求
再谈被动安全 教你看懂中保研碰撞测试的评级报告

Google developer tool preserve log

全排列(DFS和next_permutation解法)

无关联进程间通信 —— 命名管道的创建和使用

C language guessing game and trickery game
随机推荐
iTextSharp 显示中文字体
Gbase 8s数据库日志简介及管理
JD side: comment un thread enfant obtient - il la valeur de threadlocal du thread parent? Je suis couvert...
gin--hello
计蒜客家谱(dfs求直系后代数)
【Android工程师与智能家居产品的第一次接触③】SmartConfig一键配网在硬件端的具体实现|ESP8266一键配网在Arduino的具体实现|玉念聿辉
JD side: how can a child thread get the value of the parent thread ThreadLocal? I got...
"Self abuse artifact" exploded overnight: control your face with a handle, take your own code, and bear the consequences
Earth day collection: Microsoft and Intel invite you to get the green Ambassador badge and give you negative carbon emission!
.NET(C#) MySQL conn.Open()报错:SSL Connection error的解决方法
Thingskit Internet of things platform
Is it difficult for girls to learn software testing?
Text justify, orientation, combine text attributes
Get rid of the "small workshop" of AI production: how to build a cloud native AI platform based on kubernetes
How to introduce SPI into a project
Gbase 8t index
ai2022新功能,illustrator 2022 新功能介绍
蒜头君开公司(DFS全排列)
Unity combines itextsharp to generate PDF preparation DLL
Gbase 8s 并发控制之粒度锁介绍