当前位置:网站首页>[depth first search] Ji suanke: Square
[depth first search] Ji suanke: Square
2022-07-06 18:46:00 【muse_ age】
Suppose the sides of the square are a ,b,c ,d, Each stick Yes 4 A choice : You can join abcd Any one , You can use dfs
dfs(int index,int a,int b,int c,int d)// Enumerate to index And the current side length is abcd
To make a choice
dfs(index+1,a+p[index],b,c,d;// Join in a
dfs(index+1,a,b+p[index],c,d);// Join in b
dfs(index+1,a,b,c+p[index],d);// Join in c
dfs(index+1,a,b,c,d+p[index]);// Join in d
exit :
index==n: Reach the empty node in the search tree , Judge abcd Whether the side lengths are equal
prune :
When side length > Chief, /4 when , It is impossible to form a square , There is no need to search , cut off
Code :
#include<iostream>
using namespace std;
int n;
int p[1000];
int sum=0;
bool flag=false;
void dfs(int index,int a,int b,int c,int d){
if(flag){
return;
}
if(index==n){
if(a==b&&b==c&&c==d&&d==sum/4){
cout<<"YES";
flag=true;
return;
}
}
if(a>sum/4||b>sum/4||c>sum/4||d>sum/4){
return;
}
dfs(index+1,a+p[index],b,c,d);
dfs(index+1,a,b+p[index],c,d);
dfs(index+1,a,b,c+p[index],d);
dfs(index+1,a,b,c,d+p[index]);
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>p[i];
sum+=p[i];
}
if(sum%4!=0){
cout<<"NO";
return 0;
}
else{
dfs(0,0,0,0,0);
}
return 0;
}
边栏推荐
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- Huawei 0 foundation - image sorting
- STM32+ENC28J60+UIP协议栈实现WEB服务器示例
- From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
- 爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
- 使用block实现两个页面之间的传统价值观
- Alibaba cloud international ECS cannot log in to the pagoda panel console
- Cobra quick start - designed for command line programs
- 44所高校入选!分布式智能计算项目名单公示
- UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
猜你喜欢
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
[the 300th weekly match of leetcode]
手写一个的在线聊天系统(原理篇1)
44所高校入选!分布式智能计算项目名单公示
On AAE
10、 Process management
[Matlab] Simulink 同一模块的输入输出的变量不能同名
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
C#/VB.NET 给PDF文档添加文本/图像水印
Maixll dock camera usage
随机推荐
解读云原生技术
Specify flume introduction, installation and configuration
青龙面板最近的库
使用block实现两个页面之间的传统价值观
Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
C language college laboratory reservation registration system
SQL injection - access injection, access offset injection
287. 寻找重复数
Collection of penetration test information -- use with nmap and other tools
How does crmeb mall system help marketing?
Deep circulation network long-term blood pressure prediction [translation]
Huawei 0 foundation - image sorting
Cocos2d Lua 越来越小样本 内存游戏
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
POJ 2208 已知边四面体六个长度,计算体积
SQL injection Foundation
bonecp使用数据源
test about BinaryTree
Docker installation redis
Crawling data encounters single point login problem