题意:给你一个高L长R宽C的图形。每个坐标都能够视为一个方格。你一次能够向上。下。左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动。

问:从S出发能不能到达E,假设能请输出最少的移动次数。

策略:简单的深搜。

注意:由于是求最少的移动次数。所以要从全部能到达的中选出最少的。

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
char map[35][35][35];
int ans, l, r, c;
const int dirx[6] = {1, -1, 0, 0, 0, 0};
const int diry[6] = {0, 0, 1, -1, 0, 0};
const int dirz[6] = {0, 0, 0, 0, 1, -1};
struct node{
int x, y, z;
int step;
};
node st, en;
queue<node > q;
char s[10000];
bool vis[35][35][35]; int limit(node s){
return (s.x<l&&s.x>=0&&s.y>=0&&s.y<r&&s.z>=0&&s.z<c&&map[s.x][s.y][s.z] != '#');
} int match(node s){
if(s.x==en.x&&s.y==en.y&&s.z==en.z) return 1;
else return 0;
}
void bfs(){
memset(vis, 0, sizeof(vis));
ans = INF; //初始化最大
int i;
q.push(st);
//map[st.x][st.y][st.z] = '#';
vis[st.x][st.y][st.z] = 1;
while(!q.empty()){
node temp = q.front(); for(i = 0; i < 6; i ++){
node temp2;
temp2.x = temp.x+dirx[i];
temp2.y = temp.y+diry[i];
temp2.z = temp.z+dirz[i];
temp2.step = temp.step+1;
//printf("%d %d %d %d..", temp2.x, temp2.y, temp2.z, temp2.step);
if(limit(temp2)&&!vis[temp2.x][temp2.y][temp2.z]){
if(match(temp2)){
ans = ans<temp2.step? ans:temp2.step; //要bfs完所有的
}
else{
q.push(temp2);
vis[temp2.x][temp2.y][temp2.z] = 1;
}
}
}
// if(ans) break;
q.pop();
}
if(ans == INF) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", ans);
}
int main(){
int i, j, k;
while(scanf("%d%d%d", &l, &r, &c), l||r||c){
memset(map, 0, sizeof(map));
for(i = 0; i < l; i ++){
for(j = 0; j < r; j ++){
scanf("%s", map[i][j]);
for(k = 0; k <c; k ++){
if(map[i][j][k] == 'S'){
st.x = i; st.y = j; st.z = k; st.step = 0;
}
if(map[i][j][k] == 'E'){
en.x =i; en.y = j; en.z = k; en.step = 0;
}
}
}
gets(s);
}
bfs(); }
return 0;
}

NYOJ 353 3D dungeon 【bfs】的更多相关文章

  1. NYOJ353 3D dungeon 【BFS】

    3D dungeon 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 You are trapped in a 3D dungeon and need to find ...

  2. nyoj 353 3D dungeon

    3D dungeon 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 You are trapped in a 3D dungeon and need to find ...

  3. nyoj 523 亡命逃窜 【BFS】

    亡命逃窜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 从前有个叫hck的骑士,为了救我们漂亮的公主,潜入魔王的老巢,够英雄吧.只是英雄不是这么好当的.这个可怜的娃 ...

  4. NYOJ 284 坦克大战 【BFS】+【优先队列】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...

  5. NYOJ 58 步数最少 【BFS】

    意甲冠军:不解释. 策略:如果: 这个问题也可以用深宽搜索搜索中使用.我曾经写过,使用深层搜索.最近的学校范围内的搜索,拿这个问题来试试你的手. 代码: #include<stdio.h> ...

  6. nyoj 92 图片实用面积【bfs】

    图像实用区域 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 "ACKing"同学曾经做一个图像处理的项目时.遇到了一个问题,他须要摘取出图片中某 ...

  7. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  8. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  9. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

随机推荐

  1. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)

    靠这把上了蓝 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabyte ...

  2. 基于 K8S 构建数据中心操作系统

    在 12 月 22 日 ECUG 的下午场 ,七牛云容器计算部技术总监袁晓沛为大家带来了主题为<基于 K8S 的 DCOS 之路>的精彩分享,向大家介绍了七牛容器云目前 K8S 的状况和产 ...

  3. HDU-3592 World Exhibition

    差分约束. 很容易看出两种约束方式,然后建图.而且题目要求排序不能乱,于是加上第三种约束. 求最长就跑一遍最短路啊就行了. #include <cstdlib> #include < ...

  4. C++ 错误解决 —— internal compiler error

    问题: g++ 编译时,报错: g++: internal compiler error: Killed (program cc1plus) 出错原因: 出错的原因是(虚拟机)运行内存不足,而大量te ...

  5. poj 3468 线段树成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 54012   ...

  6. APUE 学习笔记(十) 高级I/O

    1. Unix IPC(InterProcess Communication) 同一主机的各个进程间的IPC:管道.FIFO.消息队列.信号量.共享存储器 不同主机上的各个进程间IPC:socket套 ...

  7. 26深入理解C指针之---不规则数组与指针

    一.不规则数组:每一行的列数不相等 1.复合字面量: 1).复合字面量是一种C构造 2).外形和数组声明差不多,写法与类型转换一样,(int[3]){10, 20, 30,} 3).将多个复合字面量可 ...

  8. static变量的生命周期

    static生命周期 2011-07-15 16:01 静态变量的类型说明符是static.静态变量当然是属于静态存储方式,但是属于静态存储方式的量不一定就是静态变量,例如外部变量虽属于静态存储方式, ...

  9. Scrapy学习-15-降低被识别为爬虫的方法

    3种常见的方法 1. 在settings中配置禁用cookies COOKIES_ENABLED = False 2. scrapy限速处理,scrapy为我们提供了扩展模块,它能动态的限制下载速度 ...

  10. django中表变更后migrate无效的问题

    问题描述: 已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrat ...