HDU1518(dfs)java/ c++
Square
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12859 Accepted Submission(s):
4078
possible to join them end-to-end to form a square?
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.
possible to form a square; otherwise output "no".
#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++的更多相关文章
- BFS和DFS (java版)
package com.algorithm.test; import java.util.ArrayDeque; import java.util.Scanner; public class DfsA ...
- 使用Maven对JAVA程序打包-带主类、带依赖【转】
很多时候,我们需要对编写的程序进行打包,这个时候,我们可以借助一些项目构建工具,如maven, sbt, ant等,这里我使用的是maven. 打包成可执行有主类的jar包(jar包中无依赖) 以下是 ...
- "《算法导论》之‘图’":深度优先搜索、宽度优先搜索(无向图、有向图)
本文兼参考自<算法导论>及<算法>. 以前一直不能够理解深度优先搜索和广度优先搜索,总是很怕去碰它们,但经过阅读上边提到的两本书,豁然开朗,马上就能理解得更进一步. 下文将会用 ...
- 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [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 ...
- 488. Zuma Game
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
- 473. Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 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 ...
随机推荐
- CreateObject("Wscript.Shell")用法
WScript.Shell是WshShell对象的ProgID,创建WshShell对象可以运行程序.操作注册表.创建快捷方式.访问系统文件夹.管理环境变量. 该对象有一个run方法. Run 方法创 ...
- 带你走进EJB--MDB实现发送邮件(3)
接上篇,在业务逻辑中已经发送JMS消息,而接下来的消息驱动Bean作为JMS消息监听器,主要是负责监听指定的JMS消息,此时已经接受到JMS的消息,那么MDB的onMessage()方法会被触发.调用 ...
- 全新 D 系列虚拟机型号
Kenaz KwaAzure计算运行时项目经理 今天,我们宣布将发布名为D系列的Windows Azure 新VM型号,并支持虚拟机和 Web/Worker Role.这些虚拟机型号最多可以提供 11 ...
- c语言字符串库函数#include<string.h>
字符串函数<string.h> 在头文件<string.h>中定义了两组字符串函数.第一组函数的名字以str开头:第二组函数的名字以mem开头.只有函数memmove对重叠对象 ...
- 使用console进行性能测试和计算代码运行时间
对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用debugger会显得过于笨重,最常用的方法是会将值输出到控制台上方便调试.最常用的语句就是console.log(expre ...
- 从零开始学习jQuery (十一) 实战表单验证与自动完成提示插件
一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案, 即使你会使用jQuery也能在阅读中发现些许秘籍. 本文是介绍两个最常用的jQuery插件. 分别用 ...
- [Andrew]Ext.Net常用布局(Border布局)
@(Html.X().Window().Width(600).Height(400).Layout(LayoutType.Border) .Items(p => { p.Add(Html ...
- 【原】实战-Java如何使用Redis
实战-Java如何使用Redis Redis的Client支持的语言非常丰富,如下: ActionScript Bash C C# C++ Clojure Common Lisp Crystal D ...
- Hacking Secret Ciphers with Python翻译序言
马上就要下班,一直想做点什么,学点什么,但是似乎从未着手. 是的,我想学习Hacking,或许很多人都想学,但是诸多的大牛说,这个得有基础,万丈高楼平地起,我做过那么一点点的密码分析,加上某些地方有小 ...
- (原)Eclipse Tomcat配置(2014.12.27——By小赞)
Eclipse中配置自己已经安装的Tomcat 首先为Eclipse安装Tomcat插件: 进入Tomcat插件下载页:http://www.eclipsetotale.com/tomcatPlugi ...