kuangbin 专题一 简单搜索(POJ 2251)Dungeon Master

Dungeon Master

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

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

  • Input
The input consists of a number of dungeons. Each dungeon description starts with a
line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each
character describes one cell of the dungeon. A cell full of rock is indicated by a 
'#' and empty cells 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. 
Input is terminated by three zeroes for L, R and C. 
  • Output
Each maze generates one line of output. If it is possible to reach the exit, print 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, print 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!

地下城大师

你被困在一个3D地牢里,需要找到最快捷的出路!地牢由单位立方体组成,可以填充或不填充岩石。向北,向南,向东,向西,向上或向下移动一个单位需要一分钟。你不能对角移动,迷宫的四周都是坚硬的岩石。
逃脱可能吗?如果可能,需要多长时间?

  • 输入

输入包含许多地下城。每个地下城描述都以包含三个整数L,R和C(全部限制为30个大小)的行开头。
L是组成地牢的等级数。
R和C是构成每个级别计划的行数和列数。
然后将跟随L个R行,每行包含C个字符。每个角色描述一个地牢的一个单元格。充满岩石的单元格用’#‘表示,空单元格用’’'表示。您的起始位置用’S’表示,出口用’E’表示。每个级别后面都有一个空白行。对于L,R和C,输入以三个零结束。

  • 输出
每个迷宫产生一行输出。如果可以到达出口,请打印
Escaped in x minute(s). 
其中x被逃逸所需的最短时间所取代。 
如果无法逃脱,请打印 
Trapped! 
  • 样例输入
3 4 5
S....
.###.
.##..
###.#
        
#####
#####
##.##
##...
        
#####
#####
#.###
####E
        
1 3 3
S##
#E#
###
        
0 0 0
  • 样例输出
Escaped in 11 minute(s).
Trapped!

纪念一下第一道完全靠自己写的BFS题

顺便这个本来能一遍过的代码WA得莫名其妙

用各种例子试了三个多小时到心态爆炸…然后在某大兄弟的帮助下发现是被困的时间上限开小了??

我之前还改过这个然后发现没用以为不是这的问题,结果是因为我还是开小了

对不起是我太单纯!!

#include <stdio.h>
#include <string.h>

char a[50][50][50];
int b[50][50][50];/*储存走到该单元格所需时间*/
int time = 0, l, r, c, sh, si, sj, eh, ei, ej;

int Judge(int h, int i, int j) {
  int f = 0;
  if(h < 0 || i < 0 || j < 0 || h >= l || i >= r || j >= c) f = 1;/*判断是否越界*/
  else if(a[h][i][j] == '#') f = 1;/*判断是否为空单元格*/
    else if(b[h][i][j]) f = 1;/*判断是否已搜到过*/
  return f;
}
void  bl(int x, int y, int z) {/*遍历函数*/
    x ++;
    if(!Judge(x, y, z)) b[x][y][z] = time + 1;
    x -= 2;
    if(!Judge(x, y, z)) b[x][y][z] = time + 1;
    x ++;
    y ++;
    if(!Judge(x, y, z)) b[x][y][z] = time + 1;
    y -= 2;
    if(!Judge(x, y, z)) b[x][y][z] = time + 1;
    y ++;
    z ++;
    if(!Judge(x, y, z)) b[x][y][z] = time + 1;
    z -= 2;
    if(!Judge(x, y, z)) b[x][y][z] = time + 1;
  }
int Bfs() {
  int h, i, j;
  if(b[eh][ei][ej]) return b[eh][ei][ej];/*搜到E后返回b数组E所在单元格的值*/
  time ++;
  for (h = 0; h < l; h ++) {
    for (i = 0; i < r; i ++) {
      for(j = 0; j < c; j ++) {
        if(b[h][i][j] == time) bl(h, i, j);/*按时间搜索保证最短时间*/
      }
    }
  }
  if(time > 10000) return 0;/*被困返回0*/
  Bfs();
}

int main(int argc, char const *argv[]) {
  while (scanf("%d %d %d", &l, &r, &c) && l != 0) {
    time = 0;
    memset(a, '#', sizeof(a));
    memset(b, 0, sizeof(b));/*清零*/
    for (int h = 0; h < l; h ++) {
      for (int i = 0; i < r; i ++) {
        scanf("%s", a[h][i]);
        for(int j = 0;j < c;j ++) {
          if(a[h][i][j] == 'S') sh = h, si = i, sj = j;
          if(a[h][i][j] == 'E') eh = h, ei = i, ej = j;/*记住S和E的位置*/
    }
      }
      getchar();/*读取空行*/
    }
    bl(sh, si, sj);
    if(Bfs()) printf("Escaped in %d minute(s).\n", b[eh][ei][ej]);
    else printf("Trapped!\n");
  }
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值