1280: 诡异的迷宫

时间限制: 2 秒  内存限制: 128 MB
提交: 174  解决: 27
提交 状态

题目描述

Simple最近刷题(打游戏)刷多了,一觉醒来发现自己到了一个迷宫里,怎么也出不去了。这时传来了一句话,告诉Simple必须按顺序收集完所有的宝石,才能出迷宫。所谓的顺序,就是按照每块宝石上英文字母的顺序。迷宫里面还有一些传送门,可以传送到任意一个另外的传送门的位置。(你走到一个不是空地上的地方的时候,就一定会触发相应事件,不可拒绝,从一个传送门传送到另一个传送门不用再次传送)。每走一步花费一个单位时间,传送门到另外一个传送门不需要时间。Simple初始就在字母为A的宝石的位置上(开局一宝石,其他全靠找)。

当Simple收集完所有宝石的时候就被传送出迷宫。

Simple还要赶回去刷题(打游戏),你们能告诉他最少需要多长时间才能回去吗?如果不可能出去就输出Impossible。

输入

多组实例,每组输入一个n,表示迷宫的大小为n*n  (n <= 10)
下面n行每行n个字符 
'.'表示空地,
'#'表示墙,不可以通过
'$'表示一个传送门
大写字母表示宝石

输出

每个实例输出一个数字,表示最少需要的时间,如果不能逃出迷宫,就输出Impossible。

样例输入

  1. 5
  2. A....
  3. ####.
  4. ..B..
  5. .####
  6. C.DE.
  7.  
  8. 2
  9. AC
  10. .B
  11.  
  12. 2
  13. A#
  14. #B
  15.  
  16. 3
  17. A$.
  18. ...
  19. .$B

样例输出

  1. 15
  2. 3
  3. Impossible
  4. 2

    /*
    这题就是一个简单的bfs,只是要注意的地方有很多。
    题目要求收集宝石只能按照英文字母的顺序,所以顺序不对的宝石当前是当作墙来看待的,是不能走的。
    收集过的宝石的地方,是可以重复走的,不然有的时候就不能收集完所有宝石,所以每收集一次宝石,就相当于重新跑一次bfs
    至于传送门,你踩到一个传送门,必须要传送到另一个传送门,此时到达另外一个传送门就不能触发效果了
    */

    (我debug一个下午。。。)
    附本人debug一下午的代码:
  1. 1 #include <cstdio>
  2. 2 #include <cmath>
  3. 3 #include <iostream>
  4. 4 #include <vector>
  5. 5 #include <set>
  6. 6 #include <sstream>
  7. 7 #include <algorithm>
  8. 8 #include <string>
  9. 9 #include <cstring>
  10. 10 using namespace std;
  11. 11 const int maxn = 15;
  12. 12 string st[maxn];
  13. 13 struct nod{
  14. 14 int x;
  15. 15 int y;
  16. 16 }tran[1050],nu[1050];
  17. 17 int vis[maxn][maxn];
  18. 18 int dir[5][2] = {{0,1},{1,0},{0,-1},{-1,0}};
  19. 19 int n;
  20. 20 int f = 0;
  21. 21 int sx,sy,len;
  22. 22 int cnt = 0,all = 0; //宝石
  23. 23 int head = 0,tail = 0;
  24. 24 void bfs(int sx,int sy) {
  25. 25 int xx,yy;
  26. 26 head = 0,tail = 1;
  27. 27 nu[head].x = sx;
  28. 28 nu[head].y = sy;
  29. 29 while(head != tail) {
  30. 30
  31. 31 int i;
  32. 32 for( i = 0; i < 4; i++) {
  33. 33 xx = nu[head].x + dir[i][0];
  34. 34 yy = nu[head].y + dir[i][1];
  35. 35 if(xx >= 0 && xx < n && yy >= 0 && yy < n && st[xx][yy] != '#' && !vis[xx][yy] ) {
  36. 36 if(st[xx][yy] == '.') {
  37. 37 nu[tail].x = xx;
  38. 38 nu[tail++].y = yy;
  39. 39 vis[xx][yy] = vis[nu[head].x][nu[head].y] + 1;
  40. 40
  41. 41 // cout<<vis[xx][yy]<<endl;
  42. 42 }
  43. 43 if(st[xx][yy] == '$') {
  44. 44
  45. 45 vis[xx][yy] = vis[nu[head].x][nu[head].y] + 1;
  46. 46 for(int j = 0; j < len; j++) {
  47. 47 if(!vis[tran[j].x][tran[j].y]) {
  48. 48
  49. 49
  50. 50 nu[tail].x = tran[j].x;
  51. 51 nu[tail++].y = tran[j].y;
  52. 52 vis[tran[j].x][tran[j].y] = vis[xx][yy];
  53. 53 // cout<< vis[tran[j].x][tran[j].y]<<endl;
  54. 54 continue;
  55. 55 }
  56. 56 }
  57. 57 }
  58. 58 else {
  59. 59 if(st[xx][yy] - 'A' == cnt + 1) {
  60. 60 // cout<<st[nu[head].x][nu[head].y]<<endl;
  61. 61 int u = vis[nu[head].x][nu[head].y] + 1;
  62. 62 nu[tail].x = xx;
  63. 63 nu[tail++].y = yy;
  64. 64 st[xx][yy] = '.';
  65. 65 head = tail - 1;
  66. 66 // cout<<xx<<yy<<"!!!"<<endl;
  67. 67 cnt++;
  68. 68 f = 1;
  69. 69
  70. 70
  71. 71 // cout<<"u "<<u<<endl;
  72. 72 memset(vis,0,sizeof(vis));
  73. 73 vis[nu[head].x][nu[head].y] = u;
  74. 74 // cout<<vis[nu[head].x][nu[head].y]<<"!!"<<endl;
  75. 75 // if(vis[nu[head].x][nu[head].y] == 5)
  76. 76
  77. 77 break;
  78. 78 // cout<<nu[head].x<<" "<<nu[head].y<<endl;
  79. 79 }
  80. 80
  81. 81 }
  82. 82 }
  83. 83 }
  84. 84 // cout<<i<<endl;
  85. 85 if(cnt == all) break;
  86. 86
  87. 87
  88. 88 if(f == 1) {
  89. 89 f = 0;
  90. 90 continue;
  91. 91 }
  92. 92 head++;
  93. 93 }
  94. 94 }
  95. 95 int main() {
  96. 96 ios::sync_with_stdio(false);
  97. 97
  98. 98 while(cin>>n) {
  99. 99 memset(vis,0,sizeof(vis));
  100. 100 cnt = 0,all = 0; //宝石
  101. 101 head = 0,tail = 0;
  102. 102 sx = 0,sy = 0,len = 0;
  103. 103 f = 0;
  104. 104 for(int i = 0; i < n; i++) {
  105. 105 cin>>st[i];
  106. 106 }
  107. 107
  108. 108
  109. 109 for(int i = 0; i < n; i++) {
  110. 110 for(int j = 0; j < n; j++) {
  111. 111
  112. 112 if(st[i][j] == 'A') {
  113. 113 st[i][j] = '.';
  114. 114 sx = i;
  115. 115 sy = j;
  116. 116 vis[i][j] = 1;
  117. 117 }
  118. 118 if(st[i][j] > 'A' && st[i][j] <= 'Z') all++;
  119. 119
  120. 120 if(st[i][j] == '$') {
  121. 121 tran[len].x = i;
  122. 122 tran[len++].y = j;
  123. 123 }
  124. 124 }
  125. 125
  126. 126 }
  127. 127 // cout<<all<<endl;
  128. 128 bfs(sx,sy);
  129. 129
  130. 130 if(cnt != all) cout<<"Impossible"<<endl;
  131. 131 else cout<<vis[nu[head].x][nu[head].y]-1<<endl;
  132. 132 }
  133. 133 return 0;
  134. 134 }

附标程:

  1. 1 #include<queue>
  2. 2 #include<cstdio>
  3. 3 #include<cstring>
  4. 4 #include<iostream>
  5. 5 #include<algorithm>
  6. 6
  7. 7 using namespace std;
  8. 8
  9. 9 struct door
  10. 10 {
  11. 11 int x,y;
  12. 12 }e[110];
  13. 13
  14. 14 int n,cnt,ce;
  15. 15 char a[15][15];
  16. 16 int b[15][15];
  17. 17 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
  18. 18
  19. 19
  20. 20 struct node
  21. 21 {
  22. 22 int x,y,s,cnt;
  23. 23 };
  24. 24
  25. 25 void bfs(int x,int y)
  26. 26 {
  27. 27 memset(b,0,sizeof(b));
  28. 28 a[x][y] = '.';
  29. 29 b[x][y] = 1;
  30. 30 queue<node> q;
  31. 31 node t,next;
  32. 32 t.x = x;
  33. 33 t.y = y;
  34. 34 t.s = 0;
  35. 35 t.cnt = 1;
  36. 36 q.push(t);
  37. 37 while(!q.empty())
  38. 38 {
  39. 39 t = q.front();
  40. 40 q.pop();
  41. 41 if(t.cnt == cnt)
  42. 42 {
  43. 43 printf("%d\n",t.s);
  44. 44 return;
  45. 45 }
  46. 46 for(int i=0;i<4;i++)
  47. 47 {
  48. 48 int tx = t.x + dir[i][0];
  49. 49 int ty = t.y + dir[i][1];
  50. 50 if(tx < 0 || ty < 0 || tx >= n || ty >= n)
  51. 51 continue;
  52. 52 if(a[tx][ty] == '#' || b[tx][ty])
  53. 53 continue;
  54. 54 if(a[tx][ty] >= 'A' && a[tx][ty] <= 'Z' && a[tx][ty] != 'A' + t.cnt)
  55. 55 continue;
  56. 56 if(a[tx][ty] >= 'A' && a[tx][ty] <= 'Z')
  57. 57 {
  58. 58 next.x = tx;
  59. 59 next.y = ty;
  60. 60 next.s = t.s + 1;
  61. 61 next.cnt = t.cnt + 1;
  62. 62 a[tx][ty] = '.';
  63. 63 memset(b,0,sizeof(b));
  64. 64 while(!q.empty())
  65. 65 q.pop();
  66. 66 b[tx][ty] = 1;
  67. 67 q.push(next);
  68. 68 break;
  69. 69 }
  70. 70 if(a[tx][ty] == '$')
  71. 71 {
  72. 72 for(int j=0;j<ce;j++)
  73. 73 {
  74. 74 if(e[j].x == tx && e[j].y == ty)
  75. 75 continue;
  76. 76 if(b[e[j].x][e[j].y])
  77. 77 continue;
  78. 78 next.x = e[j].x;
  79. 79 next.y = e[j].y;
  80. 80 next.s = t.s + 1;
  81. 81 next.cnt = t.cnt;
  82. 82 b[e[j].x][e[j].y] = 1;
  83. 83 q.push(next);
  84. 84 continue;
  85. 85 }
  86. 86 continue;
  87. 87 }
  88. 88 next.x = tx;
  89. 89 next.y = ty;
  90. 90 next.s = t.s + 1;
  91. 91 next.cnt = t.cnt;
  92. 92 b[tx][ty] = 1;
  93. 93 q.push(next);
  94. 94 }
  95. 95 }
  96. 96 printf("Impossible\n");
  97. 97 }
  98. 98 int main(void)
  99. 99 {
  100. 100 int T,i,j,x,y;
  101. 101 while(scanf("%d",&n)==1)
  102. 102 {
  103. 103 cnt = 0;
  104. 104 ce = 0;
  105. 105 for(i=0;i<n;i++)
  106. 106 {
  107. 107 scanf("%s",a[i]);
  108. 108 for(j=0;j<n;j++)
  109. 109 {
  110. 110 if(a[i][j] == 'A')
  111. 111 x = i, y = j;
  112. 112 if(a[i][j] >= 'A' && a[i][j] <= 'Z')
  113. 113 cnt++;
  114. 114 if(a[i][j] == '$')
  115. 115 {
  116. 116 e[ce].x = i;
  117. 117 e[ce++].y = j;
  118. 118 }
  119. 119 }
  120. 120 }
  121. 121 bfs(x,y);
  122. 122 }
  123. 123
  124. 124 return 0;
  125. 125 }

haut-1280 诡异的迷宫的更多相关文章

  1. HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...

  2. C语言动态走迷宫

    曾经用C语言做过的动态走迷宫程序,先分享代码如下: 代码如下: //头文件 #include<stdio.h> #include<windows.h>//Sleep(500)函 ...

  3. 你还可以再诡异点吗——SQL日志文件不断增长

    前言 今天算是遇到了一个罕见的案例. SQL日志文件不断增长的各种实例不用多说,园子里有很多牛人有过介绍,如果我再阐述这些陈谷子芝麻,想必已会被无数次吐槽. 但这次我碰到的问题确实比较诡异,其解决方式 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. BFS_Maze_求解迷宫最短路径

    /* 10 10 #.######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .## ...

  6. Delphi编程时候诡异地出现ORA-00937错误,记录解决它的思路和方法

    首先需要说明,这个问题的出现需要几个前提:使用微软的Oracle驱动(使用Oracle自己的驱动不会出现这个问题).使用绑定变量法,使用Format等方式拼接SQL也不会出现这个问题,还有一些诡异的规 ...

  7. 【刷题笔记】I'm stuck! (迷宫)-----java方案

    题目描述 : 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七个字符中的一个,分别表示如下意思: '#': 任何时候玩家都不能移动到此 ...

  8. 诡异的localhost无法连接

    上午试了localhost发现提示无法连接,ping了下localhost,能够ping通. 重启了Apache,还是无法解决. 试着停止了Apache服务,然后再连接localhost,发现浏览器提 ...

  9. canvas实例 ---- 制作简易迷宫(一)

    这个系列分为两部分,第一部分为迷宫的生成及操作,第二部分为自动寻路算法. 我们先看效果: See the Pen QGKBjm by fanyipin (@fanyipin) on CodePen. ...

随机推荐

  1. Python语言程序设计---函数的定义与使用

    推荐一个Python学习交流的q群:610380249 在学习Python的过程中,有什么不懂的问题都可以发群里,一起讨论. 1 函数的理解和定义 函数是一段代码的表示,所指定的参数是一种占位符,如果 ...

  2. canvas星空背景特效+CSS旋转相册学习

    今天在看帖子的时候,看到了个有趣的css旋转相册,刚好之前做了一个星空背景dome,这里给大家分享下代码: 旋转相册参考:https://blog.csdn.net/gitchatxiaomi/art ...

  3. Ubuntu安装记录

    好吧,这成功地让我想起了那些边肯红薯边黑苹果的早晨······ 本人纯属Windows用腻,后期请大佬多多指教 前面因为没U盘而碰壁的内容在此不说,接下来因为太兴奋,关于安装U盘制作没记录什么.最终, ...

  4. Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740

    Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...

  5. 《Effective C#》之减少装箱和拆箱

    <Effective C#>之减少装箱和拆箱_天极网 http://dev.yesky.com/msdn/359/3486359.shtml <Effective C#>之减少 ...

  6. 龙芯fedora28日常生存指南

    2021-01-30 v0.0.5 从0.0.1开始改了非常多,一月余时间的花费渴望为其他人提供一点帮助,能够快速上手. 这主要是这一年来我从3B1500到3A4000再到福珑2的日常使用记录,是之前 ...

  7. odoo-nginx 配置之80端口

    1 upstream odoo { 2 server 127.0.0.1:8069 weight=1 fail_timeout=0; 3 } 4 5 upstream odoo-im { 6 serv ...

  8. requestAnimationFrame小结

    背景 在Web应用中,实现动画效果的方法比较多,Javascript 中可以通过定时器 setTimeout或者setInterval 来实现,css3 可以使用 transition 和 anima ...

  9. 复制虚拟机,链接网络问题:没有找到合适的设备:没有找到可用于链接System eth0 的

    http://my.oschina.net/coolfire368/blog/292742 1./etc/udev/rules.d/70-persistent-net.rules 修改也成,修改时留下 ...

  10. OpenStack (neutron 网络服务)

    neutron介绍 提供 OpenStack 虚拟网络服务,也是 OpenStack 重要的核心模块之一,该模块最开始是 Nova 的一部分,叫 nova-network,后来从 Nova 中分离出来 ...