当前位置:网站首页>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 ~
边栏推荐
- Paxos 入门
- Global and Chinese markets for stratospheric UAV payloads 2022-2028: Research Report on technology, participants, trends, market size and share
- Wechat applet; Gibberish generator
- La jeunesse sans rancune de Xi Murong
- SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
- [pure tone hearing test] pure tone hearing test system based on MATLAB
- 19. Delete the penultimate node of the linked list
- 抓包整理外篇——————状态栏[ 四]
- What happened to those who focused on automated testing?
- Basic operation of database and table ----- phased test II
猜你喜欢
Basic operation of database and table ----- phased test II
JS implementation determines whether the point is within the polygon range
Pandora IOT development board learning (RT thread) - Experiment 4 buzzer + motor experiment [key external interrupt] (learning notes)
26.2 billion! These universities in Guangdong Province have received heavy support
【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
The performance of major mainstream programming languages is PK, and the results are unexpected
【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真
Hedhat firewall
107. Some details of SAP ui5 overflow toolbar container control and resize event processing
随机推荐
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Les phénomènes de « salaire inversé » et de « remplacement des diplômés » indiquent que l'industrie des tests a...
Introduction to the gtid mode of MySQL master-slave replication
多模输入事件分发机制详解
The most complete regular practical guide of the whole network. You're welcome to take it away
User login function: simple but difficult
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
[untitled]
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
I was beaten by the interviewer because I didn't understand the sorting
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
SAP UI5 应用开发教程之一百零七 - SAP UI5 OverflowToolbar 容器控件介绍的试读版
Ruby tutorial
无心剑英译席慕容《无怨的青春》
Armv8-a programming guide MMU (3)
What you learned in the eleventh week
Senior Test / development programmers write no bugs? Qualifications (shackles) don't be afraid of mistakes
Daily question brushing record (13)