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. Date简介

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...

  2. HDU 4009 不定根最小树形图

    讲一下建图过程,首先建立一个超级源点S,对于这个源点,向每个HOUSE连一条有向边,权值为该HOUSE建立WELL的费用,即高度*X. 然后每个可以连边的WELL之间,费用为曼哈顿距离*Y,然后考虑两 ...

  3. FileSystemWatcher使用方法

    FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删除.改动.重命名等活动.可以动态地定义需要监控的文件类型及文件属性改动的类型. 1.常用的几个基本属性: (1) P ...

  4. msdn上wcf的介绍

    https://msdn.microsoft.com/zh-cn/library/dd456779(v=vs.110).aspx Windows Communication Foundation Ar ...

  5. MySQL 授权详解

    (1)确认一下3306是否对外开放,mysql默认状态下是不开放对外访问功能的.查看的办法如下: 1 2 3 4 5 6 7 netstat -an | grep 3306 tcp        0  ...

  6. apache开源项目--JMeter

    JMeter是Apache组织的开放源代码项目,它是功能和性能测试的工具,100%的用java实现.

  7. iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”

    iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.” http://stackoverf ...

  8. 【libsvm学习】

    参考: http://www.cnblogs.com/bigshuai/articles/2883256.html http://www.cnblogs.com/tornadomeet/archive ...

  9. 关于 UGUI 字体花屏或乱码。

    我们项目从某个时候开始ui突然开始出现字体花屏现象(unity 开发版本:5.3.6p6),而且很难必现却又时有发生,确实查找和解决起来不太容易. 关于这个问题,uwa官方给出了解释,http://b ...

  10. Hadoop HDFS的常用命令

    1.将目录/root/data/下的item.txt复制到HDFS下的/user/root下: hadoop fs -copyFromLocal /root/data/item.txt itemdat ...