POJ 2362 Square
题意:给n个木棍,问能不能正好拼成一个正方形。
解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; int n;
int stick[25];
bool vis[25];
bool dfs(int len, int remlen, int pos, int num)
{
if(remlen == 0)
{
if(num == 3) return true;
return dfs(len, len, 0, num + 1);
}
for(int i = pos; i < n; i++)
{
if(i > 0 && !vis[i - 1] && stick[i] == stick[i - 1]) continue;
if(!vis[i] && remlen >= stick[i])
{
vis[i] = true;
if(dfs(len, remlen - stick[i], i + 1, num)) return true;
vis[i] = false;
}
if(!vis[i] && i == 0) return false;
}
return false;
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(vis, 0, sizeof vis);
scanf("%d", &n);
int sum = 0;
int maxn = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &stick[i]);
sum += stick[i];
maxn = max(maxn, stick[i]);
}
sort(stick, stick + n, cmp);
if(sum % 4 != 0 || maxn > sum / 4 || n < 4)
{
puts("no");
continue;
}
if(dfs(sum / 4, sum / 4, 0, 1)) puts("yes");
else puts("no");
}
return 0;
}
POJ 2362 Square的更多相关文章
- DFS POJ 2362 Square
题目传送门 /* DFS:问能否用小棍子组成一个正方形 剪枝有3:长的不灵活,先考虑:若根本构不成正方形,直接no:若第一根比边长长,no 这题是POJ_1011的精简版:) */ #include ...
- POJ 2362 Square DFS
传送门:http://poj.org/problem?id=2362 题目大意: 给一些不同长度的棍棒,问是否可能组成正方形. 学习了写得很好的dfs 赶紧去玩博饼了.....晚上三个地方有约.... ...
- POJ 2362:Square 觉得这才算深度搜索
Square Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21821 Accepted: 7624 Descripti ...
- poj 2362:square
题目大意:给你T组数据,每组数据有n个棍子,问你能不能用这些棍子拼成一个正方形(所有都要用上,而且不能截断棍子). Sample Input 34 1 1 1 15 10 20 30 40 508 1 ...
- POJ 1099 Square Ice
Square Ice Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxyg ...
- (中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。
Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...
- POJ 1099 Square Ice 连蒙带猜+根据样例找规律
目录 题面 思路 思路 AC代码 题面 Square Ice Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4526 A ...
- poj 2362
回溯加剪枝 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...
- [DLX反复覆盖] poj 1084 Square Destroyer
题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...
随机推荐
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- the-type-java-lang-charsequence-cannot-be-resolved-in-package-declaration
http://stackoverflow.com/questions/24301986/the-type-java-lang-charsequence-cannot-be-resolved-in-pa ...
- HUSTOJ(转发)
来源:http://blog.csdn.net/xiajian2010/article/details/12954855 缘起 大四了,快毕业了,所以想准备点LAMP的知识和经验.刚好实验室里有人在搞 ...
- 包含中文的字符串中截取前N个字符
package com.wangzhu.string; import java.io.UnsupportedEncodingException; public class SubStringDemo1 ...
- Python list去重及找出,统计重复项
http://bbs.chinaunix.net/thread-1680208-1-1.html 如何找出 python list 中有重复的项 http://www.cnblogs.com/feis ...
- mysql字段的适当冗余有利于提高查询速度
CREATE TABLE `comment` ( `c_id` int(11) NOT NULL auto_increment COMMENT '评论ID', `u_id` int(11) NOT ...
- HTML5入门十一---Canvas画布实现画图(二)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- HTML5入门2---js获取HTML元素的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- python自省指南
深入python中对自省的定义: python的众多强大功能之一,自省,正如你所知道的,python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其他模块和函数,获取它们的信息,并对它们进行 ...
- PCB走线角度选择 — PCB Layout 跳坑指南
现在但凡打开SoC原厂的PCB Layout Guide,都会提及到高速信号的走线的拐角角度问题,都会说高速信号不要以直角走线,要以45度角走线,并且会说走圆弧会比45度拐角更好.狮屎是不是这样?PC ...