POJ2362 Square

题目描述:给定一系列枝条,判断他们是否可以收尾相连接成一个正方形。

样例输入:

第一行是样例个数N,以下各n行是的第一个数M是这个样例的枝条数,然后后面跟着各个枝条的长度。

限制条件:

4<=M<=20

每个枝条的长度between 1 and 10,000。

样例i/o:

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

思路:dfs,也是我目前做过的最难的一道dfs,思路都显然知道:深搜找到4组边即可。主要是代码实现比较难。因为没接触过四组和标记的消除问题。

上c++代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int map[21]={0},stick[21],l,sum,N;
bool dfs(int n,int s,int len)
{
	if (n==3) return true;                    //不需要找到四组,三组已找到,四组必然。
	for(int i=s;i>=0;i--)
	{
		if(!map[i])
		{
			map[i]=1;
			if(len+stick[i]<l&&dfs(n,i-1,len+stick[i]))
				return true;
			if(len+stick[i]==l&&dfs(n+1,N-1,0))
				return true;
			else map[i]=0;                          //标记的实现和消除是难点。
		}

	}
	return false;
}

int main()
{
	int _;
	scanf("%d",&_);
	while(_--)
	{
		sum=0;
		memset(map,0,sizeof(map));
		memset(stick,0,sizeof(stick));
		scanf("%d",&N);
		for(int i=0;i<N;i++) {
			scanf("%d",&stick[i]);
			sum+=stick[i];

		}
		l=sum/4;
		sort(stick,stick+N);
		if(l*4!=sum||stick[N-1]>l)
<span style="white-space:pre">						</span>//条件可以写成 if(l*4!=sum) 时间会从73ms升到二百多ms,但是去掉l*4!=sum这个<span style="white-space:pre">							</span>条件 以后就会wa。因为只可能是l*4<sum,所以其中有可能有一个或者几个树棍没计在内。
		{
			printf("no\n");
		}

		else if(dfs(0,N-1,0)) printf("yes\n");
		else printf("no\n");

	}
	return 0;
}

本题的剪枝也是很多的。

上大仙的剪枝代码(0ms运行):

/*
 * 剪枝
 * 1:树枝的长度和为4的倍数
 * 2:树枝的长度和除以4要大于等于最长的树枝的长度
 * 3:将树枝从大到小排序
 * 5:如果当前树枝不能被选取,则与当前树枝等长的树枝也不能被选取
 * 6:如果以当前最长的没有被选取的树枝开始拼一条新边却失败了,则不可能成功。       //好剪枝。
 */
/*************************************************************************
 > File Name: 2362.cpp
 > Author: UnknownCUnknown
 > Mail: [email protected]
 > Created Time: 二 12/16 00:08:45 2014
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <string>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <climits>
using namespace std;
bool vis[25];
int len[25];
int n;
int sum,side;
int Max=0;
bool flag1;
void dfs(int le,int now,int num){
    if(le==side) {
        ++num;
        now=0;
        le=0;
    }
    if(num==3){
        flag1=true;
    }
    if(le>side){
        return;
    }
    if(flag1) return;
    for(int i=now;i<n;++i){
      //  if(flag1) return;
        if(i&&!vis[i-1]&&len[i]==len[i-1]) continue;//剪枝5
        if(!vis[i]){
            vis[i]=true;
            dfs(le+len[i],i+1,num);
      //      if(flag1) return;
            vis[i]=false;
            if(le==0) return;
        }
    }
}
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        sum=0;
        Max=0;
        for(int i=0;i<n;++i){
            scanf("%d",len+i);
            if(len[i]>Max) Max=len[i];
            sum+=len[i];
        }
        if(sum%4!=0){//剪枝1
            printf("no\n");
            continue;
        }
        side=sum/4;
        if(Max>side) {//剪枝2
            printf("no\n");
            continue;
        }
        sort(len,len+n,cmp);//剪枝3
        flag1=false;
        memset(vis,false,sizeof(vis));
        dfs(0, 0,0);
        if(!flag1) printf("no\n");
        else printf("yes\n");
    }
    return 0;
}
时间: 2024-08-02 20:50:44

POJ2362 Square的相关文章

n个数的全排列的相关问题

问题类型:需要进行n个数的全排列再进行其他操作O(2^n) c++代码实现: #include<iostream> using namespace std; int perm[11],pos,n; bool mapp[11]={0}; void perm1(int pos,int n) { if(pos==n+1) { for(int k=1;k<=n;k++) printf("%2d ",perm[k]); putchar('\n'); <span style

[leetcode] 367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 使用二分查找寻找input

Project Euler 92:Square digit chains C++

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

LeetCode Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 思路分析:这题考察DP.缓存中间结果降低反复计算.DP方程为 if(matrix[i][j

Theatre Square 题解代码

Theatre Square CodeForces - 1A 水题水题... 最开始的程序是这个,可是AC不了我也不知道为什么......QAQ... #include <stdio.h>#include<stdlib.h>int main(){    int m,n,a,ans,tmp,k,h,tep,i,j;    scanf("%d %d %d",&m,&n,&a);    if(m%a==0)    {        if(n%a

[LeetCode] Matchsticks to Square 火柴棍组成正方形

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, a

codeforces 710C C. Magic Odd Square(构造)

题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the

hdu1518(Square)深搜+剪枝

点击打开杭电1518 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,

poj 1084 Square Destroyer dlx解重复覆盖

分析: 将问题转化为重复覆盖问题,DancingLink解决. 代码: //poj 1084 //sep9 #include <iostream> using namespace std; const int maxN=10024; const int maxL=128; int L[maxN],R[maxN],U[maxN],D[maxN]; int C[maxN],H[maxN]; int S[maxN],A[maxN],X[maxN]; bool makeup[maxL][maxL];