haut-1280 诡异的迷宫

1280: 诡异的迷宫

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

题目描述

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

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

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

输入

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

输出

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

样例输入

5
A....
####.
..B..
.####
C.DE.2
AC
.B2
A#
#B3
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 }
View Code

   附标程:

  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 }
View Code

 

转载于:https://www.cnblogs.com/zmin/p/7599489.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399693.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

datatables 获取筛选后的数据

https://blog.csdn.net/weixin_34417200/article/details/88204395 背景&#xff1a; 使用 jquery 的表格插件 datatables 获取它筛选后的数据&#xff0c;一开始&#xff0c;我只能获取到全部数据&#xff0c;因为插件默认把不符合条件的隐藏了&#xff0c;但是不代表数据改…

1.2-Nginx编译安装

安装nginxcd /usr/local/src/wget http://nginx.org/download/nginx-1.6.2.tar.gztar zxvf nginx-1.6.2.tar.gz cd nginx-1.6.2./configure --prefix/usr/local/nginx --with-pcre make make install启动nginx&#xff1a; /usr/local/nginx/sbin/nginx注意&#xff1a;ng…

一个经典实例理解继承与多态原理与优点(附源码)---面向对象继承和多态性理解得不够深刻的同学请进...

一 引子 都说面向对象的4大支柱是抽象&#xff0c;封装&#xff0c;继承与多态。但是一些初涉编程的开发人员&#xff0c;体会不到继承与多态的妙用&#xff0c;本文就试以一个经典实例来诠释继承与多态的用武之地。本实例的需求来自《重构》一书。 二 需求 1. 任务说明 我们的…

推荐几款好用的云笔记软件

一直钟爱印象笔记&#xff0c;程序员的电脑上必装的软件&#xff0c;但最近期限到了&#xff0c;再也不能像以前无限制的上传文件&#xff0c;续费也比去年的只要九块九一年高出了很多倍&#xff0c;因此&#xff0c;注册试用了其他的笔记&#xff0c;发现云笔记众多&#xff0…

DataTables怎么给某一列加上click事件

$(#example tbody).on(click,tr td:nth-child(3), function (e) {var name $(this).text();alert(name); } ); https://ask.csdn.net/questions/178026

oschina下载工具

http://www.oschina.net/project/tag/97/download-tools

读《启示录》有感-----1

书本封面很有特色&#xff0c;已经标注这本书内容的真谛&#xff1a;表明了这本书要做到的东西&#xff0c;看这本书能得到的东西 怎样打造用户喜爱的产品? 好产品基本条件&#xff1a;价值、可用性、可行性&#xff0c;三者缺一不可。 做一个合格的产品经理需要哪些能力&…

Sublime Text 3 初试牛刀

每次我在其他视频网站上看学习视频的时候&#xff0c;看着老师用的编辑器高大上档次&#xff0c;而我一般用Notepad&#xff0c;和Dreamweaver去编辑网页&#xff0c;需要每一行代码&#xff0c;打进去&#xff0c;效率低。最近看到sublime编辑器&#xff0c;在网上搜了一下说是…

js 去除左右空格

//去左右空格; function trim(s){return s.replace(/(^\s*)|(\s*$)/g, ""); }

在没有域环境的情况下配置完整安装的SharePoint2010和2013

完整安装SharePoint2010。完成后先不要运行配置向导。配置数据库。SharePoint会安装一个POWERSHELL在这里14\CONFIG\POWERSHELL\Registration。运行该目录下的psconsole会打开一个命令行窗口。执行New-SPConfigurationDatabase【回车】DatabaseName <config database name&g…

mysql: union / union all / 自定义函数用法详解

mysql&#xff1a; union / union all http://www.cnblogs.com/wangyayun/p/6133540.html mysql&#xff1a;自定义函数用法详解 http://www.cnblogs.com/caoruiy/p/4485273.html 转载于:https://www.cnblogs.com/bj20170624/p/7605426.html

[C++学习历程]基础部分 C++中的函数学习

本文地址&#xff1a;http://blog.csdn.net/sushengmiyan/article/details/20305815 作者&#xff1a;sushengmiyan 一。静态变量&#xff1a; 局部变量是线程到达定义的地方的时候进行初始化&#xff0c;如果定义在函数中&#xff0c;那么每次函数调用的时候&#xff0c;都会进…

js 表单自动提交

var form document.getElementById(formid);form.submit();

linux下安装Mysql(干货!!!)解决mysql 1130问题,远程登录问题

转载自&#xff1a;http://www.cnblogs.com/xxoome/p/5864912.html linux版本&#xff1a;CentOS7 64位 1、下载安装包“mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz” # 安装依赖 yum -y install perl perl-devel autoconf libaio 2、把下载的安装包移动到/usr/local/下。…

linux命令:ftp

1. 登录&#xff1a; ftp IP_ADDR &#xff1b; 根据提示输入USER_NAME PASS_WORD 或&#xff1a; ftp -i -n IP_ADDR user USER_NAME PASS_WORD ftp -i -n 172.17.17.17 user PUB 123456 2. 下载文件 下载文件通常用get和mget这两条命令。 a) get 格式&a…

flex 有关数据类型强制转

flex 编辑页面里变量的强制类型转化时&#xff0c;竟然不能用 as 比如&#xff1a;在mxml里&#xff0c; private var aa:String"89"; private var bb:Number(Number)aa; 正确 private var aa:String"89"; private var bb:intaa as int ; 不正确 估计这两种…

Openstack Neutron : 安全

目录 - iptable&#xff1a;起源 - tables - chains - rules - 方向 - Security group 安全组&#xff1a; - Firewall 防火墙&#xff1a; - 更高的安全 - 无处安放的安全 - 公共安全 当业务从传统环境迁移到云上之后&a…

SQL语句汇总(三)——聚合函数、分组、子查询及组合查询

https://www.cnblogs.com/ghost-xyx/p/3811036.html SQL语句汇总&#xff08;三&#xff09;——聚合函数、分组、子查询及组合查询 拖了一个星期&#xff0c;终于开始写第三篇了。走起&#xff01; 聚合函数&#xff1a; SQL中提供的聚合函数可以用来统计、求和、求最值等等…

iOS应用国际化教程(2014版)

本文转载至 http://www.cocoachina.com/industry/20140526/8554.html 这篇教程将通过一款名为iLikeIt的应用带你了解最基础的国际化概念&#xff0c;并为你的应用添加国际化的支持。该示例应用有一个标签和一个You Like&#xff1f;按钮&#xff0c;用户无论何时点击You Like?…

公众平台商户接入(微信支付)功能申请教程

场景及类型介绍 商家可以申请公众账号支付和APP&#xff08;应用客户端&#xff09;支付两种接入微信支付方式。 公众账号支付&#xff1a;用户在微信公众帐号内使用微信支付消费&#xff0c;案例&#xff1a;易迅、QQ充值。 APP&#xff08;应用客户端&#xff09;支付&#x…