1、给定一个用数字表示的迷宫矩阵,其中-2是入口,-3是出口,-1是障碍物,0表示道路,>0的表示传送门,处于传送门的位置可以像道路一样从上下左右走,相比道路传送门可以一步就传送到另外一个传送门的位置。比如上面的例子:第一行的1可以直接跳到行末的1。
输入:
4 3
1 0 -1 1
-2 0 -1 -3
2 2 0 0
输出:
3
代码如下:
package com.cc.demo; import java.util.*; public class test024 { /** * test: 4 3 1 0 -1 1 -2 0 -1 -3 2 2 0 0 */ // 存储位置 static class Pos{ int x; int y; public Pos(int x, int y) { this.x = x; this.y = y; } } private static final int INF = 2000*2000; // 下,上,右,左 private static int[][] move = new int[][]{ {0,1}, {0,-1}, {1,0}, {-1,0}}; private static Map<Integer, ArrayList<Pos>> map; public static void main(String[] args) { // 这是我自己封装的读矩阵的函数,会先读入行和列,再按照行和列读入矩阵 // 在写笔试的时候可以把一些经常用到的读取函数提前写好, // 可以节省很多处理输入的时间(写笔试的一点小Trick) int[][] matrix = readMatrix(true); for(int i=0; i<matrix.length; i++){ for(int j=0; j<matrix[0].length; j++){ System.out.print(matrix[i][j]+" "); } System.out.println(); } map = new