您的位置:首页 > 其它

港口钓鱼 求人员分配 移动最短距离值和 三个门

2017-01-17 16:31 295 查看

package work;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class AboatC {

 static int T, N, mind;

 static int[] gate;

 static int[] peo;

 static boolean[] used;

 static int[] orip;//人的方向位置

 public static void main(String[] args) throws FileNotFoundException {

  /* Scanner sc=new Scanner(System.in); */

  Scanner sc = new Scanner(new File("src/Aboat.txt"));

  T = sc.nextInt();

  for (int t = 0; t < T; t++) {

   N = sc.nextInt();

   gate = new int[3];

   peo = new int[20];

   used = new boolean[3];

   orip = new int[N+1];

   for (int i = 0; i < 3; i++) {

    gate[i] = sc.nextInt();

    peo[i] = sc.nextInt();

   }

   mind = 0xfffffff;

   dfs(0, 0);

   System.out.println(mind);

  }

 

 }

 private static void dfs(int step, int sum) {

  if (step == 3) {

   if (mind > sum) {

    mind = sum;

   }

   return;

  }

  if (sum > mind) {

   return;

  }

  for (int i = 0; i < 3; i++) {

   if (!used[i]) {

    used[i] = true;

    if (i == 0) {

     dfs(step + 1, sum + enterleft(i));

     init(i);

    }

    if (i == 1) {

     dfs(step + 1, sum + enterleft(i));

     init(i);

     dfs(step + 1, sum + enterright(i));

     init(i);

    }

    if (i == 2) {

     dfs(step + 1, sum + enterright(i));

     init(i);

    }

    used[i] = false;

   }

   

  }

 }

 private static int enterleft(int p) {

  int pos = gate[p];// 门的位置

  int tmp = 0;// 港口到门的距离

  int count = 0;// 走的总步数

  for (int i = peo[p]; i >=1; ) {

   if (pos - tmp > 0 && orip[pos - tmp] == 0) {

    orip[pos - tmp] = -p - 1;

    i--;

    count += 1 + tmp;

   }

   if (i == 0) {

    break;

   }

   if (pos + tmp < N && orip[pos + tmp] == 0) {

    orip[pos + tmp] = -p - 1;

    i--;

    count += 1 + tmp;

   }

   tmp++;// 每次添加一步后tmp++

  }

  return count;

 }

 private static int enterright(int p) {

  int pos = gate[p];// 门的位置

  int tmp = 0;// 港口到门的距离

  int count = 0;// 走的总步数

  for (int i = peo[p]; i >= 1; ) {

   if (pos + tmp < N && orip[pos + tmp] == 0) {

    orip[pos + tmp] = -p - 1;

    i--;

    count += 1 + tmp;

   }

   if (i == 0) {

    break;

   }

   if (pos - tmp > 0 && orip[pos - tmp] == 0) {

    orip[pos - tmp] = -p - 1;

    i--;

    count += 1 + tmp;

   }

   tmp++;// 每次添加一步后tmp++

  }

  return count;

 }

 private static void init(int p) {

  for (int i = 0; i < orip.length; i++) {

   if (orip[i] == -p - 1) {

    orip[i] = 0;

   }

  }

 }

}

、、input

1

10

4 5

6 2

10 2

、、output

18
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: