Google Code Jam Notes-Cross The Maze-Java

Source: Internet
Author: User

Problem: http://code.google.com/codejam/contest/2924486/dashboard#s=p3
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 your 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 stubreturn found;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.