zoukankan      html  css  js  c++  java
  • 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


    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 ");
    continue;
    }
    int i;

    for(i=0;i<m;i++){
    if(a[i]>sum/4)
    break;
    }
    if(i!=m){
    printf("no ");
    continue;
    }
    sum=sum/4;
    flag=0;
    memset(vis,0,sizeof(vis));
    bfs(1,0,0);
    if(flag)
    {
    printf("yes ");
    }
    else
    {
    printf("no ");
    }
    }
    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,因为要考虑到全面,只要不能整除就不能构成正方形。

  • 相关阅读:
    ElasticSearch原理
    redis master配置了密码进行主从同步
    redis sentinel 高可用(HA)方案部署,及python应用示例
    Linux Redis集群搭建与集群客户端实现
    字符串倒序
    单链表反转
    【python】面试常考数据结构算法
    面试中的排序算法总结
    Memcached 真的过时了吗?
    Activity生命周期
  • 原文地址:https://www.cnblogs.com/1314wamm/p/5664026.html
Copyright © 2011-2022 走看看