Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12859    Accepted Submission(s):
4078

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<stdio.h>
#include<string.h>
int flag;
int m,a[30],sum ,vis[30];
void bfs(int s,int l,int k){
if(s==5)
{
flag=1;
return;
}
if(l==sum)
{
bfs(s+1,0,0);
if(flag)
return;
}
for(int j=k;j<m;j++){
if(!vis[j]&&l+a[j]<=sum)
{
int temp=a[j];
vis[j]=1;
bfs(s,l+temp,j+1);
vis[j]=0;
if(flag)
return;

}
}
}
int main()
{int n;
scanf("%d",&n);
while(n--){
scanf("%d",&m);
sum=0;
for(int i=0;i<m;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum%4==1){
printf("no\n");
continue;
}
int i;

for(i=0;i<m;i++){
if(a[i]>sum/4)
break;
}
if(i!=m){
printf("no\n");
continue;
}
sum=sum/4;
flag=0;
memset(vis,0,sizeof(vis));
bfs(1,0,0);
if(flag)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}

------------------------------------------------

import java.util.Scanner;
public class Main1518 {
static int[]now;
static int flag,m,sum;
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
while(cin.hasNext()){
int n=cin.nextInt();
while(n-->0){
m=cin.nextInt();
now=new int[m];
sum=0;
for(int i=0;i<m;i++){
now[i]=cin.nextInt();
sum+=now[i];
}
if(sum%4!=0){
System.out.println("no");
continue;
}
int i;
for(i=0;i<m;i++){
if(now[i]>sum/4){
break;
}
}
if(i!=m){
System.out.println("no");
continue;
}
sum=sum/4;
flag=0;
bfs(1,0,0);
if(flag==1){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
}
return;
}
private static void bfs(int i, int j, int k) {
if(i==5){
flag=1;
return;
}
if(j==sum){
bfs(i+1,0,0);
if(flag==1)
return;
}
for(int s=k;s<m;s++){
if(now[s]!=0&&now[s]+j<=sum){
int sk=now[s];
now[s]=0;
bfs(i,sk+j,s+1);
if(flag==1)
return;
now[s]=sk;

}
}

}

}

-------------------------------------------------

通过java和c之间的编写,我发现了java要比c严密很多。例如sum%4==1在c里面可以通过,但是在java里面sun%4!=0,因为要考虑到全面,只要不能整除就不能构成正方形。

HDU1518(dfs)java/ c++的更多相关文章

  1. BFS和DFS (java版)

    package com.algorithm.test; import java.util.ArrayDeque; import java.util.Scanner; public class DfsA ...

  2. 使用Maven对JAVA程序打包-带主类、带依赖【转】

    很多时候,我们需要对编写的程序进行打包,这个时候,我们可以借助一些项目构建工具,如maven, sbt, ant等,这里我使用的是maven. 打包成可执行有主类的jar包(jar包中无依赖) 以下是 ...

  3. "《算法导论》之‘图’":深度优先搜索、宽度优先搜索(无向图、有向图)

    本文兼参考自<算法导论>及<算法>. 以前一直不能够理解深度优先搜索和广度优先搜索,总是很怕去碰它们,但经过阅读上边提到的两本书,豁然开朗,马上就能理解得更进一步. 下文将会用 ...

  4. 856. Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  5. [LeetCode] 399. Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  6. [LeetCode] 499. The Maze III 迷宫 III

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  7. 488. Zuma Game

    Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...

  8. 473. Matchsticks to Square

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

  9. 805. Split Array With Same Average

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

随机推荐

  1. c/c++ void 指针

    原文 : http://blog.csdn.net/yyyuhan/article/details/3153290 1.概述 许多初学者对C/C++语言中的void及void指针类型不甚理解,因此在使 ...

  2. BZOJ_1011_[HNOI2008]_遥远的行星_(近似)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1011 \(n\)个行星,第\(i\)颗行星的质量为\(m_i\),给出一个很小的常数\(A\) ...

  3. Lua查找字符串注意

    问题: 使用Lua写Wireshark插件时,经常匹配字符串.今天使用string.find()函数查找字符串”max-age”,没有找到. 分析: local index = string.find ...

  4. extjs获得store数据

    var json = new Array(); for (var i = 0; i < storeEditFee.getCount(); i++) { json.push(storeEditFe ...

  5. 性能测试vs负载测试vs压力测试-概念普及

    下面我们主要介绍性能测试.负载测试和压力测试. 效率作为ISO 9126内部和外部质量的重要质量属性之一,其含义是在规定条件下,相对于所用的资源的数量,软件产品可提供适当性能的能力.资源可能包括其他软 ...

  6. 如何在Azure上动态配置IP地址

    微软最近对 Windows Azure 网站进行了升级,并启用了IIS8的动态 IP 限制模块.现在,开发人员可以为其网站启用并配置动态 IP 限制功能(或简称 DIPR). 可以通过以下链接查看此 ...

  7. 怎么加 一个 hyperlink 到 e-mail template for CRM

    Recently I had a client inquire as to how one would insert a hyperlink into a CRM email template. Wh ...

  8. VisualBox会造成VPN连接不上问题

    今天连接VPN查找点资料,但是怎么也连接不上,一直出现651错误,开始以为是服务器配置原因,重新配置后发现问题依旧,后查找相关资料,发现有提到VisualPC, 感觉VisualBox跟VisualP ...

  9. leptonica使用问题

    想要使用leptonica编写一个图像处理程序,或者调用leptonica/prog下例子程序,出现类似错误: leptTest ./test Error in pixReadStreamJpeg: ...

  10. DATASNAP复杂中间件的一些处理方法

    1.中间件需要连接SQL SERVER\ORACLE\MYSQL多种数据库,怎么办? [解决]:可以搞多种数据模块池对应多种数据库,一种数据模块池对应一种数据库 2.中间件业务对象多,在一个单元里面定 ...