haut-1280 诡异的迷宫
1280: 诡异的迷宫
时间限制: 2 秒 内存限制: 128 MB
提交: 174 解决: 27
提交 状态
题目描述
Simple最近刷题(打游戏)刷多了,一觉醒来发现自己到了一个迷宫里,怎么也出不去了。这时传来了一句话,告诉Simple必须按顺序收集完所有的宝石,才能出迷宫。所谓的顺序,就是按照每块宝石上英文字母的顺序。迷宫里面还有一些传送门,可以传送到任意一个另外的传送门的位置。(你走到一个不是空地上的地方的时候,就一定会触发相应事件,不可拒绝,从一个传送门传送到另一个传送门不用再次传送)。每走一步花费一个单位时间,传送门到另外一个传送门不需要时间。Simple初始就在字母为A的宝石的位置上(开局一宝石,其他全靠找)。
当Simple收集完所有宝石的时候就被传送出迷宫。
Simple还要赶回去刷题(打游戏),你们能告诉他最少需要多长时间才能回去吗?如果不可能出去就输出Impossible。
输入
多组实例,每组输入一个n,表示迷宫的大小为n*n (n <= 10)
下面n行每行n个字符
'.'表示空地,
'#'表示墙,不可以通过
'$'表示一个传送门
大写字母表示宝石
输出
样例输入
5
A....
####.
..B..
.####
C.DE. 2
AC
.B 2
A#
#B 3
A$.
...
.$B
样例输出
15
3
Impossible
2
/*
这题就是一个简单的bfs,只是要注意的地方有很多。
题目要求收集宝石只能按照英文字母的顺序,所以顺序不对的宝石当前是当作墙来看待的,是不能走的。
收集过的宝石的地方,是可以重复走的,不然有的时候就不能收集完所有宝石,所以每收集一次宝石,就相当于重新跑一次bfs
至于传送门,你踩到一个传送门,必须要传送到另一个传送门,此时到达另外一个传送门就不能触发效果了
*/
(我debug一个下午。。。)
附本人debug一下午的代码:
1 #include <cstdio>
2 #include <cmath>
3 #include <iostream>
4 #include <vector>
5 #include <set>
6 #include <sstream>
7 #include <algorithm>
8 #include <string>
9 #include <cstring>
10 using namespace std;
11 const int maxn = 15;
12 string st[maxn];
13 struct nod{
14 int x;
15 int y;
16 }tran[1050],nu[1050];
17 int vis[maxn][maxn];
18 int dir[5][2] = {{0,1},{1,0},{0,-1},{-1,0}};
19 int n;
20 int f = 0;
21 int sx,sy,len;
22 int cnt = 0,all = 0; //宝石
23 int head = 0,tail = 0;
24 void bfs(int sx,int sy) {
25 int xx,yy;
26 head = 0,tail = 1;
27 nu[head].x = sx;
28 nu[head].y = sy;
29 while(head != tail) {
30
31 int i;
32 for( i = 0; i < 4; i++) {
33 xx = nu[head].x + dir[i][0];
34 yy = nu[head].y + dir[i][1];
35 if(xx >= 0 && xx < n && yy >= 0 && yy < n && st[xx][yy] != '#' && !vis[xx][yy] ) {
36 if(st[xx][yy] == '.') {
37 nu[tail].x = xx;
38 nu[tail++].y = yy;
39 vis[xx][yy] = vis[nu[head].x][nu[head].y] + 1;
40
41 // cout<<vis[xx][yy]<<endl;
42 }
43 if(st[xx][yy] == '$') {
44
45 vis[xx][yy] = vis[nu[head].x][nu[head].y] + 1;
46 for(int j = 0; j < len; j++) {
47 if(!vis[tran[j].x][tran[j].y]) {
48
49
50 nu[tail].x = tran[j].x;
51 nu[tail++].y = tran[j].y;
52 vis[tran[j].x][tran[j].y] = vis[xx][yy];
53 // cout<< vis[tran[j].x][tran[j].y]<<endl;
54 continue;
55 }
56 }
57 }
58 else {
59 if(st[xx][yy] - 'A' == cnt + 1) {
60 // cout<<st[nu[head].x][nu[head].y]<<endl;
61 int u = vis[nu[head].x][nu[head].y] + 1;
62 nu[tail].x = xx;
63 nu[tail++].y = yy;
64 st[xx][yy] = '.';
65 head = tail - 1;
66 // cout<<xx<<yy<<"!!!"<<endl;
67 cnt++;
68 f = 1;
69
70
71 // cout<<"u "<<u<<endl;
72 memset(vis,0,sizeof(vis));
73 vis[nu[head].x][nu[head].y] = u;
74 // cout<<vis[nu[head].x][nu[head].y]<<"!!"<<endl;
75 // if(vis[nu[head].x][nu[head].y] == 5)
76
77 break;
78 // cout<<nu[head].x<<" "<<nu[head].y<<endl;
79 }
80
81 }
82 }
83 }
84 // cout<<i<<endl;
85 if(cnt == all) break;
86
87
88 if(f == 1) {
89 f = 0;
90 continue;
91 }
92 head++;
93 }
94 }
95 int main() {
96 ios::sync_with_stdio(false);
97
98 while(cin>>n) {
99 memset(vis,0,sizeof(vis));
100 cnt = 0,all = 0; //宝石
101 head = 0,tail = 0;
102 sx = 0,sy = 0,len = 0;
103 f = 0;
104 for(int i = 0; i < n; i++) {
105 cin>>st[i];
106 }
107
108
109 for(int i = 0; i < n; i++) {
110 for(int j = 0; j < n; j++) {
111
112 if(st[i][j] == 'A') {
113 st[i][j] = '.';
114 sx = i;
115 sy = j;
116 vis[i][j] = 1;
117 }
118 if(st[i][j] > 'A' && st[i][j] <= 'Z') all++;
119
120 if(st[i][j] == '$') {
121 tran[len].x = i;
122 tran[len++].y = j;
123 }
124 }
125
126 }
127 // cout<<all<<endl;
128 bfs(sx,sy);
129
130 if(cnt != all) cout<<"Impossible"<<endl;
131 else cout<<vis[nu[head].x][nu[head].y]-1<<endl;
132 }
133 return 0;
134 }
附标程:
1 #include<queue>
2 #include<cstdio>
3 #include<cstring>
4 #include<iostream>
5 #include<algorithm>
6
7 using namespace std;
8
9 struct door
10 {
11 int x,y;
12 }e[110];
13
14 int n,cnt,ce;
15 char a[15][15];
16 int b[15][15];
17 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
18
19
20 struct node
21 {
22 int x,y,s,cnt;
23 };
24
25 void bfs(int x,int y)
26 {
27 memset(b,0,sizeof(b));
28 a[x][y] = '.';
29 b[x][y] = 1;
30 queue<node> q;
31 node t,next;
32 t.x = x;
33 t.y = y;
34 t.s = 0;
35 t.cnt = 1;
36 q.push(t);
37 while(!q.empty())
38 {
39 t = q.front();
40 q.pop();
41 if(t.cnt == cnt)
42 {
43 printf("%d\n",t.s);
44 return;
45 }
46 for(int i=0;i<4;i++)
47 {
48 int tx = t.x + dir[i][0];
49 int ty = t.y + dir[i][1];
50 if(tx < 0 || ty < 0 || tx >= n || ty >= n)
51 continue;
52 if(a[tx][ty] == '#' || b[tx][ty])
53 continue;
54 if(a[tx][ty] >= 'A' && a[tx][ty] <= 'Z' && a[tx][ty] != 'A' + t.cnt)
55 continue;
56 if(a[tx][ty] >= 'A' && a[tx][ty] <= 'Z')
57 {
58 next.x = tx;
59 next.y = ty;
60 next.s = t.s + 1;
61 next.cnt = t.cnt + 1;
62 a[tx][ty] = '.';
63 memset(b,0,sizeof(b));
64 while(!q.empty())
65 q.pop();
66 b[tx][ty] = 1;
67 q.push(next);
68 break;
69 }
70 if(a[tx][ty] == '$')
71 {
72 for(int j=0;j<ce;j++)
73 {
74 if(e[j].x == tx && e[j].y == ty)
75 continue;
76 if(b[e[j].x][e[j].y])
77 continue;
78 next.x = e[j].x;
79 next.y = e[j].y;
80 next.s = t.s + 1;
81 next.cnt = t.cnt;
82 b[e[j].x][e[j].y] = 1;
83 q.push(next);
84 continue;
85 }
86 continue;
87 }
88 next.x = tx;
89 next.y = ty;
90 next.s = t.s + 1;
91 next.cnt = t.cnt;
92 b[tx][ty] = 1;
93 q.push(next);
94 }
95 }
96 printf("Impossible\n");
97 }
98 int main(void)
99 {
100 int T,i,j,x,y;
101 while(scanf("%d",&n)==1)
102 {
103 cnt = 0;
104 ce = 0;
105 for(i=0;i<n;i++)
106 {
107 scanf("%s",a[i]);
108 for(j=0;j<n;j++)
109 {
110 if(a[i][j] == 'A')
111 x = i, y = j;
112 if(a[i][j] >= 'A' && a[i][j] <= 'Z')
113 cnt++;
114 if(a[i][j] == '$')
115 {
116 e[ce].x = i;
117 e[ce++].y = j;
118 }
119 }
120 }
121 bfs(x,y);
122 }
123
124 return 0;
125 }
haut-1280 诡异的迷宫的更多相关文章
- HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...
- C语言动态走迷宫
曾经用C语言做过的动态走迷宫程序,先分享代码如下: 代码如下: //头文件 #include<stdio.h> #include<windows.h>//Sleep(500)函 ...
- 你还可以再诡异点吗——SQL日志文件不断增长
前言 今天算是遇到了一个罕见的案例. SQL日志文件不断增长的各种实例不用多说,园子里有很多牛人有过介绍,如果我再阐述这些陈谷子芝麻,想必已会被无数次吐槽. 但这次我碰到的问题确实比较诡异,其解决方式 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- BFS_Maze_求解迷宫最短路径
/* 10 10 #.######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .## ...
- Delphi编程时候诡异地出现ORA-00937错误,记录解决它的思路和方法
首先需要说明,这个问题的出现需要几个前提:使用微软的Oracle驱动(使用Oracle自己的驱动不会出现这个问题).使用绑定变量法,使用Format等方式拼接SQL也不会出现这个问题,还有一些诡异的规 ...
- 【刷题笔记】I'm stuck! (迷宫)-----java方案
题目描述 : 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七个字符中的一个,分别表示如下意思: '#': 任何时候玩家都不能移动到此 ...
- 诡异的localhost无法连接
上午试了localhost发现提示无法连接,ping了下localhost,能够ping通. 重启了Apache,还是无法解决. 试着停止了Apache服务,然后再连接localhost,发现浏览器提 ...
- canvas实例 ---- 制作简易迷宫(一)
这个系列分为两部分,第一部分为迷宫的生成及操作,第二部分为自动寻路算法. 我们先看效果: See the Pen QGKBjm by fanyipin (@fanyipin) on CodePen. ...
随机推荐
- 面试官问我CAS,我一点都不慌
文章以纯面试的角度去讲解,所以有很多的细节是未铺垫的. 文章中写到的处理线程安全的思路每一项技术都可以写出一篇文章,AQS.Synchronized.Atomic...周末肝起来!下周再来给大家安排! ...
- 渗透测试中期--漏洞复现--MS08_067
靶机:Win2k3 10.10.10.130 攻击机:BT5 10.10.10.128 一:nmap 查看WinK3是否开放端口3389 开放3389方法:我的电脑->属性-&g ...
- CMOS 摄像头的Skipping 和 Binning 模式
在通常的摄像头中,不同的resolution对应不同的帧率.想要提高帧率就要考虑是否需要缩小视野(FOV).若不希望视野缩小,就需要减少resolution. 常用的减少resolution的两种方式 ...
- 代码托管从业者 Git 指南
本文作者:李博文 - CODING 后端开发工程师 前言 六七年前,我机缘巧合进入了代码托管行业,做过基于 Git 支持 SVN 客户端接入.Git 代码托管平台分布式.Git 代码托管读写分离.Gi ...
- 华为路由配置IPSec
用该方法配置后用抓包工具抓取的就看不到两个通讯点的IP,而显示的是加密点的IP. 原文:https://www.cnblogs.com/yangyang1988/p/11559819.html
- Mybatis参数预编译
Mybatis参数预编译 一.数据库预编译介绍 1.数据库SQL语句编译特性: 数据库接受到sql语句之后,需要词法和语义解析,优化sql语句,制定执行计划.这需要花费一些时间.但是很多情况,我们的一 ...
- Hash Join: Basic Steps
Joins https://docs.oracle.com/database/121/TGSQL/tgsql_join.htm#TGSQL242 tidb/index_lookup_hash_join ...
- 2.kafka架构深入——生产者
一个topic有多个partition,每个partition又有多个副本,在这些副本中又有一个leader和多个follower. 1)分区的原因 (1)方便在集群中扩展,每个Partition可以 ...
- 从零开始学Java (五)条件选择
if switch while do while for break continue 这块对于有语言基础的人来说可以跳过了. 注意有个equals方法. 1 public class Main { ...
- 引入 Gateway 网关,这些坑一定要学会避开!!!
Spring cloud gateway是替代zuul的网关产品,基于Spring 5.Spring boot 2.0以上.Reactor, 提供任意的路由匹配和断言.过滤功能.上一篇文章谈了一下Ga ...