3D迷宫(啊啊啊我要预习)java版

Juruo is an adventurous person. Now he is trapped in a perplexing 3D maze. Due to the air is damp crisp, he has to find a quickest way out. The 3D maze is made up of unit cubes. Some cubes are filled with snakes while others are not. Snakes!!! It will take one minute to take a unit move. He can choose to go north, south, east, west, up or down, but he cannot move diagonally. 

Is an escape possible? If yes, how long will it take? 

Input

The input contains a number of 3D mazes. Each description of maze starts with a line containing three integers L, R and C (L, R, C <= 30). 
L: The number of levels makes up the maze. 
R and C: The number of rows and columns maks up the plane of each level. 
Then there will follow L blocks of R lines, and each containing C characters. Each character describes one cube of the maze. A cube full of snakes is indicated by a '#' and empty cubes are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. The Input terminates when L = R = C = 0.

Output

Each maze generates one line of output. If it is possible to reach the exit, output a line of the form: 

Escaped in x minute(s).


where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, output the line: 

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Hint

BFS

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Node {
    int x,y,z;
    int step;
    Node() {}
    Node(int z,int y,int x,int step) {
        this.z = z;
        this.y = y;
        this.x = x;
        this.step = step;
    }
    Node(int z,int y,int x) {
        this.z = z;
        this.y = y;
        this.x = x;
    }
}
public class Main {
    static int high,row,cloum;//高,行,列
    static int[][][] map;//0表示能走,1表示不能走
    static boolean[][][] vis;
    static Queue<Node> q;
    static Node start,end;
    static int[][] move = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
    static boolean check(int x,int y,int z) {
        return x >= 0 && x < cloum && y >= 0 && y < row && z >= 0 && z < high && map[z][y][x] != 1 && !vis[z][y][x];
    }
    
    static void bfs() {
        Node cur,new_cur;
        vis[start.z][start.y][start.x] = true;
        q = new LinkedList<Node>();
        q.add(start);
        while(!q.isEmpty()) {
            cur = new Node(q.peek().z,q.peek().y,q.peek().x,q.poll().step);
            if(cur.x == end.x && cur.y == end.y && cur.z == end.z) {
                System.out.println("Escaped in " + cur.step + " minute(s).");
                return;
            }
            for(int i = 0;i < 6;i++) {
                new_cur = new Node(cur.z,cur.y,cur.x,cur.step);
                new_cur.x += move[i][0];
                new_cur.y += move[i][1];
                new_cur.z += move[i][2];
                if(check(new_cur.x,new_cur.y,new_cur.z)) {
                    vis[new_cur.z][new_cur.y][new_cur.x] = true;
                    new_cur.step++;
                    q.add(new_cur);
                }
            }
        }
        System.out.println("Trapped!");
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            high = cin.nextInt();
            row = cin.nextInt();
            cloum = cin.nextInt();
            vis = new boolean[high][row][cloum];
            if(high == 0)
                break;
            map = new int[high][row][cloum];
            String tmp;
            for(int z = 0;z < high;z++) {//高
                for(int y = 0;y < row;y++) {//行
                    tmp = cin.next();
                    for(int x = 0;x < cloum;x++) {//列
                        if(tmp.charAt(x) == 'S') 
                            start = new Node(z,y,x,0);
                        else if(tmp.charAt(x) == 'E') 
                            end = new Node(z,y,x);
                        else if(tmp.charAt(x) == '#')
                            map[z][y][x] = 1;
                    }
                }
            }
            bfs();
        }
    }
}

 

前面几日我重新实现了迷宫的自动生成方法,并且添加了迷宫自动寻路方法。有了一个独立于GUI的迷宫库,我开始迫不及待地实现三维迷宫了! 下面是我在开发迷宫程序中遇到的问题。 1、怎样将迷宫类的行和列映射到真实的三维坐标中?迷宫应该用哪个参考系来描述? 其实我在制作的时候为了简化,将二维迷宫的左上角与三维的原点重合,二维迷宫的右对应三维的X轴正方向,迷宫的下对应Z轴的正方向。 2、迷宫的“上、下、左、右”在三维中应该叫做什么? 在确定好迷宫的位置后,我们将迷宫的上对应Z轴的负半轴,下对应Z轴的正半轴,左对应X轴的负半轴,右对应Y轴的正半轴。 3、三维点绘制顺序以及OpenGL裁剪模式造成的一些面不可见问题。 这个问题是我在编写二维迷宫没有想到的。主要是因为二维迷宫中描述墙是用一条直线,而到了三维则是一个面。由于在OpenGL中有裁剪模式可以选择,我使用了 glFrontFace( GL_CW ); // 顺时针的绘制为正面 glEnable( GL_CULL_FACE ); // 剔除不是正面的面 进行设定,也就是说,所有在摄像机看来是逆时针绘制的图形都无法显示。因此我不得不用同样的顶点绘制两个面。下面是相关的函数: void DrawInnerWall( Point3F& p1, Point3F& p2, Point3F& p3, Point3F& p4 ) { glTexCoord2f( 0.0f, 1.0f ); glVertex3fv( p1 ); glTexCoord2f( 1.0f, 1.0f ); glVertex3fv( p2 ); glTexCoord2f( 1.0f, 0.0f ); glVertex3fv( p3 ); glTexCoord2f( 0.0f, 0.0f ); glVertex3fv( p4 ); } void DrawOuterWall( Point3F& p1, Point3F& p2, Point3F& p3, Point3F& p4 ) { glTexCoord2f( 1.0f, 1.0f ); glVertex3fv( p1 ); glTexCoord2f( 0.0f, 1.0f ); glVertex3fv( p2 ); glTexCoord2f( 0.0f, 0.0f ); glVertex3fv( p3 ); glTexCoord2f( 1.0f, 0.0f ); glVertex3fv( p4 ); } 在编写这些函数的时候尤其注意纹理坐标的绘制顺序。可以在纸上绘制一个草图。下面是我调用的代码: if ( Cell_UpWall( cell ) ) { DrawInnerWall( p8, p7, p3, p4 ); DrawOuterWall( p7, p8, p4, p3 ); } 4、怎样设置阻挡? 设置阻挡的基本原则还是先检测后执行。首先我先尝试着执行走一步,再判断这一步是不是出现了越界问题。如果出现了越界问题,那么不执行这一步,否则执行这一步。为了不让我们无限地靠近墙,我设定了一个gap,即摄像机必须与墙保持gap的距离。 下面是我相关的代码: bool View3D::CanGo( Maze& maze, float step ) { static float gap = m_CellSize.w / 8.0f;// 摄像机与墙最近不能超过的间隔 const Point3F& pos = m_Camera.Pos( ); Point3F tryPos; if ( pos.y > 0 && pos.y ( row - 1 ) * m_CellSize.w ); if (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuoyanli

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值