Edison, a robot, does not have a right hand or eyes. As a brave robot, he always puts his left hand on the wall no matter he walks or turns around. Because he thinks it is too dangerous, Edison does not walk backward.
Assume that Edison has found himself in a square-shaped maze of NxN square cells which is surrounded by walls from the outside. In the maze, some of the cells are also walls. Edison can only move between two empty cells in four directions, north, south, west and east. In order to get out of the maze, he drafts a plan. He uses his left hand to lean on the wall and goes by following the wall.
Here is the question, is Edison able to get out of the maze in at most 10,000 steps? If he can make it, output the path. By getting out of the maze, he only needs to be in the exit cell. If the starting cell is the same as the exit, Edison won't need to move and can directly get out of the maze.
Input:
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with an integer N. N is the size of the maze. The following N lines, each line contains N characters which may be '.' or '#'. '.' is an empty cell, '#' is a wall. Followed by a line which contains four integers: sx, sy, ex, ey. (sx, sy) means that Edison is standing on row sx and column sy as his starting cell, (ex, ey) is the exit of the maze. (sx, sy) is guaranteed to be at one of the 4 corners of the maze, and Edison can only touch the wall on 4 adjacent cells(not 8) initially. (ex, ey) can be anywhere in the maze. Note that the top-left corner is at position (1,1).
Output:
For each test case, output a line containing "Case #x: y", where x is the case number (starting from 1) and y is "Edison ran out of energy." (without the quotes) if Edison can't reach the exit of the maze in at most 10,000 steps, otherwise y should be the number of steps followed by another line which contains y characters to describe the path (each character should be E for east, S for south, W for west or N for north). There is no character to represent the turning around. We don't care about the turning around steps, please only output the path of how Edison will cross the maze.
Limits:
1 ≤ T ≤ 30.
1 ≤ sx, sy, ex, ey ≤ N.
The starting cell and the exit of the maze will always be an empty cell. And the starting cell and the exit of the maze won't be the same.
Small dataset
2 ≤ N ≤ 10.
Large dataset
2 ≤ N ≤ 100.
Sample:
One Solution:
乍看是一個 Maze 的問題, 但多了一些限制:
在這邊我們使用數字來定義方向: East=0; South=1; West=2; North=3
在這個 Solution 我定義一個類別 Maze 來處理這個問題; 並使用類別 Point 來代表座標. 而該類別定義如下:
- public static class Point{
- // Note that the top-left corner is at position (1,1).
- public int x;
- public int y;
- public Point(int x, int y){this.x = x; this.y = y;}
- /**
- * BD: The east coordination of current Point.
- * @return
- */
- public Tuple east() {return new Tuple(x,y+1);}
- public Tuple west() {return new Tuple(x,y-1);}
- public Tuple south() {return new Tuple(x+1,y);}
- public Tuple north() {return new Tuple(x-1,y);}
- /**
- * BD: Translate
into coordination tuple. - * @param dir: Direction(0-3)
- * @return Coordination Tuple(x,y)
- */
- public Tuple dir(int dir)
- {
- switch(dir)
- {
- case 0: // east
- return east();
- case 1: // south
- return south();
- case 2: // west
- return west();
- case 3: // north
- return north();
- }
- return null;
- }
- /**
- * BD: Given left hand direction and output face direction.
- * Ex. If you left hand is in east, it means you face south.
- * @param lhdir: Left hand direction(0-3)
- * @return Coordination Tuple(x,y)
- */
- public Tuple face(int lhdir)
- {
- switch(lhdir)
- {
- case 0: // east->south
- return south();
- case 1: // south
- return west();
- case 2: // west
- return north();
- case 3: // north
- return east();
- }
- return null;
- }
- /**
- * BD: Move according to given direction.
- * @param dir: Direction to move
- * @return Point after moving.
- */
- public Point move(int dir)
- {
- switch(dir)
- {
- case 0: // east
- //System.out.printf("\t\tMove E\n");
- return new Point(x,y+1);
- case 1: // south
- //System.out.printf("\t\tMove S\n");
- return new Point(x+1,y);
- case 2: // west
- //System.out.printf("\t\tMove W\n");
- return new Point(x,y-1);
- case 3: // north
- //System.out.printf("\t\tMove N\n");
- return new Point(x-1,y);
- }
- return null;
- }
- @Override
- public boolean equals(Object o)
- {
- if(o instanceof Point)
- {
- Point p = (Point)o;
- if(p.x == x && p.y == y) return true;
- }
- return false;
- }
- @Override
- public String toString(){return String.format("(%d,%d)", x,y);}
- }
- public static class Maze{
- public Point start;
- public Point end;
- public List
track; - public List
move; - public int shape[][];
- public int n;
- public Maze(int n, List
shapeList, int sx, int sy, int ex, int ey) - {
- this.n = n;
- start = new Point(sx, sy);
- end = new Point(ex, ey);
- shape = new int[n][n];
- for(int x=0; x
- {
- char[] yaxis = shapeList.get(x).toCharArray();
- for(int y=0; y
- {
- if(yaxis[y]=='.') shape[x][y]= 0;
- else shape[x][y]= 1; // wall
- }
- }
- track = new ArrayList
(); - move = new ArrayList
(); - }
- /**
- * BD: Check given coordination is wall or not.
- * @param x: X axis
- * @param y: Y axis
- * @return True if the given coordination is wall; False otherwise.
- */
- public boolean isWall(int x, int y)
- {
- if(x<0 && x>=n) return true;
- else if(y<0 && y>=n) return true;
- else return shape[x][y]==1;
- }
- /**
- * BD: Check given coordination is wall or not.
- * @param t: Coordination tuple
- * @return True if the given coordination is wall; False otherwise.
- */
- public boolean isWall(Tuple t)
- {
- int x = (Integer)t.get(0)-1;
- int y = (Integer)t.get(1)-1;
- if(x<0 || x>=n) return true;
- else if(y<0 || y>=n) return true;
- else {
- //System.out.printf("\t[Test] n=%d; x=%d; y=%d\n", n ,x, y);
- return shape[x][y]==1;
- }
- }
- /**
- * BD: Output one character to represent given direction(0-3)
- * @param d: Direction(0-3)
- * @return String to represent direction.
- */
- public String dir(int d)
- {
- switch(d)
- {
- case 0: // east
- return "E";
- case 1: // south
- return "S";
- case 2: // west
- return "W";
- case 3: // north
- return "N";
- }
- return null;
- }
- /**
- * BD: Return the direction after turning right. (E->S;S->W;W->N;N->E)
- * @param dir: Current direction
- * @return Direction after turning right.
- */
- public int turnRight(int dir) {return (dir+1)%4;}
- /**
- * BD: Return the direction after turning left. (E->N;N->E;E->S;S->E)
- * @param dir: Current direction
- * @return Direction after turning left.
- */
- public int turnLeft(int dir)
- {
- dir = dir-1;
- if(dir<0) return dir+4;
- return dir;
- }
- /**
- * BD: Start running the maze.
- * @return True if reaching destination; False otherwise.
- * @throws Exception
- */
- public boolean go() throws Exception
- {
- int handDir=-1, moveDir=0;
- track.add(start);
- // Decide left hand direction
- if(isWall(start.east())) handDir=0;
- else if(isWall(start.south())) handDir=1;
- else if(isWall(start.west())) handDir=2;
- else if(isWall(start.north())) handDir=3;
- System.out.printf("\t\tStart Left Hand Dir=%s %s:\n", dir(handDir), start);
- int swd=0; // switch direction
- for(int i=0; i<10000; i++)
- {
- Tuple ft = start.face(handDir);
- if(isWall(ft))
- {
- handDir=turnRight(handDir);
- //System.out.printf("\t\tChange Left Hand Dir=%s\n", dir(handDir));
- swd++;
- if(swd==4) return false;
- continue;
- }
- else swd=0;
- moveDir = turnRight(handDir);
- start = start.move(moveDir);
- move.add(moveDir);
- track.add(start);
- if(start.equals(end)) return true;
- if(!isWall(start.dir(handDir))) // Turn around
- {
- start = start.move(handDir);
- move.add(handDir);
- track.add(start);
- handDir = turnLeft(handDir);
- }
- if(start.equals(end)) return true;
- }
- return false;
- }
- public String trackStr()
- {
- StringBuffer strBuf = new StringBuffer();
- for(int i:move) strBuf.append(dir(i));
- return strBuf.toString();
- }
- @Override
- public String toString()
- {
- StringBuffer strBuf = new StringBuffer();
- for(int x=0; x
- {
- for(int y=0; y
- {
- Point p = new Point(x+1,y+1);
- if(start.equals(p)) strBuf.append("S");
- else if(end.equals(p)) strBuf.append("E");
- else
- {
- if(shape[x][y]==0) strBuf.append(".");
- else strBuf.append("#");
- }
- }
- strBuf.append("\n");
- }
- return strBuf.toString();
- }
- }
而讀入輸入, 跑迷宮, 輸出結果代碼如下:
- /**
- * BD: https://code.google.com/codejam/contest/2924486/dashboard#s=p3
- * @param args
- */
- public static void main(String[] args) throws Exception{
- String fn="D-small-practice.in";
- QSReader qsr = new QSReader(String.format("data/2014/%s", fn));
- QSWriter qsw = new QSWriter(String.format("data/2014/%s.out", fn));
- qsr.open();
- Integer pc = Integer.valueOf(qsr.line());
- System.out.printf("\t[Info] Total %d question...\n", pc);
- for(int i=1; i<=pc; i++)
- {
- int n = Integer.valueOf(qsr.line());
- List
shape = new ArrayList (); - for(int j=0; j
// read maze - String pos[] = qsr.line().split(" "); // read start/end
- Maze maze = new Maze(n, shape, Integer.valueOf(pos[0]), Integer.valueOf(pos[1]), Integer.valueOf(pos[2]), Integer.valueOf(pos[3]));
- System.out.printf("\t\tP%d: Start(%s,%s), End(%s,%s)\n%s", i, pos[0],pos[1],pos[2],pos[3], maze);
- if(maze.go())
- {
- String track = maze.trackStr();
- qsw.line(String.format("Case #%d: %d\n%s", i, track.length(), track));
- }
- else
- {
- qsw.line(String.format("Case #%d: Edison ran out of energy.", i));
- }
- }
- qsr.close();
- qsw.close();
- }
* [ Algorithm ] Maze problem - Using Online DFS Search
* [ Algorithm ] Maze problem - Using Online LRTA*
沒有留言:
張貼留言