zoukankan      html  css  js  c++  java
  • Dungeon Master

    poj2251:http://poj.org/problem?id=2251

    题意:给你一个三维的立方体,然后给你一个起点,和终点的坐标。然后让你求从起点到终点的最短路程。
    题解:该题就是求三维的最短路,可以采用BFS,三维的BFS。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    using namespace std;
    int counts[32][32][32];//记录到起点的最短距离
    struct Node{
       int x;
       int y;
       int z;
       int step;
    };
    int n,m,l;//长,宽,高
    char map1[32][32][32];//原来的三维的地图
    int startx,starty,startz;//起点坐标
    int endx,endy,endz;//终点坐标
    int BFS(int x,int y,int z){
         int xxx[6]={0,0,0,0,-1,1};
         int yyy[6]={0,0,-1,1,0,0};
         int zzz[6]={-1,1,0,0,0,0};//六个方向的对应的3个坐标
         for(int i=1;i<=l;i++)
            for(int j=1;j<=n;j++)
              for(int k=1;k<=m;k++)//初始化
                 counts[i][j][k]=10000000;
         counts[x][y][z]=0;//注意这里的初始化
         queue<Node>Q;
         Node tt;
         tt.x=x;tt.y=y;tt.z=z;tt.step=0;
         Q.push(tt);
        while(!Q.empty()){
            Node temp=Q.front();
            Q.pop();
            int xx=temp.x;
            int yy=temp.y;
            int zz=temp.z;
            int step=temp.step;
            for(int i=0;i<6;i++){//六个方向进行搜索
                if(xx+xxx[i]>=1&&xx+xxx[i]<=n&&yy+yyy[i]>=1&&yy+yyy[i]<=m&&zz+zzz[i]>=1&&zz+zzz[i]<=l){
                    if(map1[zz+zzz[i]][xx+xxx[i]][yy+yyy[i]]!='#'&&step+1<counts[zz+zzz[i]][xx+xxx[i]][yy+yyy[i]]){
                        counts[zz+zzz[i]][xx+xxx[i]][yy+yyy[i]]=step+1;
                        Node ttt;
                        ttt.x=xx+xxx[i];
                        ttt.y=yy+yyy[i];
                        ttt.z=zz+zzz[i];
                        ttt.step=step+1;
                        Q.push(ttt);
                    }
                }
            }
        }
      return counts[endz][endx][endy];//返回终点距离
    
    }
    int main(){
       while(~scanf("%d%d%d",&l,&n,&m)&&l&&n&&m){
           for(int i=1;i<=l;i++){
            for(int j=1;j<=n;j++){
                for(int k=1;k<=m;k++){
                    cin>>map1[i][j][k];
                    if(map1[i][j][k]=='S'){
                        startz=i;
                        startx=j;
                        starty=k;
                    }
                     if(map1[i][j][k]=='E'){
                        endz=i;
                        endx=j;
                        endy=k;
                    }
                }
            }
           }
           int ans=BFS(startx,starty,startz);
           if(ans==10000000)
            printf("Trapped!
    ");
           else
           printf("Escaped in %d minute(s).
    ",ans);
       }
    
    
    }
    View Code
  • 相关阅读:
    汉化DevExpress
    《苹果往事》的台式翻译
    说说《程序员》杂志的排版
    关于量化考核绩效
    as3 浅复制 深复制
    斜视角的讨论(转)
    斜角地图原理解释及斜角图形绘制实例细述(转)
    垃圾回收测试
    Flash务实主义(八)——减少数据传输量(转)
    翻译]游戏主循环
  • 原文地址:https://www.cnblogs.com/chujian123/p/3530366.html
Copyright © 2011-2022 走看看