题意:给你一个高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. LiveScript 流程控制、循环以及列表推导式

    The LiveScript Book     The LiveScript Book Generators and Yield 你可以在你的 LiveScript 代码中使用 Ecmascript ...

  2. HashSet源码分析 jdk1.6

    Set的特点:Set元素无顺序,且元素不可以重复. 1.定义 public class HashSet<E> extends AbstractSet<E> implements ...

  3. IP地址资源的分配和管理

    IP地址资源的分配和管理 参考资料 https://wenku.baidu.com/view/3bdf94172cc58bd63086bd8c.html   http://www.iana.org/ ...

  4. ACM程序设计选修课——1065: Operations on Grids(暴力字符串)

    1065: Operations on Grids Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 17  Solved: 4 [Submit][Sta ...

  5. Codeforces Round #345 (Div. 2)——B. Beautiful Paintings(贪心求上升序列个数)

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. co模块总结

    1.thunk函数 javascript中的thunk函数就是一个单参数函数,且该参数必须是一个callback函数,callback的签名必须为callback(err,args...); 所谓的t ...

  7. docker (centOS 7) 使用笔记1

    1. docker配置 初次在安装完docker后,初始化配置 copy默认的docker.service后,重启服务,会在/etc/systemd/system/multi-user.target. ...

  8. 网络流(自行理解的Dinic)

    自行理解的Dinic 注释即讲解 #include<bits/stdc++.h> ; using namespace std; int read() { ,w=; ;ch=getchar( ...

  9. 百度网络监控实战:NetRadar横空出世(上)

    原文:https://mp.weixin.qq.com/s/VBShicsqReDtureKAdEgDA 转自订阅号「AIOps智能运维」,已授权运维帮转发 作者简介:运小贝,百度高级研发工程师 负责 ...

  10. jquery 选择器加变量

    var $role_id = btn.parent().prev().prev().attr('id') var $department_id = btn.parent().prev().prev() ...