当前位置:网站首页>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:二维费用
- Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
- Basic operations of database and table ----- delete index
- C basic knowledge review (Part 3 of 4)
- Ruby tutorial
- What if the programmer's SQL data script coding ability is weak and Bi can't do it?
- 当产业互联网时代真正发展完善之后,将会在每一个场景见证巨头的诞生
- 微信小程序:全新独立后台月老办事处一元交友盲盒
- 26.2 billion! These universities in Guangdong Province have received heavy support
猜你喜欢
【纯音听力测试】基于MATLAB的纯音听力测试系统
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Arbitrum:二维费用
【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
Game 280 of leetcode week
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
dotnet-exec 0.6.0 released
Huawei employs millions of data governance experts! The 100 billion market behind it deserves attention
ROS command line tool
资深测试/开发程序员写下无bug?资历(枷锁)不要惧怕错误......
随机推荐
Redis(1)之Redis简介
College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
Intel sapphire rapids SP Zhiqiang es processor cache memory split exposure
Global and Chinese markets of emergency rescue vessels (errv) 2022-2028: Research Report on technology, participants, trends, market size and share
Single step debugging of master data reading of SAP commerce cloud products
Discrete mathematics: propositional symbolization of predicate logic
Analysis and comparison of leetcode weekly race + acwing weekly race (t4/t3)
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
Discrete mathematics: reasoning rules
Database postragesql lock management
Expose testing outsourcing companies. You may have heard such a voice about outsourcing
微信小程序:全网独家小程序版本独立微信社群人脉
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
無心劍英譯席慕容《無怨的青春》
Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
Call Huawei order service to verify the purchase token interface and return connection reset
微信小程序:全新独立后台月老办事处一元交友盲盒
抓包整理外篇——————状态栏[ 四]