NYOJ 353 3D dungeon 【bfs】
题意:给你一个高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】的更多相关文章
- NYOJ353 3D dungeon 【BFS】
3D dungeon 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 You are trapped in a 3D dungeon and need to find ...
- nyoj 353 3D dungeon
3D dungeon 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 You are trapped in a 3D dungeon and need to find ...
- nyoj 523 亡命逃窜 【BFS】
亡命逃窜 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 从前有个叫hck的骑士,为了救我们漂亮的公主,潜入魔王的老巢,够英雄吧.只是英雄不是这么好当的.这个可怜的娃 ...
- NYOJ 284 坦克大战 【BFS】+【优先队列】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...
- NYOJ 58 步数最少 【BFS】
意甲冠军:不解释. 策略:如果: 这个问题也可以用深宽搜索搜索中使用.我曾经写过,使用深层搜索.最近的学校范围内的搜索,拿这个问题来试试你的手. 代码: #include<stdio.h> ...
- nyoj 92 图片实用面积【bfs】
图像实用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 "ACKing"同学曾经做一个图像处理的项目时.遇到了一个问题,他须要摘取出图片中某 ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
随机推荐
- jmeter非常好的博客收藏
http://blog.sina.com.cn/s/blog_56c9b55c010148os.html
- 常见python快捷键
http://www.cnblogs.com/toutou/p/4778818.html Ctrl+/注释(取消注释)选择的行 Shift + Enter开始新行 Ctrl + Enter智能换行 T ...
- python学习--同目录下调用 (*.py)及不同目录下调(*.py)
注:__init__.py 内容为空 1. 同目录下调用 (Contract_Statelog.py) 如图: temp1.py 调用 Contract_Statelog.py中的方法 2. 不同目 ...
- TOJ3031: Multiple bfs
3031: Multiple Time Limit(Common/Java):2000MS/6000MS Memory Limit:65536KByte Total Submit: 60 ...
- Winform 连接Oracle10g时出错的解决方法
环境:Win7(64位). VS2012 update3.Oracle10 (本机已安装ODTwithODAC1120320_32bit) 最近在开发一程序时莫名其妙报一个错误(未能加载文件或程序集“ ...
- onclick跳转到其他页面的几种方式
如果是本页显示可以直接用location,方法如下: ①onclick="javascript:window.location.href='URL'" ②onclick=" ...
- [USACO07NOV]牛继电器Cow Relays (最短路,DP)
题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...
- Python Base One
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...
- 【spring aop切面】基础使用教程
package tpf.aspect; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFact ...
- Python基础教程总结(二)
上周总结了一下Python的一些基本数据类型和用法.这次总结一下4-9章的内容,完后,赶紧学以致用吧. 5. 第四章——字典:当索引不好用时 字典是Python中唯一内建的映射类型.字典中的值并没有特 ...