当前位置:网站首页>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 ~
边栏推荐
- Game 280 of leetcode week
- Robley's global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
- Playwright recording
- Introduction to redis (1)
- Intel sapphire rapids SP Zhiqiang es processor cache memory split exposure
- Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
- College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
- 107. SAP UI5 OverflowToolbar 容器控件以及 resize 事件处理的一些细节介绍
- 【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
- 视频网站手绘
猜你喜欢
LeetCode周赛 + AcWing周赛(T4/T3)分析对比
[wave modeling 2] three dimensional wave modeling and wave generator modeling matlab simulation
[pure tone hearing test] pure tone hearing test system based on MATLAB
“薪資倒掛”、“畢業生平替” 這些現象說明測試行業已經...
107. SAP UI5 OverflowToolbar 容器控件以及 resize 事件处理的一些细节介绍
[wave modeling 3] three dimensional random real wave modeling and wave generator modeling matlab simulation
JS implementation determines whether the point is within the polygon range
The performance of major mainstream programming languages is PK, and the results are unexpected
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Nebula Importer 数据导入实践
随机推荐
Game 280 of leetcode week
Les phénomènes de « salaire inversé » et de « remplacement des diplômés » indiquent que l'industrie des tests a...
微信小程序:独立后台带分销功能月老办事处交友盲盒
Four pits in reentrantlock!
Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
Innovation leads the direction. Huawei Smart Life launches new products in the whole scene
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
Global and Chinese market of optical densitometers 2022-2028: Research Report on technology, participants, trends, market size and share
“薪資倒掛”、“畢業生平替” 這些現象說明測試行業已經...
Basic operation of database and table ----- the concept of index
26.2 billion! These universities in Guangdong Province have received heavy support
Are you still writing the TS type code
SAP ui5 application development tutorial 107 - trial version of SAP ui5 overflow toolbar container control introduction
BGP comprehensive experiment
Maximum number of "balloons"
Expose testing outsourcing companies. You may have heard such a voice about outsourcing
Redis(1)之Redis简介
Call Huawei order service to verify the purchase token interface and return connection reset
当产业互联网时代真正发展完善之后,将会在每一个场景见证巨头的诞生
SAP UI5 应用开发教程之一百零七 - SAP UI5 OverflowToolbar 容器控件介绍的试读版