当前位置:网站首页>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 ~
边栏推荐
- SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
- Digital DP template
- 小程序直播 + 电商,想做新零售电商就用它吧!
- Database postragesq PAM authentication
- 各大主流编程语言性能PK,结果出乎意料
- Playwright recording
- FEG founder rox:smartdefi will be the benchmark of the entire decentralized financial market
- Discrete mathematics: propositional symbolization of predicate logic
- 当产业互联网时代真正发展完善之后,将会在每一个场景见证巨头的诞生
- Armv8-a programming guide MMU (3)
猜你喜欢
Detailed explanation of multi-mode input event distribution mechanism
Pycharm professional download and installation tutorial
小程序直播 + 电商,想做新零售电商就用它吧!
Pandora IOT development board learning (RT thread) - Experiment 4 buzzer + motor experiment [key external interrupt] (learning notes)
POAP:NFT的采用入口?
Chia Tai International Futures: what is the master account and how to open it?
Senior Test / development programmers write no bugs? Qualifications (shackles) don't be afraid of mistakes
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
26.2 billion! These universities in Guangdong Province have received heavy support
"Upside down salary", "equal replacement of graduates" these phenomena show that the testing industry has
随机推荐
142. Circular linked list II
Roads and routes -- dfs+topsort+dijkstra+ mapping
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
Redis(1)之Redis简介
[Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
DOM basic syntax
Pycharm professional download and installation tutorial
Expose testing outsourcing companies. You may have heard such a voice about outsourcing
Hedhat firewall
Check if this is null - checking if this is null
Basic operations of database and table ----- delete index
La jeunesse sans rancune de Xi Murong
dotnet-exec 0.6.0 released
Take you ten days to easily complete the go micro service series (IX. link tracking)
What you learned in the eleventh week
College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
【CTF】AWDP总结(Web)
Complex, complicated and numerous: illustration of seven types of code coupling
Armv8-a programming guide MMU (3)