当前位置:网站首页>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 ~
边栏推荐
- Arbitrum:二维费用
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验4 蜂鸣器+马达实验【按键外部中断】(学习笔记)
- JS implementation determines whether the point is within the polygon range
- Daily question brushing record (13)
- Expansion operator: the family is so separated
- Redis(1)之Redis简介
- Inventory of more than 17 typical security incidents in January 2022
- Huawei employs millions of data governance experts! The 100 billion market behind it deserves attention
- Detailed explanation of multi-mode input event distribution mechanism
- The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
猜你喜欢

【海浪建模1】海浪建模的理论分析和matlab仿真

Wechat applet; Gibberish generator

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

整理混乱的头文件,我用include what you use

Les phénomènes de « salaire inversé » et de « remplacement des diplômés » indiquent que l'industrie des tests a...

JS implementation determines whether the point is within the polygon range

微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益

多模输入事件分发机制详解

Basic operations of database and table ----- delete index

微信小程序:全新独立后台月老办事处一元交友盲盒
随机推荐
Maximum number of "balloons"
整理混乱的头文件,我用include what you use
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
dotnet-exec 0.6.0 released
[CTF] AWDP summary (WEB)
Basic operation of database and table ----- phased test II
Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Game 280 of leetcode week
FEG founder rox:smartdefi will be the benchmark of the entire decentralized financial market
A simple SSO unified login design
Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!
微信小程序;胡言乱语生成器
那些一门心思研究自动化测试的人,最后都怎样了?
Arbitrum: two-dimensional cost
I was beaten by the interviewer because I didn't understand the sorting
Nebula Importer 数据导入实践
【CTF】AWDP总结(Web)
Database postragesql client authentication
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
Ruby tutorial