Square POJ - 2362

6人阅读 评论(0) 收藏 举报
分类:

Square

 POJ - 2362 

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
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
Sample Output
yes
no
yes
题解+解析:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#define INF 0x7f7f7f7f
typedef long long ll;
using namespace std;
int n;
int m[25];
bool visit[25];
int sum, mid;
int flag;
void dfs(int num, int len, int s)//num为凑好的边的个数,len为现在在凑的木棍长度,s代表起点位置
{
	if (flag)//
	{
		return;
	}
	if (num == 4)
	{
		flag = true;
		return;
	}
	if (len == mid)
	{
		dfs(num+1, 0, 0);//符合边长,深搜下一条边
	}
	for (int i = s; i < n; i++)
	{
		if (!visit[i]&&len+m[i]<=mid)//不搜重复和不符合条件的
		{
			visit[i] = true;
			dfs(num, len+m[i], i+1);//符合条件就搜一下
			visit[i] = false;//回溯
		}
	}
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		sum = 0;
		flag = false;
		memset(visit, 0, sizeof(visit));
		scanf("%d", &n);
		for (int i =0; i < n; i++)
		{
			scanf("%d",&m[i]);
			sum += m[i];
		}
		sort(m,m+n);//排序
		mid = sum/4;
		if(mid*4!=sum)//如果mid不是整数,直接输出no
		{
			printf("no\n");
			continue;
		}
		if (m[n-1]>mid)//如果最长的棍大于边长,直接输出no
		{
			printf("no\n");
			continue;
		}
		dfs(0,0,0);
		if (flag)
		{
			printf("yes\n");
		}
		else
		{
			printf("no\n");
		}
	}
	return 0;
}


查看评论

POJ2362:Square(DFS)

Description Given a set of sticks of various lengths, is it possible to join them end-to-end to for...
  • libin56842
  • libin56842
  • 2013-11-30 21:52:41
  • 3105

POJ2362-Square

转载请注明出处:優YoU    http://user.qzone.qq.com/289065406/blog/1311605012 大致题意: 给定一堆不定长度的小棒子,问他们能否构成一个正...
  • lyy289065406
  • lyy289065406
  • 2011-07-31 10:26:06
  • 3146

POJ 2362 Square(DFS回溯构造)

POJ2362 Square(DFS回溯构造) http://poj.org/problem?id=2362 题意:给你M根长度可能不同的棍子,问你用着M根棍子能否拼成一个正方形. 分析:首先将M根棍...
  • u013480600
  • u013480600
  • 2014-05-29 22:24:47
  • 1115

TOJ 1398 POJ 2362 Square DFS

优化了很久,最后发现只要在普通暴力DFS里加一个参数即可避免超时的命运…… 最后在TOJ运行时间0'00.00", POJ是16ms 我的代码用到的思路有: 1. 所有小棍的和要能够...
  • yueashuxia
  • yueashuxia
  • 2012-01-12 22:28:45
  • 731

HDU1518 & POJ2362 & ZOJ1909 Square

DFS+剪枝
  • wangwenhao00
  • wangwenhao00
  • 2014-04-17 11:32:16
  • 940

POJ 2362 Square [DFS]

DFS + 剪枝 一定要加上最长的那个边比s/4小 不然就WA 不知为何?#include #include #include #include using namespace std;...
  • YYecust
  • YYecust
  • 2016-07-28 19:08:31
  • 150

poj2362Square

Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21988   Accept...
  • z8110
  • z8110
  • 2015-08-26 14:56:02
  • 624

POJ 2362 Square dfs

来源:http://poj.org/problem?id=2362 题意:给你一些木棍,判断能不能用这些木棍围城一个正方形。注意是所有的木棍都要用到,且每个木棍只能用一次。 思路:数据范围不大,n...
  • wmn_wmn
  • wmn_wmn
  • 2012-08-15 10:13:43
  • 1060

dfs Square poj 2362

Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20988   Accept...
  • chaoyueziji123
  • chaoyueziji123
  • 2015-04-02 19:37:12
  • 410
    个人资料
    持之以恒
    等级:
    访问量: 3096
    积分: 688
    排名: 8万+
    文章分类
    文章存档
    最新评论