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 ...
随机推荐
- Android:DES加密
private static final String KEY = "xxxxxx"; // KEY的字节长度必须超过24 public DESUtil(){ super(); } ...
- 【HDOJ】1230 火星A+B
个人觉得这道题没那么水,wa了几次,才发现自己居然没有给srcb数组reset,打错了.搞死啊. #include <stdio.h> #include <string.h> ...
- Mysql表的七种引擎类型,InnoDB和MyISAM引擎对比区别总结
InnoDB和MyISAM区别总结 我用MySQL的时候用的是Navicat for MySQL(Navicat for mysql v9.0.15注册码生成器)操作库.表操作的,默认的表就是Inno ...
- WPF手写代码配置文件——单例
public class SettingHelper { //WPF下配置文件路径 public static readonly string SettingFilePath = AppDomain. ...
- C#常用的命名规范
C#常用的命名规则 Pascal 规则 每个单词开头的字母大写(如 TestCounter). Camel 规则 除了第一个单词外的其他单词的开头字母大写. 如. testCounter. Upper ...
- [POJ 1151] Atlantis
一样的题:HDU 1542 Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18148 Accepted ...
- windows下面虚拟主机
1. wondows xp + apmserv 5.2.6 端口8088 2.httpd.config文件 1. ServerRoot "D:/APMServ/APMServ5.2.6/A ...
- jQuery on()方法绑定动态元素的点击事件
之前就一直受这个问题的困扰,在jQuery1.7版本之后添加了on方法,之前就了解过,其优越性高于live(),bind(),delegate()等方法,在此之前项目中想用这个来测试结果发现,居然动态 ...
- C# asp.net 操作Word的前提配置和简单的方法
操作的前提: 1.要保证机器本身要安装OFFICE. 有时安装了Office,但是不能找到Microsoft Word 11.0(或者更高的版本) Object Library.那可能是因为在安装of ...
- 利用Testng注释实现多线程并发测试
Testng 是一款非常优秀的测试框架,真正从测试角度出发,为测试所想.在测试过程中我们经常会遇到对某一个场景做并发请求,主要想了解该程序在并发时是否会有异常或者没考虑到的其他情况,这时往往不是要做性 ...