#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int len[20],sum,M;
bool flag,f[20];//flag标记是否找到
void dfs(int d,int stick,int nowlen)
{
    //i:正在处理的小棍,stick:已经拼好的边框数,nowlen:当前正在拼的框的长度
    int j;
    if(flag||stick>2)
	{
        if(stick>2)
			flag=1;//已经拼好3个框,第四个不用拼了
        return ;
    }
    else
	{
        for(j=d;j<M;j++)
		{
            if((!f[j])&&nowlen+len[j]<=sum)
			{
                f[j]=1;//标记
                if(nowlen+len[j]==sum)
					dfs(0,stick+1,0);//拼好了一个,继续拼下一个
                else
					dfs(j+1,stick,nowlen+len[j]);//继续拼当前的
                f[j]=0;//恢复标记
            }
        }
    }
}
int main()
{
    int ca,i,j;
    scanf("%d",&ca);
    while(ca--)
	{
        scanf("%d",&M);
        for(i=sum=0;i<M;i++)
		{
            scanf("%d",&len[i]);
            sum+=len[i];
        }
        if(sum%4!=0)
		{
			printf("no\n");
			continue;
		}
        sum/=4;
        for(int i=0;i<M;i++)
			for(int j=i+1;j<M;j++)
			{
				if(len[i]<len[j])
				{
					int t=len[i];
					len[i]=len[j];
					len[j]=t;
				}
			}
        if(len[0]>sum)
		{
			printf("no\n");
			continue;
		}
        flag=0;
        dfs(0,0,0);
        printf("%s\n",flag?"yes":"no");
    }
    //system("pause");
    return 0;
}

  

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

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.

For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".

Sample Input

3
#include <stdio.h>
int a[20];
int sum=0;
int b[20]={0};
int n;
int flag=0;
void fun(int step,int num,int toile)
{
	if(num>2)
	{
		flag=1;
	}

	for(int i=step;i<n;i++)
	{
		if((!b[i])&&(toile+a[i]<=sum))
		{
			b[i]=1;
			if(toile+a[i]==sum)
			{
				fun(0,num+1,0);
			}
			else
			{
				fun(i+1,num,toile+a[i]);
			}

			b[i]=0;

		}
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		sum=sum+a[i];
	}

	if(sum%4==0)
	{
		sum=sum/4;
		fun(0,0,0);
	}

	else
	{
		printf("NO");
	}
	if(flag==1)
		printf("YES");
	return 1;
}
#include <stdio.h> int a[20]; int sum=0; int b[20]={0}; int n; int flag=0; void fun(int step,int num,int toile) { if(num>2) { flag=1; } for(int i=step;i<n;i++) { if((!b[i])&&(toile+a[i]<=sum)) { b[i]=1; if(toile+a[i]==sum) { fun(0,num+1,0); } else { fun(i+1,num,toile+a[i]); } b[i]=0; } } } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum=sum+a[i]; } //先排序 for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { if(a[i]>a[j]) { int t=a[i]; a[i]=a[j]; a[j]=t; } } if(sum%4==0) { sum=sum/4; fun(0,0,0); } else { printf("NO"); } if(flag==1) printf("YES"); return 1; }
4 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5

Output for Sample Input

yes
no
yes

TOJ1398正方形的编成 或者 POJ2362的更多相关文章

  1. poj2362 Square(DFS)

    题目链接 http://poj.org/problem?id=2362 题意 输入n根棍子的长度,求这n根棍子是否能组成一个正方形. 思路 假设能组成正方形,则正方形的周长为sum,sum/4为正方形 ...

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

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  3. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  4. 【纯css】左图右文列表,左图外框宽度占一定百分比的正方形,右上下固定,右中自动响应高度。支持不规则图片。

    查看演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  5. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  6. 洛谷 P1387 最大正方形 Label:奇怪的解法

    题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...

  7. H5一行显示两个正方形

    1)有时候一些图片会是正方形或者长方形,对于这样的图片一般都是居中显示到正方体内,代码如下:  .exhibition_list img{width:100%;position: relative;t ...

  8. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  9. for 循环 正方形

    <?php//================================正方形//for($q = 1; $q <= 5; $q ++ ){//    for($z =1; $z & ...

随机推荐

  1. C# 查找其他应用程序并打开、显示、隐藏、关闭

    软件开发中,有时迫不得已要用到第三方的软件,这时就涉及到在C#应用程序需要对第三方软件打开.显示.隐藏以及关闭. 下面列举了几个常用的方式 打开应用程序,下面是2种简单用法: 第一种: public ...

  2. MyEclipse 配置Android环境

    eclipse https://www.eclipse.org/downloads/download.php?file=/oomph/epp/mars/R2/eclipse-inst-win64.ex ...

  3. VS2015 C#6.0 中的没有实现/支持的特性

      VS2015 C#6.0 中的没有实现/支持的特性   .数组增强:赋值 维数组 Int[] numbers: numbers = {2,3,4,5}; 维数组 Int[,] numbers2; ...

  4. 大楼轮廓 · building-outline

    [抄题]: 水平面上有 N 座大楼,每座大楼都是矩阵的形状,可以用一个三元组表示 (start, end, height),分别代表其在x轴上的起点,终点和高度.大楼之间从远处看可能会重叠,求出 N  ...

  5. [leetcode]206. Reverse Linked List反转链表

    Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...

  6. springboot与消息(rabbtiMQ)

    1.大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 1.1异步通信 1.2解耦 1.3削峰 2.消息服务中两个重要概念:       消息代理(message broker)和目的地 ...

  7. SVG脚本编程简介

    本文主要介绍SVG的脚本编程,并分别给出放大.缩小,查询,鼠标事件等实例. 一. SVG简介 SVG,全称为Scalable Vector Graphics(可伸缩矢量图形).它是W3C制定的.用矢量 ...

  8. Linux automake命令

    1)automake 使用教程 http://loftor.com/archives/automake.html 2)configure.in Makefile.am解析 http://blog.cs ...

  9. eclipse中的.project 和 .classpath文件的具体作用(综合):

    .project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置 .classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH 这些文件你用文 ...

  10. linux下的shell运算(加、减、乘、除

    linux下的shell运算(加.减.乘.除 摘自:https://blog.csdn.net/hxpjava1/article/details/80719112 2018年06月17日 16:03: ...