当前位置:网站首页>Leetcode 473. 火柴拼正方形 [暴力+剪枝]
Leetcode 473. 火柴拼正方形 [暴力+剪枝]
2022-06-13 02:17:00 【JackDawn!?】
题目

题目大意及思路
题目的意思就是给定一个数组,要求判断能否将数组中的数字平均分为4份。先初始化四个边长度为0,依次尝试将一个火柴添加到其中一条边上。剪枝的方法写在注释里。
代码
class Solution {
int sum;
bool check(int l1, int l2, int l3, int l4, int cur, vector<int>& vec, int left) {
if(l1 > sum || l2 > sum || l3 > sum || l4 > sum) return false; // 某一条边大于总数的1/4, 提前返回false
if(l1+left < sum || l2+left < sum || l3+left<sum || l4+left<sum) return false; // 某一条边加上剩下的所有火柴仍达不到总数的1/4, 提前返回false
if(cur == vec.size()) {
// 达到排列树的根
if( l1 == l2 && l2 == l3 && l3 == l4) {
return true; }
else return false;
}
int l = left-vec[cur];
return (
check(l1+vec[cur], l2, l3, l4, cur+1, vec, l) ||
check(l1, l2+vec[cur], l3, l4, cur+1, vec, l) ||
check(l1, l2, l3, l4+vec[cur], cur+1, vec, l) ||
check(l1, l2, l3+vec[cur], l4, cur+1, vec, l)
);
}
static bool cmp(const int& a, const int& b) {
return a > b;
}
public:
// 判断能否四等分
bool makesquare(vector<int>& matchsticks) {
sum = 0;
for(auto i : matchsticks) sum += i;
sum /= 4;
sort(matchsticks.begin(), matchsticks.end(), this->cmp); // 先分配大的火柴, 有利于剪枝
return check(0, 0, 0, 0, 0, matchsticks, sum*4);
}
};
结果

边栏推荐
- STM32F103 IIC OLED program migration complete engineering code
- 记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
- Chapter7-11_ Deep Learning for Question Answering (2/2)
- 4.11 introduction to firmware image package
- Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
- Chapter7-10_ Deep Learning for Question Answering (1/2)
- cmake_ example
- Sensorless / inductive manufacturing of brushless motor drive board based on stm32
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- [work with notes] MFC solves the problem that pressing ESC and enter will automatically exit
猜你喜欢

Build MySQL environment under mac
![[learning notes] xr872 GUI littlevgl 8.0 migration (display part)](/img/5e/fc8c3fe3029c36648fbc3f48bc0c2f.jpg)
[learning notes] xr872 GUI littlevgl 8.0 migration (display part)

Huawei equipment is configured with IP and virtual private network hybrid FRR

【 unity】 Problems Encountered in Packaging webgl Project and their resolution Records

Sensorless / inductive manufacturing of brushless motor drive board based on stm32

Ctrip reshapes new Ctrip

4.11 introduction to firmware image package
![[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)](/img/f7/b2463d8ffe75113d352cae332046db.jpg)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)

Huawei equipment is configured with CE dual attribution

Gadgets: color based video and image cutting
随机推荐
Application circuit and understanding of BAT54C as power supply protection
Yovo3 and yovo3 tiny structure diagram
柏瑞凯电子冲刺科创板:拟募资3.6亿 汪斌华夫妇为大股东
STM32 sensorless brushless motor drive
4.11 introduction to firmware image package
华为设备配置双反射器优化虚拟专用网骨干层
C language conditional compilation routine
16 embedded C language interview questions (Classic)
Gadgets: color based video and image cutting
STM32 timer interrupt learning notes
Share three stories about CMDB
Is space time attention all you need for video understanding?
What are the differences in cache/tlb?
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
Why is Huawei matebook x Pro 2022 leading a "laptop" revolution
[keras] generator for 3D u-net source code analysis py
Application and routine of C language typedef struct
Mbedtls migration experience
Cumulative tax law: calculate how much tax you have paid in a year
Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation