当前位置:网站首页>Blue Bridge Cup Square filling (DFS backtracking)
Blue Bridge Cup Square filling (DFS backtracking)
2022-07-05 01:16:00 【Woodenman Du】
Question:

Solve:
In general, it is a little Limited dfs Board question
Keep trying which number can be filled in each grid , Then go to the next box and continue filling , until 10 Put all the numbers in or you can't put them to an end
As for adjacent numbers , It can be written directly as enumeration if,CV Just modify it , Therefore, when I write code, I expand the range of coordinate grids , Then each cell is initialized directly to an impossible number
The second is to remember to look back after entering the next box
Code:
#include<bits/stdc++.h>
using namespace std;
int res = 0; // Count
int a[5][6]; // grid
bool num[10]; // Numeric fill marks
// Absolute value judgment
int f(int x)
{
return x < 0 ? -x : x;
}
// Deep search
void dfs(int x,int y,int deep)
{
if(deep > 10){
res++;
return ;
}
for(int i = 0; i <= 9; i++){
// The number is selected
if(num[i]) continue;
// Cross lattice
if( f(a[x-1][y] - i)<=1 ) continue;
if( f(a[x+1][y] - i)<=1 ) continue;
if( f(a[x][y+1] - i)<=1 ) continue;A
if( f(a[x][y-1] - i)<=1 ) continue;
// Diagonally
if( f(a[x+1][y+1] - i)<=1 ) continue;
if( f(a[x-1][y+1] - i)<=1 ) continue;
if( f(a[x+1][y-1] - i)<=1 ) continue;
if( f(a[x-1][y-1] - i)<=1 ) continue;
// Fill in the figures , Go to the next level
a[x][y] = i; num[i] = true;
if(x == 1 && y == 4) dfs(2,1,deep+1);
else if(x == 2 && y == 4) dfs(3,1,deep+1);
else dfs(x,y+1,deep+1);
// to flash back
a[x][y] = -10; num[i] = false;
}
}
int main(void)
{
// initialization
memset(num,false,sizeof(num));
for(int i = 0; i <= 4; i++)
for(int j = 0; j <= 5; j++)
a[i][j] = -10;
// Search for
dfs(1,2,1);
cout <<res;
return 0;
}Statement : The pictures are from the official website of the Blue Bridge Cup , For the purpose of sorting out personal questions , In case of infringement , Please contact to delete ~
边栏推荐
- Introduction to the gtid mode of MySQL master-slave replication
- 107. Some details of SAP ui5 overflow toolbar container control and resize event processing
- Discrete mathematics: reasoning rules
- 那些一门心思研究自动化测试的人,最后都怎样了?
- Compare whether two lists are equal
- Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
- Inventory of more than 17 typical security incidents in January 2022
- Hand drawn video website
- Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Getting started with Paxos
猜你喜欢
![Grabbing and sorting out external articles -- status bar [4]](/img/88/8267ab92177788ac17ab665a90b781.png)
Grabbing and sorting out external articles -- status bar [4]

What you learned in the eleventh week

Chia Tai International Futures: what is the master account and how to open it?

BGP comprehensive experiment

Innovation leads the direction. Huawei Smart Life launches new products in the whole scene

微信小程序:全新独立后台月老办事处一元交友盲盒

A simple SSO unified login design

Wechat applet; Gibberish generator

Implementation steps of master detail detail layout mode of SAP ui5 application

Remote control service
随机推荐
视频网站手绘
Discrete mathematics: reasoning rules
User login function: simple but difficult
Four pits in reentrantlock!
Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!
Take you ten days to easily complete the go micro service series (IX. link tracking)
抓包整理外篇——————状态栏[ 四]
【微处理器】基于FPGA的微处理器VHDL开发
【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真
大专学历,33岁宝妈又怎样?我照样销售转测试,月入13k+
Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
[FPGA tutorial case 9] design and implementation of clock manager based on vivado core
Postman automatically fills headers
Global and Chinese market of optical densitometers 2022-2028: Research Report on technology, participants, trends, market size and share
Are you still writing the TS type code
Open3d uses GICP to register point clouds
【FPGA教程案例9】基于vivado核的时钟管理器设计与实现
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
I was beaten by the interviewer because I didn't understand the sorting
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #