• poj 2251 Dungeon Master(简单三维广搜)


    题意:

             之前做过的所有题都是   在一个平面   上搜索 。

             本题很新,在一个三维空间里 ,首先   l  x  y   分别代表 l 层   每一层有 x 行   y 列  

             问从 S 开始   走到 E   最小步是多少    显然用广搜,只是多了一个向上向下的搜索。

    注意:

            所谓广搜  ,是一层一层的搜,  每一层其步数是一样的   ,可以通过建立一个数组存步数 ,也可以 将步数同时存入队列中

            另外搜过的要做标记, 在做标记时  尽量要在里面做标记(就是每搜过一个点就  立马 将之标记)不然一个点也会多次进入队列  ,会爆内存。

           如: 假设队列里为a,b, 此时 a出队列  1,2,3,4 进队列 ,第二次 b出队列   ,如果1不在里面标记的话  ,1就会又一次进队列,显然多余!!

                                       要知道一个点  第一次   被访问的时候所  用的步数  一定是最小的,

                                       所以   以后  该点再被访问的话    肯定步数比第一次大   ,就没必要再进行它之后的路径搜索  ,即就没必要进队列。

    小改善:

            可以通过定义数组   模拟   队列  的先进先出

            可以   把要进队列的所有内容    存入结构体    直接push结构体    直接front结构体    更简单

            可以   把下面代码bfs里的   6个方向搜索   合并   成   一个   for循环

      1 #include <algorithm>
      2 #include <iostream>
      3 #include <sstream>
      4 #include <cstdlib>
      5 #include <cstring>
      6 #include <cstdio>
      7 #include <string>
      8 #include <bitset>
      9 #include <vector>
     10 #include <queue>
     11 #include <stack>
     12 #include <cmath>
     13 #include <list>
     14 #include <map>
     15 #include <set>
     16 using namespace std;
     17 /*10^8-----1s*/
     18 /***************************************/
     19 typedef vector<int> VI;
     20 typedef vector<char> VC;
     21 typedef vector<string> VS;
     22 typedef set<int> SI;
     23 typedef set<string> SS;
     24 typedef map<int ,int> MII;
     25 typedef map<string,int> MSI;
     26 typedef pair<int,int> PII;
     27 typedef vector<PII> VII;
     28 typedef vector<VI > VVI;
     29 /***************************************/
     30 #define min(a,b) (a>b?b:a)
     31 #define max(a,b) (a>b?a:b)
     32 
     33 #define clr(a,b) memset(a,b,sizeof(a))
     34 #define all(x)    (x).begin(), (x).end()
     35 #define sz(x) ((int)(x).size())
     36 #define ll long long
     37 #define int64 __int64
     38 #define pb push_back
     39 #define mp make_pair
     40 #define LL(x) ((x)<<1)
     41 #define RR(x) ((x)<<1|1)
     42 #define ri(x) scanf("%d",&x)
     43 #define rii(x,y) scanf("%d%d",&x,&y)
     44 #define rd(x) scanf("%lf",&x)
     45 #define rdd(x,y) scanf("%lf%lf",&x,&y)
     46 #define rs(x) scanf("%s",x)
     47 #define pi(x) printf("%d",x)
     48 #define pin(x) printf("%d
    ",x)
     49 #define ps(x) printf("%s",x)
     50 #define pn()  printf("
    ")
     51 #define sqr(x) ((x)*(x))
     52 #define rep(i,a,b)  for(int i=(a);i<(b);i++)
     53 #define repu(i,a,b) for(int i=(a);i<=(b);i++)
     54 #define repd(i,a,b) for(int i=(a);i>=(b);i--)
     55 #define repc(i,a,c) for(int i=(a);(c);i++)
     56 /***************************************/
     57 const int INF = 0x7f7f7f7f;
     58 const double eps = 1e-8;
     59 const double PIE=acos(-1.0);
     60 const int dx[]= {0,-1,0,1};
     61 const int dy[]= {1,0,-1,0};
     62 const int fx[]= {-1,-1,-1,0,0,1,1,1};
     63 const int fy[]= {-1,0,1,-1,1,-1,0,1};
     64 /***************************************/
     65 void openfile()
     66 {
     67     freopen("data.in","rb",stdin);
     68     freopen("data.out","wb",stdout);
     69 }
     70 /**********************The End OF The Template*****************/
     71 
     72 char map_[33][33][33];
     73 int l,x,y;
     74 
     75 int eh,ei,ej;//E点所在的坐标
     76 
     77 int cnt,flag;//cnt记录步数
     78 
     79 
     80 //进行广搜
     81 void bfs(int h,int xx,int yy)
     82 {
     83     queue<int >q;//广搜   首先定义队列
     84     q.push(h);
     85     q.push(xx);
     86     q.push(yy);
     87     q.push(0);
     88     flag=0;
     89     while(!q.empty())
     90     {
     91         // ###################### 
     92         int h1=q.front();
     93         q.pop();
     94         int x1=q.front();
     95         q.pop();
     96         int y1=q.front();
     97         q.pop();
     98         cnt=q.front();
     99         q.pop();
    100 
    101         if(eh==h1&&ei==x1&&ej==y1)
    102         {
    103             flag=1;
    104             return;
    105         }
    106 
    107         /*//内存超限的原因
    108            if(eh==h1&&ei==x1&&ej==y1)
    109          {
    110              flag=1;
    111              return;
    112          }
    113          else
    114              map_[h1][x1][y1]='#';//在外面做标记!!!容易重复访问,并重复存入队列
    115         */
    116 
    117         if(h1+1<l&&map_[h1+1][x1][y1]!='#')//上搜
    118         {
    119             map_[h1+1][x1][y1]='#';//在里面  做标记!!!!!!
    120             q.push(h1+1);
    121             q.push(x1);
    122             q.push(y1);
    123             q.push(cnt+1);
    124         }
    125         if(h1-1>=0&&map_[h1-1][x1][y1]!='#')//下搜
    126         {
    127             map_[h1-1][x1][y1]='#';
    128             q.push(h1-1);
    129             q.push(x1);
    130             q.push(y1);
    131             q.push(cnt+1);
    132         }
    133         if(x1+1<x&&map_[h1][x1+1][y1]!='#')//左搜
    134         {
    135             map_[h1][x1+1][y1]='#';
    136             q.push(h1);
    137             q.push(x1+1);
    138             q.push(y1);
    139             q.push(cnt+1);
    140         }
    141         if(x1-1>=0&&map_[h1][x1-1][y1]!='#')//右搜
    142         {
    143             map_[h1][x1-1][y1]='#';
    144             q.push(h1);
    145             q.push(x1-1);
    146             q.push(y1);
    147             q.push(cnt+1);
    148         }
    149         if(y1+1<y&&map_[h1][x1][y1+1]!='#')//前搜
    150         {
    151             map_[h1][x1][y1+1]='#';
    152             q.push(h1);
    153             q.push(x1);
    154             q.push(y1+1);
    155             q.push(cnt+1);
    156         }
    157         if(y1-1>=0&&map_[h1][x1][y1-1]!='#')//后搜
    158         {
    159             map_[h1][x1][y1-1]='#';
    160 
    161             q.push(h1);
    162             q.push(x1);
    163             q.push(y1-1);
    164             q.push(cnt+1);
    165         }
    166         // ###################### 
    167     }
    168 }
    169 
    170 int main()
    171 {
    172 
    173     int jh,ji,jj;
    174     int h,i,j;
    175     while(scanf("%d %d %d",&l,&x,&y)!=EOF)
    176     {
    177         if(l==0&&x==0&&y==0)
    178             break;
    179             
    180         /*输入*/
    181         for(h=0; h<l; h++)
    182         {
    183             for(i=0; i<x; i++)
    184             {
    185                 getchar();
    186                 for(j=0; j<y; j++)
    187                 {
    188                     scanf("%c",&map_[h][i][j]);
    189                     if(map_[h][i][j]=='S')
    190                     {
    191                         jh=h;
    192                         ji=i;
    193                         jj=j;
    194                     }
    195                     if(map_[h][i][j]=='E')
    196                     {
    197                         eh=h;
    198                         ei=i;
    199                         ej=j;
    200                     }
    201                 }
    202             }
    203             getchar();
    204         }
    205         
    206         /*进行搜索*/
    207         map_[jh][ji][jj]='#';
    208         bfs(jh,ji,jj);
    209         
    210         /*输出*/
    211         if(!flag)
    212             printf("Trapped!
    ");
    213         else
    214             printf("Escaped in %d minute(s).
    ",cnt);
    215             
    216     }
    217     return 0;
    218 }
    View Code
  • 相关阅读:
    ELF文件格式解析
    了解ELF文件类型
    汇编程序-简介
    汇编指令入门级整理
    双系统: Windows10时间快和Ubuntu时间差问题
    iDRAC on DELL Server
    ubuntu WIFI 连接不稳定(wifi无法连接问题)
    删除字符串中的空格(空字符)
    VBS 基础知识
    VBS 操作Excel的常用方法
  • 原文地址:https://www.cnblogs.com/bibier/p/3996590.html
Copyright © 2020-2023  润新知