Google Code Jam Notes - Cross The Maze - Java

Problem:

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, TT test cases follow. Each test case starts with an integer NN 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: sxsyexey. (sxsy) means that Edison is standing on row sx and column sy as his starting cell, (exey) is the exit of the maze. (sxsy) 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. (exey) 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


Input 
 

Output 
 
3
2
.#
#.
1 1 2 2
5
.##.#
.....
...#.
.###.
...#.
1 1 5 3
3
...
.#.
...
1 1 3 3
Case #1: Edison ran out of energy.
Case #2: 22
SEEENSESSSNNNWWSWWSSEE
Case #3: 4
EESS
Note: 
In the 2nd test case after moving 1 cell down from his starting cell, Edison will still be able to lean on the wall at the cell (1,2) by his left hand. 
In the third test case, due to Edison can't touch the wall at cell (2,2) initially, so he has to go east in his first step.

Analysis:
This problem is a depth-first-search (DFS) algorithm, however, we can not implement it recursively. Because it allows 10000 steps, which lets us save too many states.

We can implement it iteratively because at each location, there is only one choice for the robot to move according to the rule, then we don't need to save the previous states,

There are four direction, at each location, we decide its face direction first, and then start from its left hand direction to find whether there is a way.

Time complexity is O(1), since no more than 10000 steps.

My solution: (Your opinion is highly appreciated)

package codeJam.google.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Zhenyi 2013 Dec 21, 2013 12:47:00 PM
 */

enum DIRECTION {
	E, S, W, N
};

public class CrossTheMaze {
	static String path = "";
	static Integer ex;
	static Integer ey;
	static Integer sx;
	static Integer sy;
	static Integer steps;
	static char[][] maze;
	static int N;

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(
				"C:/Users/Zhenyi/Downloads/D-small-practice.in"));
		FileWriter out = new FileWriter(
				"C:/Users/Zhenyi/Downloads/D-small-practice.out");
		// BufferedReader in = new BufferedReader(new
		// FileReader("C:/Users/Zhenyi/Downloads/D-large-practice.in"));
		// FileWriter out = new
		// FileWriter("C:/Users/Zhenyi/Downloads/D-large-practice.out");

		int T = new Integer(in.readLine());

		for (int cases = 1; cases <= T; cases++) {
			N = new Integer(in.readLine());
			maze = new char[N][N];
			for (int i = 0; i < N; i++) {
				maze[i] = in.readLine().toCharArray();
			}

			String[] st = in.readLine().split("\\s");
			sx = new Integer(st[0]) - 1;
			sy = new Integer(st[1]) - 1;
			ex = new Integer(st[2]) - 1;
			ey = new Integer(st[3]) - 1;
			Integer faceDirc = 0;

			if (sx == N - 1 && sy == 0) {
				faceDirc = 1;
			}
			if (sx == N - 1 && sy == N - 1) {
				faceDirc = 2;
			}
			if (sx == 0 && sy == N - 1) {
				faceDirc = 3;
			}
			path = "";
			steps = 0;
			if (findPath(faceDirc)) {
				out.write("Case #" + cases + ": " + steps + "\n");
				out.write(path + "\n");
			} else {
				out.write("Case #" + cases + ": " + "Edison ran out of energy."
						+ "\n");
			}

		}
		in.close();
		out.flush();
		out.close();
	}

	private static boolean findPath(Integer faceDirc) {
		boolean found = false;
		while (steps <= 10000 && !found) {

			if (sx == ex && sy == ey) {
				found = true;
				break;
			}

			int startDirc = (faceDirc + 1 <= 3) ? (faceDirc + 1)
					: (faceDirc - 3);
			boolean findway = false;
			for (int i = 0; i < 4 && !findway; i++) {
				int num = (startDirc - i) % 4 >= 0 ? (startDirc - i)
						: (startDirc - i + 4);
				switch (num) {
				case 0:
					if (sx + 1 < N && maze[sx + 1][sy] == '.') {
						findway = true;
						path = path + "S";
						steps++;
						faceDirc = 0;
						sx++;
					}
					break;
				case 1:
					if (sy + 1 < N && maze[sx][sy + 1] == '.') {
						findway = true;
						path = path + "E";
						steps++;
						faceDirc = 1;
						sy++;
					}
					break;
				case 2:
					if (sx - 1 >= 0 && maze[sx - 1][sy] == '.') {
						findway = true;
						path = path + "N";
						steps++;
						faceDirc = 2;
						sx--;
					}
					break;
				case 3:
					if (sy - 1 >= 0 && maze[sx][sy - 1] == '.') {
						findway = true;
						path = path + "W";
						steps++;
						faceDirc = 3;
						sy--;
					}
					break;
				}

			}
			if (!findway) {
				steps = 20000;
			}
		}
		// TODO Auto-generated method stub
		return found;
	}
}


  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

心跳的节奏eric

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值