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

本文介绍了一个关于船员调度的问题及其实现算法。该算法通过深度优先搜索的方式寻找最优解,以最小化所有船员到达指定位置所需的总时间。具体实现中使用了递归调用,并考虑了船员的不同方向及其对总时间的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值