HDOJ1518Square 深搜

Stella981
• 阅读 530

Square
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11099 Accepted Submission(s): 3566

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
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<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s,su,a[1010],vis[1010],len;
bool cmp(int a,int b)
{
    return a>b;//从大到小排序
}
void dfs(int a1,int a2,int a3)//(0,0,1)
{
    if(a1==3)/**只需要筹齐3次,那么剩下的一定能够成len长度**/
    {
        su=1;
        return ;
    }
    if(su==1)
        return ;/**优化时间**/
    for(int i=a3; i<=n; i++)
    {
  /**对于那些用过的和不符合条件的,for那里可以不扫,故从a3开始**/
   /**a3前面的对于最初的a2来说一定不符合**/
        if(vis[i]==0)
        {
            vis[i]=1;
            if(a2+a[i]==len)
            {
                dfs(a1+1,0,1);
                 /**但是换另外一条边的时候a3要改回1,因为那些未用的,对上一条边来说不符合条件的,可能符合这条边的条件**/
            }
            else if(a2+a[i]<len)
            {
                dfs(a1,a2+a[i],i+1);
                /**没筹齐从i+1继续,前面的不符合**/
                while(a[i]==a[i+1])
                    i++;
                    //回溯后如果后面的相同那么不需要再DFS
       //前面的数和后面的相同就可以跳过这个数,剪枝
            }
            vis[i]=0;
        }
    }
}

int main()
{
    int i;
    scanf("%d",&s);
    while(s--)
    {
        su=0;
        len=0;
        memset(vis,0,sizeof(vis));
        //memset函数在string.h头文件中
        scanf("%d",&n);
        for(i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            len=len+a[i];
        }
        int mm=len/4;
        sort(a+1,a+n+1,cmp);
        //在algorithm头文件中
        if(len%4==0&&a[1]<=mm&&n>=4)
        {
            len/=4;
            dfs(0,0,1);
            if(su==1)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

本文同步分享在 博客“谙忆”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java 复制Map对象(深拷贝与浅拷贝)
java复制Map对象(深拷贝与浅拷贝)CreationTime2018年6月4日10点00分Author:Marydon1.深拷贝与浅拷贝  浅拷贝:只复制对象的引用,两个引用仍然指向同一个对象
桃浪十七丶 桃浪十七丶
2年前
输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。
题目内容:输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。例:(1)输入:Ilovehebeu!输出:character:10,space:2,digit:0,others:1(2)输入:2020,haveabrilliantyear!输出:character:18,space:4,digit:4,others:2答案:c
Easter79 Easter79
2年前
Typescript 常见的几种函数重载方法详解与应用示例
所谓的重载,其实就是使用相同的函数名,传入不同数量的参数或不同类型的参数,以此创建出多个方法或产生不同结果。1\.最常见的,也就是根据定义傻瓜式地判断参数类型与数量functionshowPerson(name,...others){console.log(name,others)}
Wesley13 Wesley13
2年前
HDU 6336 子矩阵求和
ProblemE.MatrixfromArraysTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:262144/262144K(Java/Others)TotalSubmission(s):1162    
Stella981 Stella981
2年前
Python time模块 返回格式化时间
常用命令  strftimetime.strftime("%Y%m%d%H:%M:%S",formattime)第二个参数为可选参数,不填第二个参数则返回格式化后的当前时间日期201812112:00:00time.strftime('%H:%M:%S')返回当前时间的时分秒time.strftim
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
HDOJ 1237题 简单计算器
简单计算器TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):15220AcceptedSubmission(s):5195ProblemDescription读入一个只包含,
Wesley13 Wesley13
2年前
ACM_括号匹配
括号匹配(栈)TimeLimit:2000/1000ms(Java/Others)ProblemDescription:给一组包含()两种括号的序列,检查是否是合法的。如:(),(
双寿 双寿
3个月前
慕课甄选-2024年Flutter零基础极速入门到进阶实战[16章]
参考资料地址1:https://pan.baidu.com/s/1j35W30a7JQAGTV2rYgxRNA提取码:5o3h参考资料地址2:https://pan.baidu.com/s/1Iwj10AL7jdum19WQz1jdA提取码:0n8xFlu