HDU1518 Square 【剪枝】
来源:互联网 发布:淘宝让你提供质检报告 编辑:程序博客网 时间:2024/06/08 07:23
Square
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8900 Accepted Submission(s): 2893
Problem Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
Sample Input
34 1 1 1 15 10 20 30 40 508 1 7 2 6 4 4 3 5
Sample Output
yesnoyes
Source
University of Waterloo Local Contest 2002.09.21
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#define maxn 22int L[maxn], n, tar, times;bool vis[maxn], ok;bool DFS(int k, int leftLen) { if(!leftLen) { if(++times == 4) return true; for(int i = 1; i < n; ++i) { if(!vis[i]) { vis[i] = 1; if(DFS(i + 1, tar - L[i])) return true; else { --times; vis[i] = 0; return false; } } } } int i; for(i = k; i < n; ++i) { if(!vis[i] && L[i] <= leftLen) { vis[i] = 1; if(L[i-1] == L[i] && !vis[i-1]) { vis[i] = 0; continue; } if(DFS(i+1, leftLen - L[i])) return true; vis[i] = 0; } } return false;}int main() { int t, i; scanf("%d", &t); while(t--) { scanf("%d", &n); tar = 0; for(i = 0; i < n; ++i) { scanf("%d", &L[i]); vis[i] = 0; tar += L[i]; } if(tar % 4) { printf("no\n"); continue; } tar /= 4; std::sort(L, L + n, std::greater<int>()); if(L[0] > tar) { printf("no\n"); continue; } times = 0; vis[0] = 1; DFS(1, tar - L[0]); printf(times == 4 ? "yes\n" : "no\n"); } return 0;}
0 0
- hdu1518(DFS+剪枝 Square )
- HDU1518 Square 【剪枝】
- hdu1518(Square)深搜+剪枝
- hdu1518 Square | ZOJ1909 | POJ2362 (dfs + 剪枝)
- hdu1518 Square
- HDU1518 Square
- hdu1518 Square
- HDU1518 Square
- Hdu1518 Square
- HDU1518-Square
- hdu1518 Square
- hdu1518 Square
- HDU1518-Square
- ZOJ1909 POJ2362 HDU1518 Square,很经典的DFS+剪枝!
- HDU1518 & POJ2362 & ZOJ1909 Square(DFS,剪枝是关键呀)
- DFS:HDU1518-Square(剪枝较多的DFS)
- HDU1518:Square(DFS)
- HDU1518 & POJ2362 & ZOJ1909 Square
- 普朗克交回拟好嘎多晒太阳
- JDK中rt.jar、tools.jar和dt.jar作用
- listview点击一行高亮
- 关于技术学习的一点思考
- 排序字符串的前缀压缩算法,结合lucene
- HDU1518 Square 【剪枝】
- hdu 5093 Battle ships(最大流)
- 栈和队列
- HDU 5099 Comparison of Android versions(字符串)
- [LeetCode OJ]Populating Next Right Pointers in Each Node
- Android项目-高考作文功能简介(一)
- hdu 5090 Game with Pearls (二分匹配)
- MDCC 2014移动开发者大会 参展商活动的小礼品
- GitHub“最受欢迎的开源项目” TOP 50 android开源项目 直接拿来用