NYOJ 353 3D dungeon

3D dungeon

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
            描述
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? 
输入
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.
输出
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!
样例输入
3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!

分析:

3D地牢中的一个人从S向E逃跑,求最短时间,bfs

#include<stdio.h>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
char cMap[32][32][32];
int vis[32][32][32];
int iD[][3] = {{-1, 0, 0},{1, 0, 0},{0, -1, 0}, {0, 0, -1}, {0, 1, 0}, {0, 0, 1}}; //上下左右前后的偏移量
int c, n, m;
int iMin;
struct dian{
    int x, y, z;
    int iCnt;
} now, next;
dian sS, sE;
bool canput(dian cc){   //判断此位置能否走
    int x = cc.x, y = cc.y, z = cc.z;
    if(!vis[x][y][z] && x >= 0 && x < c && y >= 0 && y < n && z >= 0 && z < m &&
        (cMap[x][y][z] == '.' || cMap[x][y][z] == 'E'))   //不要忘了目标位置能走,找了半天,把它给忘了
        return true;
    else return false;
}
void bfs(dian cur){
    queue<dian> q;
    cur.iCnt = 0;
    q.push(cur);         //始点进队列
    vis[cur.x][cur.y][cur.z] = 1;
    while(!q.empty()){
        now = q.front();   //取首元素
        if(now.x == sE.x && now.y == sE.y && now.z == sE.z){
            iMin = now.iCnt;
            return;
        }
        for(int i = 0; i < 6; i ++){   //向6个方向试探
            next.x = now.x + iD[i][0];
            next.y = now.y + iD[i][1];
            next.z = now.z + iD[i][2];
            next.iCnt = now.iCnt + 1;
            if(canput(next)){
                q.push(next);
                vis[next.x][next.y][next.z] = 1;
            }
        }
        q.pop();
    }
}
int main(){
    while(~scanf("%d%d%d", &c, &n, &m) && (c || n || m)){
        iMin = 999999;
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < c; i ++)     //c层
            for(int j = 0; j < n; j ++){   //每层n行
                scanf("%s", cMap[i][j]);
                for(int k = 0; k < m; k ++)
                    if(cMap[i][j][k] == 'S'){   //找起点
                        sS.x = i;
                        sS.y = j;
                        sS.z = k;
                    }
                    else if(cMap[i][j][k] == 'E'){   //终点
                        sE.x = i;
                        sE.y = j;
                        sE.z = k;
                    }
            }
        bfs(sS);
        if(iMin == 999999) cout <<"Trapped!\n";
        else printf("Escaped in %d minute(s).\n", iMin);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值