Created
May 8, 2011 18:16
-
-
Save stphung/961555 to your computer and use it in GitHub Desktop.
Google Code Jam 2011 Qualifier Round - Problem A, Bot Trust
This file contains hidden or 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
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.util.Queue; | |
| import java.util.Scanner; | |
| import java.util.concurrent.LinkedBlockingQueue; | |
| public class BotTrust { | |
| public static void main(String[] args) throws FileNotFoundException { | |
| Scanner sc = new Scanner(new File("A-large.in")); | |
| int cases = sc.nextInt(); | |
| for(int c=1; c<=cases; c++) { | |
| int buttons = sc.nextInt(); | |
| boolean[] oTurn = new boolean[buttons]; | |
| Queue<Integer> oMoves = new LinkedBlockingQueue<Integer>(); | |
| Queue<Integer> bMoves = new LinkedBlockingQueue<Integer>(); | |
| for(int i=0; i<buttons; i++) { | |
| char bot = sc.next().charAt(0); | |
| int button = sc.nextInt(); | |
| if (bot == 'O') { | |
| oMoves.add(button); | |
| oTurn[i] = true; | |
| } else { | |
| bMoves.add(button); | |
| oTurn[i] = false; | |
| } | |
| } | |
| int time = 0; | |
| int oLoc = 1; | |
| int bLoc = 1; | |
| int turn = 0; | |
| boolean removed = false; | |
| while (oMoves.size() > 0 || bMoves.size() > 0) { | |
| removed = false; | |
| if (oMoves.size() > 0) { | |
| int dest = oMoves.element(); | |
| if (oLoc < dest) oLoc++; | |
| else if (oLoc > dest) oLoc--; | |
| else { | |
| if (oTurn[turn]) { | |
| removed = true; | |
| oMoves.remove(); | |
| turn++; | |
| } | |
| } | |
| } | |
| if (bMoves.size() > 0) { | |
| int dest = bMoves.element(); | |
| if (bLoc < dest) bLoc++; | |
| else if (bLoc > dest) bLoc--; | |
| else { | |
| if (!oTurn[turn] && !removed) { | |
| bMoves.remove(); | |
| turn++; | |
| } | |
| } | |
| } | |
| time++; | |
| } | |
| System.out.println("Case #" + c + ": " + time); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment