港口钓鱼 求人员分配 移动最短距离值和 三个门
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
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
相关文章推荐
- SSL1338 人员分配(最大匹配)
- 求两个时间点的最短距离
- 最短编辑距离
- VirtualAlloc,GlobalAlloc,HeapAlloc三个分配内存函数的区别
- 点到折线最短距离所在点距离折线起点的累积距离
- 多数球面距离最短证明的漏洞
- 【杭电oj】2083 - 简易版之最短距离(最短距离)
- 【杭电-oj】-2083-简易版之最短距离(走朋友回家,再走朋友,怎么近)
- java 代码 点到线段的最短距离
- ACM-ICPC 2015 Asia Tsukuba Regional Online Open Contest B(两挡板之间放着圆球,求挡板最短距离)
- 移动开发人员如何提升自己的技术水平
- HDU 4271 Find Black Hand [最短编辑距离DP]
- 找出距离最近的三个点
- 技术人员在转向移动开发过程中有哪些经验和教训值得大家借鉴?
- 最短距离
- 蓝桥杯 移动距离
- 虚拟与现实的距离——VR的2016正如移动互联网的2009【下篇】
- HDU 2083简易版之最短距离
- 职场人际需控制的三个距离
- 利用成员函数、友元函数和一般函数,实现三个版本的求两点间距离的函数