活动安排

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ActivitiesArrangement {
private static int activityNum = 7;
private static int totalTime;
private static int[] activityTimeArray = new int[activityNum];
private static int[][] activityTimeArrangementArray;
private static int flag = -1;

private static int personNum = 15;
private static boolean[][] activityPersonArray = new boolean[activityNum][personNum];

private static int distanceTime = Integer.MAX_VALUE;
private static int[][] copyActivityTimeArrangementArray;
static {
Random random = new Random();
//init activityTimeArray
for (int i = 0; i < activityTimeArray.length; i++) {
activityTimeArray[i] = Math.abs(random.nextInt()) % 5 + 1;
}
for (int i = 0; i < activityTimeArray.length; i++) {
System.out.printf(i + " " + activityTimeArray[i]);
System.out.println();
}
System.out.println();

totalTime = 0;
for (int i = 0; i < activityTimeArray.length; i++) {
totalTime += activityTimeArray[i];
}
totalTime = totalTime / 2;
activityTimeArrangementArray = new int[activityNum][totalTime];
for (int i = 0; i < activityTimeArrangementArray.length; i++) {
for (int j = 0; j < activityTimeArrangementArray[i].length; j++) {
activityTimeArrangementArray[i][j] = flag;
}
}
// init activityPersonArray
for (int i = 0; i < activityPersonArray.length; i++) {
for (int j = 0; j < activityPersonArray[i].length; j++) {
activityPersonArray[i][j] = (random.nextInt() % 4 == 0 ? true : false);
}
}
for (int i = 0; i < activityPersonArray.length; i++) {
for (int j = 0; j < activityPersonArray[i].length; j++) {
if (activityPersonArray[i][j]) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
}
System.out.println();
}
System.out.println();

copyActivityTimeArrangementArray = new int[activityNum][totalTime];
}

private static void arrangeActivities(List<Integer> activities, int p) {
for (Integer activity : activities) {
for (int j = 0; j < totalTime - activityTimeArray[activity]; j++) {
if (canArrangeActivity(j, activityTimeArray[activity], p, activity)) {
for (int k = j; k < j + activityTimeArray[activity]; k++) {
activityTimeArrangementArray[p][k] = activity;
}
if (p == activityNum - 1) {
printArrangement();
} else {
ArrayList<Integer> tempArrayList = new ArrayList<Integer>();
tempArrayList.addAll(activities);
tempArrayList.remove(activity);
arrangeActivities(tempArrayList, p + 1);
}
for (int k = j; k < j + activityTimeArray[activity]; k++) {
activityTimeArrangementArray[p][k] = flag;
}
}
}
}
}

private static void printArrangement() {
int temp = 0;
for (int i = 0; i < activityPersonArray.length; i++) {
for (int j = 0; j < activityPersonArray[i].length; j++) {
if (activityPersonArray[i][j]) {
for (int k = 0; k < activityPersonArray.length; k++) {
if (k != i && activityPersonArray[k][j]) {
temp += computeDistance(i, k);
}
}
}
}
}
if (temp < distanceTime) {
distanceTime = temp;
for (int i = 0; i < copyActivityTimeArrangementArray.length; i++) {
for (int j = 0; j < copyActivityTimeArrangementArray[i].length; j++) {
copyActivityTimeArrangementArray[i][j] = activityTimeArrangementArray[i][j];
}
}
}

}

private static int computeDistance(int activity1, int activity2) {
int temp1 = 0, temp2 = 0;
for (int i = 0; i < activityTimeArrangementArray.length; i++) {
for (int j = 0; j < activityTimeArrangementArray[i].length; j++) {
if (activityTimeArrangementArray[i][j] == activity1) {
temp1 = j;
}
if (activityTimeArrangementArray[i][j] == activity2) {
temp2 = j;
}
}
}
return Math.abs(temp1 - temp2);
}

private static boolean canArrangeActivity(int start, int length, int p, int activity) {
for (int i = 0; i < p; i++) {
for (int j = start; j < start + length; j++) {
if (activityTimeArrangementArray[i][j] != flag) {
if (!canActivitiesTogether(activityTimeArrangementArray[i][j], activity)) {
return false;
}
}
}
}
return true;
}

private static boolean canActivitiesTogether(int activity1, int activity2) {
for (int j = 0; j < personNum; j++) {
if (activityPersonArray[activity1][j] && activityPersonArray[activity2][j]) {
return false;
}
}
return true;
}

public static void main(String[] args) {
ArrayList<Integer> tempArrayList = new ArrayList<Integer>();
for (int i = 0; i < activityNum; i++) {
tempArrayList.add(i);
}
arrangeActivities(tempArrayList, 0);

System.out.println(distanceTime);
for (int i = 0; i < copyActivityTimeArrangementArray.length; i++) {
for (int j = 0; j < copyActivityTimeArrangementArray[i].length; j++) {
System.out.printf("%2d ", copyActivityTimeArrangementArray[i][j]);
}
System.out.println();
}
System.out.println();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值