当前位置:网站首页>[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;
}边栏推荐
- 使用cpolar建立一个商业网站(1)
- Blue Bridge Cup real question: one question with clear code, master three codes
- Maixll dock camera usage
- Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
- STM32+HC05串口蓝牙设计简易的蓝牙音箱
- AFNetworking框架_上传文件或图像server
- 测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
- Online notes
- Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
- Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
猜你喜欢

287. Find duplicates

There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
![Noninvasive and cuff free blood pressure measurement for telemedicine [translation]](/img/56/8deaec18cd9f2cf49ff234b09b1283.png)
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]

【LeetCode第 300 场周赛】

Jushan database was among the first batch of financial information innovation solutions!

Introduction to the use of SAP Fiori application index tool and SAP Fiori tools

Breadth first traversal of graph

Nuc11 cheetah Canyon setting U disk startup

ORACLE进阶(四)表连接讲解

【中山大学】考研初试复试资料分享
随机推荐
Docker installation redis
DOM简要
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
小程序在产业互联网中的作用
epoll()无论涉及wait队列分析
Afnetworking framework_ Upload file or image server
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
Splay
About NPM install error 1
用于远程医疗的无创、无袖带血压测量【翻译】
287. 寻找重复数
Alibaba cloud international ECS cannot log in to the pagoda panel console
复现Thinkphp 2.x 任意代码执行漏洞
Deep circulation network long-term blood pressure prediction [translation]
青龙面板最近的库
C#/VB.NET 给PDF文档添加文本/图像水印
node の SQLite
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
First, look at K, an ugly number