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

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

策略:简单的深搜。

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

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. #define INF 0x3f3f3f3f
  7. char map[35][35][35];
  8. int ans, l, r, c;
  9. const int dirx[6] = {1, -1, 0, 0, 0, 0};
  10. const int diry[6] = {0, 0, 1, -1, 0, 0};
  11. const int dirz[6] = {0, 0, 0, 0, 1, -1};
  12. struct node{
  13. int x, y, z;
  14. int step;
  15. };
  16. node st, en;
  17. queue<node > q;
  18. char s[10000];
  19. bool vis[35][35][35];
  20.  
  21. int limit(node s){
  22. 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] != '#');
  23. }
  24.  
  25. int match(node s){
  26. if(s.x==en.x&&s.y==en.y&&s.z==en.z) return 1;
  27. else return 0;
  28. }
  29. void bfs(){
  30. memset(vis, 0, sizeof(vis));
  31. ans = INF; //初始化最大
  32. int i;
  33. q.push(st);
  34. //map[st.x][st.y][st.z] = '#';
  35. vis[st.x][st.y][st.z] = 1;
  36. while(!q.empty()){
  37. node temp = q.front();
  38.  
  39. for(i = 0; i < 6; i ++){
  40. node temp2;
  41. temp2.x = temp.x+dirx[i];
  42. temp2.y = temp.y+diry[i];
  43. temp2.z = temp.z+dirz[i];
  44. temp2.step = temp.step+1;
  45. //printf("%d %d %d %d..", temp2.x, temp2.y, temp2.z, temp2.step);
  46. if(limit(temp2)&&!vis[temp2.x][temp2.y][temp2.z]){
  47. if(match(temp2)){
  48. ans = ans<temp2.step?
  49.  
  50. ans:temp2.step; //要bfs完所有的
  51. }
  52. else{
  53. q.push(temp2);
  54. vis[temp2.x][temp2.y][temp2.z] = 1;
  55. }
  56. }
  57. }
  58. // if(ans) break;
  59. q.pop();
  60. }
  61. if(ans == INF) printf("Trapped!\n");
  62. else printf("Escaped in %d minute(s).\n", ans);
  63. }
  64. int main(){
  65. int i, j, k;
  66. while(scanf("%d%d%d", &l, &r, &c), l||r||c){
  67. memset(map, 0, sizeof(map));
  68. for(i = 0; i < l; i ++){
  69. for(j = 0; j < r; j ++){
  70. scanf("%s", map[i][j]);
  71. for(k = 0; k <c; k ++){
  72. if(map[i][j][k] == 'S'){
  73. st.x = i; st.y = j; st.z = k; st.step = 0;
  74. }
  75. if(map[i][j][k] == 'E'){
  76. en.x =i; en.y = j; en.z = k; en.step = 0;
  77. }
  78. }
  79. }
  80. gets(s);
  81. }
  82. bfs();
  83.  
  84. }
  85. return 0;
  86. }

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. jmeter非常好的博客收藏

    http://blog.sina.com.cn/s/blog_56c9b55c010148os.html

  2. 常见python快捷键

    http://www.cnblogs.com/toutou/p/4778818.html Ctrl+/注释(取消注释)选择的行 Shift + Enter开始新行 Ctrl + Enter智能换行 T ...

  3. python学习--同目录下调用 (*.py)及不同目录下调(*.py)

    注:__init__.py 内容为空 1. 同目录下调用 (Contract_Statelog.py)  如图: temp1.py 调用 Contract_Statelog.py中的方法 2. 不同目 ...

  4. TOJ3031: Multiple bfs

    3031: Multiple Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte Total Submit: 60   ...

  5. Winform 连接Oracle10g时出错的解决方法

    环境:Win7(64位). VS2012 update3.Oracle10 (本机已安装ODTwithODAC1120320_32bit) 最近在开发一程序时莫名其妙报一个错误(未能加载文件或程序集“ ...

  6. onclick跳转到其他页面的几种方式

    如果是本页显示可以直接用location,方法如下: ①onclick="javascript:window.location.href='URL'" ②onclick=" ...

  7. [USACO07NOV]牛继电器Cow Relays (最短路,DP)

    题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...

  8. Python Base One

    //this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...

  9. 【spring aop切面】基础使用教程

    package tpf.aspect; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFact ...

  10. Python基础教程总结(二)

    上周总结了一下Python的一些基本数据类型和用法.这次总结一下4-9章的内容,完后,赶紧学以致用吧. 5. 第四章——字典:当索引不好用时 字典是Python中唯一内建的映射类型.字典中的值并没有特 ...