1014:Dividing

背包容量:所有宝石权重之和的一半
从最贵重的宝石开始搜索,这样可以避免尾递归

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

/*      *//*画蛇添足*//*
      String COLLECTION = "Collection #";
      String[] DIVIDABLE = {"Can be divided.", "Can't be divided."};
      int DIVIDE_TRUE = 0, DIVIDE_FALSE = 1;*/

      Scanner scanner = new Scanner(System.in);
      int[] N = new int[6];
      int collection = 1;
          while (true) {
          int pack = 0;
          for (int i = 0; i < 6; ++i) {
              N[i] = scanner.nextInt();
              pack += (i + 1) * N[i];
          }

          if (pack == 0) break;

          if ((1 & pack) == 1)
              System.out.print("Collection #" + collection + ":\n" + "Can't be divided.\n\n");
          else {
              if (dfs(pack >> 1, N, 6)) {
                  System.out.print("Collection #" + collection + ":\n" + "Can be divided.\n\n");
              } else {
                  System.out.print("Collection #" + collection + ":\n" + "Can't be divided.\n\n");
              }
          }
          ++collection;
      }
  }

  private static boolean dfs(int left, int[] num, int pre) {
      if (left == 0) {
          return true;
      }
      for (int i = pre; i >= 1; i--) {
          if (num[i - 1] != 0) {
              if ((left - i) >= 0) {
                  num[i - 1]--;
                  if (dfs(left - i, num, i)) {
                      return true;
                  }
              }
          }
      }
      return false;
  }
}

TLE

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

    static final String COLLECTION = "Collection #";
    static final String[] DIVIDABLE = {"Can be divided.", "Can't be divided."};
    static final int DIVIDE_TRUE = 0, DIVIDE_FALSE = 1;
    static final String ENDING_LINE = "0 0 0 0 0 0";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int[] N = new int[6];
        int collection = 1;
        while (!s.equals(ENDING_LINE)) {
            int pack = 0;
            String[] t = s.split(" ");
            for (int i = 0; i < 6; ++i) {
                N[i] = Integer.parseInt(t[i]);
                pack += (i + 1) * N[i];
            }
            if ((1 & pack) == 1)
                outPutResult(DIVIDE_FALSE, collection);
            else {
                if (dp(pack >> 1, N, new HashMap<>())) {
                    outPutResult(DIVIDE_TRUE, collection);
                } else {
                    outPutResult(DIVIDE_FALSE, collection);
                }
            }
            ++collection;
            s = scanner.nextLine();
        }
    }

    private static final void outPutResult(int result, int collection) {
        System.out.print('\n' + COLLECTION + collection + ":\n" + DIVIDABLE[result] + '\n');
    }

    private static boolean dp (int left, int[] N, HashMap map) {
        String key = left + " " + Arrays.toString(N);
        if (map.containsKey(key)) return map.get(key);
        boolean res = false;
        for (int i = 1; i <= 6; ++i) {
            if (N[i - 1] > 0) {
                if (i == left) {
                    res = true;
                    break;
                } else if ( i < left ) {
                    --N[i - 1];
                    res = dp(left - i, N, map);
                    if (res) {
                        break;
                    } else {
                        ++N[i - 1];
                    }
                } else {
                    continue;
                }
            }
        }
        map.put(key, res);
        return res;
    }
}

你可能感兴趣的:(1014:Dividing)