Created
June 26, 2014 12:39
-
-
Save valtoni/6bc327e761ba317b0647 to your computer and use it in GitHub Desktop.
Problem A resolution - http://icpc.baylor.edu/xwiki/wiki/public/download/worldfinals/problems/icpc2014.pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Ipc2014ProblemA { | |
private static class BaggageBin { | |
int space; | |
char destination; | |
public BaggageBin(int space) { | |
if (space > 0) { | |
if (space % 2 == 0) { | |
this.destination = 'A'; | |
} | |
else { | |
this.destination = 'B'; | |
} | |
} | |
this.space = space; | |
} | |
@Override | |
public String toString() { | |
return (this.space < 0 ? this.space : " " + this.space) + "/" + this.destination; | |
} | |
} | |
private static BaggageBin[] mountBinMatrix(int baggageBins) { | |
// right = baggageBins | |
// left = baggageBins | |
// totalSpace = 2 * baggageBins | |
int totalSpace = 2 * baggageBins; | |
int mostLeftSpace = - baggageBins + 1; | |
BaggageBin[] internalBaggageBin = new BaggageBin[totalSpace]; | |
for (int i = 0; i < totalSpace; i++) { | |
internalBaggageBin[i] = new BaggageBin(mostLeftSpace++); | |
} | |
return internalBaggageBin; | |
} | |
private static void printLine(int num) { | |
for (int i = 0; i <= num - 1; i++) { | |
System.out.print("+------"); | |
} | |
System.out.println("+"); | |
} | |
private static void printMatrix(BaggageBin[] baggageBinMatrix) { | |
int length = baggageBinMatrix.length; | |
int i = 0; | |
printLine(length); | |
for (BaggageBin baggageBin: baggageBinMatrix) { | |
System.out.print("| " + baggageBin.toString() + " "); | |
} | |
System.out.println("|"); | |
printLine(length); | |
} | |
public static void main(String[] args) { | |
// Assign values | |
int baggageCounter = 4; | |
int baggageBins = baggageCounter * 2; | |
int motorizedCarts = baggageCounter; | |
// Mounting baggage matrix | |
BaggageBin[] binMatrix = mountBinMatrix(baggageBins); | |
printMatrix(binMatrix); | |
// Sorting | |
BaggageBin[] sortedMatrix = sortBinMatrix(binMatrix); | |
} | |
private static BaggageBin[] sortBinMatrix(BaggageBin[] binMatrix) { | |
BaggageBin[] cartMove1Matrix = sortBinMove1(binMatrix); | |
printMatrix(cartMove1Matrix); | |
BaggageBin[] cartMove2Matrix = sortBinMove2(cartMove1Matrix); | |
printMatrix(cartMove2Matrix); | |
return cartMove2Matrix; | |
} | |
private static BaggageBin[] sortBinMove2(BaggageBin[] binMatrix) { | |
BaggageBin oldBin, newBin; | |
int newMatrixPosition, oldMatrixPosition; | |
int firstPosition = binMatrix.length / 2; | |
int lastPosition = binMatrix.length; | |
int numberMoves = (lastPosition - firstPosition) / 2; | |
// Move starts from 2 (1st already is B) | |
for (int move = 2; move <= numberMoves; move++) { | |
// Old bin | |
oldMatrixPosition = ((move * 2) - 2) + firstPosition; | |
oldBin = binMatrix[oldMatrixPosition]; | |
newMatrixPosition = firstPosition + move - 1; | |
newBin = binMatrix[newMatrixPosition]; | |
newBin.destination = oldBin.destination; | |
oldBin.destination = ' '; | |
} | |
return binMatrix; | |
} | |
private static BaggageBin[] sortBinMove1(BaggageBin[] binMatrix) { | |
BaggageBin oldBin, newBin; | |
int newMatrixPosition, oldMatrixPosition; | |
int firstPosition = binMatrix.length / 2; | |
int lastPosition = binMatrix.length; | |
int numberMoves = (lastPosition - firstPosition) / 2; | |
for (int move = 1; move <= numberMoves; move++) { | |
// Old bin | |
oldMatrixPosition = (move == 1 ? move : 2 * move - 1) + firstPosition; | |
oldBin = binMatrix[oldMatrixPosition]; | |
// New bin | |
newMatrixPosition = firstPosition - 1 - (move - 1); | |
newBin = binMatrix[newMatrixPosition]; | |
// | |
newBin.destination = oldBin.destination; | |
oldBin.destination = ' '; | |
} | |
return binMatrix; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment