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();
}
}
活动安排
最新推荐文章于 2024-11-14 14:48:14 发布