|
| 1 | +import java.io.FileReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +/* |
| 7 | + * |
| 8 | +The Problem |
| 9 | +You will be given a block of square rooms in an X by Y configuration, |
| 10 | +with a door in the center of every wall. Some rooms will have a mirror in them at a 45 degree angle. |
| 11 | +The mirrors may reflect off both sides (2-way mirrors) |
| 12 | +or reflect off one side and allow the beam to pass through from the other (1-way mirrors). |
| 13 | +When the laser hits the reflective side of one of the mirrors, |
| 14 | +the beam will reflect off at a 90 degree angle. |
| 15 | +Your challenge is to calculate the exit point of a laser shot into one of the open doors. |
| 16 | +You need to provide the room it will be exiting through along with the orientation. |
| 17 | +The definition file will be provided through command line parameters. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +public class MirrorMaze { |
| 22 | + |
| 23 | + public static void main(String[] args) throws IOException { |
| 24 | + |
| 25 | + if (args.length == 0) { |
| 26 | + System.out.println("please give input file."); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + int mazeRow = 0; |
| 31 | + int mazeCol = 0; |
| 32 | + ArrayList<String> mirrors = new ArrayList<String>(); |
| 33 | + int laserStartRow = -1; |
| 34 | + int laserStartCol = -1; |
| 35 | + String laserOrientation = null; |
| 36 | + int inputCounter = 0; |
| 37 | + |
| 38 | + FileReader fin = new FileReader(args[0]); |
| 39 | + Scanner in = new Scanner(fin); |
| 40 | + |
| 41 | + // read the configuration for the mirror board and the laser |
| 42 | + // TO-DO: to catch all kinds of input file exceptions. |
| 43 | + while (in.hasNextLine()) { |
| 44 | + String nextLine = in.nextLine().trim(); |
| 45 | + if (nextLine.startsWith("-1")) { |
| 46 | + inputCounter++; |
| 47 | + if (in.hasNext()) nextLine = in.nextLine().trim(); |
| 48 | + } |
| 49 | + // read maze size |
| 50 | + if (inputCounter == 0) { |
| 51 | + String mazeSize = nextLine; |
| 52 | + mazeCol = Integer.valueOf(mazeSize.trim().split(",")[0]); |
| 53 | + mazeRow = Integer.valueOf(mazeSize.trim().split(",")[1]); |
| 54 | + } |
| 55 | + // read mirror |
| 56 | + if (inputCounter == 1) { |
| 57 | + mirrors.add(nextLine); |
| 58 | + } |
| 59 | + |
| 60 | + // read laser |
| 61 | + if (inputCounter == 2) { |
| 62 | + String laser = nextLine; |
| 63 | + laserStartCol = Integer.valueOf(laser.substring(0, 1)); |
| 64 | + laserStartRow = Integer.valueOf(laser.substring(2, 3)); |
| 65 | + laserOrientation = new String(laser.substring(3)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + in.close(); |
| 70 | + fin.close(); |
| 71 | + |
| 72 | + Mirror[][] mirrorMaze = new Mirror[mazeCol][mazeRow]; |
| 73 | + for (String m : mirrors) { |
| 74 | + |
| 75 | + Mirror tmpMirror; |
| 76 | + if (m.length() > 4) { |
| 77 | + String d = new String(m.substring(3, 4)); |
| 78 | + String s = new String(m.substring(4)); |
| 79 | + tmpMirror = new Mirror(d, s); |
| 80 | + } else { |
| 81 | + String d = new String(m.substring(3, 4)); |
| 82 | + tmpMirror = new Mirror(d); |
| 83 | + } |
| 84 | + int col = Integer.valueOf(m.substring(0, 1)); |
| 85 | + int row = Integer.valueOf((m.substring(2, 3))); |
| 86 | + mirrorMaze[col][row] = tmpMirror; |
| 87 | + } |
| 88 | + |
| 89 | + mazePath(mirrorMaze, laserStartCol, laserStartRow, laserOrientation); |
| 90 | + } |
| 91 | + |
| 92 | + // find the path of a laser in the maze. |
| 93 | + public static void mazePath(Mirror[][] board, int col, int row, String orientation) { |
| 94 | + // validate the input of the laser |
| 95 | + if (col < 0 || row < 0 |
| 96 | + || col >= board.length |
| 97 | + || row >= board[0].length |
| 98 | + || (!orientation.equals("H") && !orientation.equals("V"))) { |
| 99 | + System.out.println("incorrect input"); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + System.out.println("the demensions of board: " + board.length + " x " + board[0].length); |
| 104 | + |
| 105 | + // track the path of the laser |
| 106 | + ArrayList<Position> path = new ArrayList<Position>(); |
| 107 | + String direction = "+"; // "+": increase step; "-": decrease step |
| 108 | + path.add(new Position(col, row, orientation, direction)); |
| 109 | + |
| 110 | + |
| 111 | + // if last position is out of board, it is finished. |
| 112 | + Position last = path.get(path.size() - 1); |
| 113 | + while ((last.col >= 0 && last.col < board.length) |
| 114 | + && (last.row >= 0 && last.row < board[0].length)) { |
| 115 | + nextPosition(board, path); |
| 116 | + last = path.get(path.size() - 1); |
| 117 | + } |
| 118 | + |
| 119 | + // print the path from start to exit. |
| 120 | + System.out.println("the path of the laser: "); |
| 121 | + for (int i = 0; i < path.size() - 1; i++) { |
| 122 | + Position p = path.get(i); |
| 123 | + System.out.println(p); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + // calculate next position of the laser |
| 129 | + public static void nextPosition(Mirror[][] board, ArrayList<Position> path) { |
| 130 | + Position prev = path.get(path.size() - 1); |
| 131 | + int prevCol = prev.col; |
| 132 | + int prevRow = prev.row; |
| 133 | + String prevOrient = prev.orientation; |
| 134 | + String prevDirection = prev.direction; |
| 135 | + int nextCol = -1; |
| 136 | + int nextRow = -1; |
| 137 | + String nextOrient = prevOrient; |
| 138 | + String nextDirection = prevDirection; |
| 139 | + |
| 140 | + if (prevOrient.equals("H")) { |
| 141 | + nextCol = prevCol + ((prevDirection.equals("+")) ? 1 : -1); |
| 142 | + nextRow = prevRow; |
| 143 | + } |
| 144 | + if (prevOrient.equals("V")) { |
| 145 | + nextRow = prevRow + ((prevDirection.equals("+")) ? 1 : -1); |
| 146 | + nextCol = prevCol; |
| 147 | + } |
| 148 | + |
| 149 | + if ((nextCol >= 0 && nextCol < board.length) |
| 150 | + && (nextRow >= 0 && nextRow < board[0].length)) { |
| 151 | + |
| 152 | + Mirror mirror = board[nextCol][nextRow]; |
| 153 | + if (mirror != null) { |
| 154 | + if (mirror.direction.equals("R")) { |
| 155 | + |
| 156 | + if (mirror.rightSide) { |
| 157 | + if (prevOrient.equals("V") && prevDirection.equals("+")) { |
| 158 | + nextOrient = "H"; |
| 159 | + nextDirection = "+"; |
| 160 | + |
| 161 | + } |
| 162 | + if (prevOrient.equals("H") && prevDirection.equals("-")) { |
| 163 | + nextOrient = "V"; |
| 164 | + nextDirection = "-"; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + if (mirror.leftSide) { |
| 169 | + if (prevOrient.equals("V") && prevDirection.equals("-")) { |
| 170 | + nextOrient = "H"; |
| 171 | + nextDirection = "-"; |
| 172 | + |
| 173 | + } |
| 174 | + if (prevOrient.equals("H") && prevDirection.equals("+")) { |
| 175 | + nextOrient = "V"; |
| 176 | + nextDirection = "+"; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + if (mirror.direction.equals("L")) { |
| 182 | + if (mirror.rightSide) { |
| 183 | + if (prevOrient.equals("V") && prevDirection.equals("-")) { |
| 184 | + nextOrient = "H"; |
| 185 | + nextDirection = "+"; |
| 186 | + |
| 187 | + } |
| 188 | + if (prevOrient.equals("H") && prevDirection.equals("-")) { |
| 189 | + nextOrient = "V"; |
| 190 | + nextDirection = "+"; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (mirror.leftSide) { |
| 195 | + if (prevOrient.equals("V") && prevDirection.equals("+")) { |
| 196 | + nextOrient = "H"; |
| 197 | + nextDirection = "-"; |
| 198 | + |
| 199 | + } |
| 200 | + if (prevOrient.equals("H") && prevDirection.equals("+")) { |
| 201 | + nextOrient = "V"; |
| 202 | + nextDirection = "-"; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + Position next = new Position(nextCol, nextRow, nextOrient, nextDirection); |
| 210 | + for (Position p : path) { |
| 211 | + // check if the laser is trapped in the maze. |
| 212 | + // path is handy for this. |
| 213 | + if (p.equals(next)) |
| 214 | + throw new RuntimeException("the laser is trapped in the maze."); |
| 215 | + } |
| 216 | + path.add(next); |
| 217 | + |
| 218 | + } |
| 219 | +} |
0 commit comments