Google Code Jam Notes - Dragon Maze - Java

Problem:


You are the prince of Dragon Kingdom and your kingdom is in danger of running out of power. You must find power to save your kingdom and its people. An old legend states that power comes from a place known as Dragon Maze. Dragon Maze appears randomly out of nowhere without notice and suddenly disappears without warning. You know where Dragon Maze is now, so it is important you retrieve some power before it disappears.

Dragon Maze is a rectangular maze, an N x M grid of cells. The top left corner cell of the maze is (0,0) and the bottom right corner is (N-1, M-1). Each cell making up the maze can be either a dangerous place which you never escape after entering, or a safe place that contains a certain amount of power. The power in a safe cell is automatically gathered once you enter that cell, and can only be gathered once. Starting from a cell, you can walk up/down/left/right to adjacent cells with a single step.

Now you know where the entrance and exit cells are, that they are different, and that they are both safe cells. In order to get out of Dragon Maze before it disappears, you must walk from the entrance cell to the exit cell taking as few steps as possible. If there are multiple choices for the path you could take, you must choose the one on which you collect as much power as possible in order to save your kingdom.

Input

The first line of the input gives the number of test cases, TT test cases follow.

Each test case starts with a line containing two integers N and M, which give the size of Dragon Maze as described above. The second line of each test case contains four integers enxenyexxexy, describing the position of entrance cell (enx, eny) and exit cell (exx, exy). Then N lines follow and each line has M numbers, separated by spaces, describing the N x M cells of Dragon Maze from top to bottom. Each number for a cell is either -1, which indicates a cell is dangerous, or a positive integer, which indicates a safe cell containing a certain amount of power.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1). If it's possible for you to walk from the entrance to the exit, y should be the maximum total amount of power you can collect by taking the fewest steps possible. If you cannot walk from the entrance to the exit, y should be the string "Mission Impossible." (quotes for clarity). Please note that the judge requires an exact match, so any other output like "mission impossible." or "Mission Impossible" (which is missing the trailing period) will be judged incorrect.

Limits

The amount of power contained in each cell will not exceed 10,000.
1 ≤ T ≤ 30.
0 ≤ enxexx < N.
0 ≤ enyexy < M.

Small dataset

1 ≤ NM ≤ 10.

Large dataset

1 ≤ NM ≤ 100.

Sample


Input 
 

Output 
 
2
2 3
0 2 1 0
2 -1 5
3 -1 6
4 4
0 2 3 2
-1 1 1 2
1 1 1 1
2 -1 -1 1
1 1 1 1
Case #1: Mission Impossible.
Case #2: 7

Analysis:

Use Breadth First Search (BFS) algorithm to solve this problem, its difference between classical BFS algorithm is to find the largest value if there are multiple shortest path, so we need to update the value matrix accordingly. 


Time Complexity O(MN).


My solution: (Your opinion is highly appreciated)

package codeJam.google.com;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author Zhenyi 2013 Dec 26, 2013 3:34:07 PM
 */
public class DragonMaze {
	public static void main(String[] args) throws IOException {
		// Scanner in = new Scanner(new File(
		// "C:/Users/Zhenyi/Downloads/D-small-practice.in"));
		// FileWriter out = new FileWriter(
		// "C:/Users/Zhenyi/Downloads/D-small-practice.out");
		Scanner in = new Scanner(new File(
				"C:/Users/Zhenyi/Downloads/D-large-practice.in"));
		FileWriter out = new FileWriter(
				"C:/Users/Zhenyi/Downloads/D-large-practice.out");

		int T = in.nextInt();

		for (int cases = 1; cases <= T; cases++) {
			int N = in.nextInt();
			int M = in.nextInt();
			int enx = in.nextInt();
			int eny = in.nextInt();
			int exx = in.nextInt();
			int exy = in.nextInt();
			int[][] maze = new int[N][M];
			int[][] value = new int[N][M];
			boolean[][] mark = new boolean[N][M];
			int[][] steps = new int[N][M];
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < M; j++) {
					maze[i][j] = in.nextInt();
					value[i][j] = maze[i][j];
				}
			}

			LinkedList<Integer[]> q = new LinkedList<Integer[]>();
			q.add(new Integer[] { enx, eny });
			boolean found = false;
			int path[][] = new int[][] { { -1, 0 }, { 0, -1 }, { 1, 0 },
					{ 0, 1 } };
			while (q.size() > 0) {
				Integer[] a = new Integer[2];
				a = q.poll();
				int x = a[0];
				int y = a[1];
				mark[x][y] = true;
				if ((x == exx) && (y == exy)) {
					found = true;
					break;
				}
				for (int i = 0; i < 4; i++) {
					int nextx = x + path[i][0];
					int nexty = y + path[i][1];
					if ((nextx >= 0) && (nexty >= 0) && (nextx < N)
							&& (nexty < M) && !mark[nextx][nexty]
							&& maze[nextx][nexty] != -1) {
						if ((steps[nextx][nexty] == 0)
								|| steps[x][y] + 1 <= steps[nextx][nexty]) {
							if (steps[nextx][nexty] == 0) {
								q.add(new Integer[] { nextx, nexty });
								steps[nextx][nexty] = steps[x][y] + 1;
							}
							if (value[nextx][nexty] < value[x][y]
									+ maze[nextx][nexty]) {
								value[nextx][nexty] = value[x][y]
										+ maze[nextx][nexty];
							}
						}
					}
				}
			}

			if (found) {
				out.write("Case #" + cases + ": " + value[exx][exy] + "\n");
			} else {
				System.out.print("Case #" + cases + ": Mission Impossible.\n");
			}
		}
		in.close();
		out.flush();
		out.close();
	}
}


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

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

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

打赏作者

心跳的节奏eric

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

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

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

打赏作者

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

抵扣说明:

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

余额充值