POJ2251 Dungeon Master (java)一直出现Memory Limit Exceeded!!!

题目链接POJ2251

一直 Memory Limit Exceeded,求解答!

//虚拟迷宫以0,0,0为起点
class point1{
	int x,y,z;
	int step;
	public point1(int x, int y, int z, int step) {
		super();
		this.x = x;
		this.y = y;
		this.z = z;
		this.step = step;
	}

	
}

public class Main {
	public static final int MAXN = 32;
	static char maze[][][]=new char[MAXN][MAXN][MAXN]; // level - row - col
	static int level,row,col;
	static int dir[][]=new int[][] {
		{0,1,0},//x,y,z
		{1,0,0},
		{0,-1,0},
		{-1,0,0},
		{0,0,1},
		{0,0,-1},
	};
	static point1 start;
	static point1 end;
	static LinkedList<point1> queue = new LinkedList<point1>();
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			level = sc.nextInt();
			row = sc.nextInt();
			col = sc.nextInt();
			if(level==0) return;
			//注意 i-level j-row  k-col
			for (int i = 0; i < level; i++) {
				for(int j = 0; j < row; j++) {
					char[] charArray = sc.next().toCharArray();
					for( int k = 0; k <col; k++) {
						if(charArray[k]=='S') start=new point1(j, k, i,0);
						maze[i][j][k]=charArray[k];
					}
				}
			}
			bfs();
			
			
		}

	}//main
	private static void bfs() {
			queue.clear();
			maze[start.z][start.x][start.y]='#';
			boolean flag=false;
			queue.offer(start);
			
			while(!queue.isEmpty()) {
				point1 qhead = queue.poll();
				int x=qhead.x;int y=qhead.y;int z=qhead.z;int step=qhead.step;
				if(maze[z][x][y]=='E') {
					end=new point1(x, y, z, step);
					flag=true;
					break;
				}
				for(int i=0;i<6;i++) {
					int nextx=x+dir[i][0];
					int nexty=y+dir[i][1];
					int nextz=z+dir[i][2];
					if(isLegal(nextx,nexty,nextz)) {
						maze[nextz][nextx][nextx]='#';
						queue.offer(new point1(nextx, nexty, nextz, step+1));
					}
				}
			}
			if(flag)
				System.out.println("Escaped in "+end.step+" minute(s).");
			else
				System.out.println("Trapped!");
	}
	private static boolean isLegal(int nextx, int nexty, int nextz) {
		if(nextz>=level||nextz<0||nextx<0||nextx>=row||nexty<0||nexty>=col)
			return false;
		if(maze[nextz][nextx][nexty]=='.'||maze[nextz][nextx][nexty]=='E')
			return true;
		return false;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值