2017年浙江工业大学大学生程序设计迎新赛热身赛 I after与迷宫 牛客
2017-12-10 22:45
423 查看
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
after的算法书的遗落在一个叫做AIJ的迷宫中了,这个迷宫有N*M个房间,迷宫的入口为(1,1),算法书遗落在(r,c)。迷宫中的房间有四种状态:空房间、无法进入的房间、有墨菲斯托存在的房间和有莉莉丝存在的房间。墨菲斯托会否定一切,而莉莉丝会诱惑人做一种叫做YK的活动。after是一个意志薄弱的人,他遇到了墨菲斯托和莉莉丝之后,便会变成眼神空洞的超级YK机器人。after每步可以从他当前的房间走至上下左右四个房间的其中一个房间。after害怕变成超级YK机器人,所以要尽快拿到算法书然后从入口逃离。问after最少需要走多少步才可以在不变成超级YK机器人的情况下从入口出发取回算法书并逃离迷宫?
示例1
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
after的算法书的遗落在一个叫做AIJ的迷宫中了,这个迷宫有N*M个房间,迷宫的入口为(1,1),算法书遗落在(r,c)。迷宫中的房间有四种状态:空房间、无法进入的房间、有墨菲斯托存在的房间和有莉莉丝存在的房间。墨菲斯托会否定一切,而莉莉丝会诱惑人做一种叫做YK的活动。after是一个意志薄弱的人,他遇到了墨菲斯托和莉莉丝之后,便会变成眼神空洞的超级YK机器人。after每步可以从他当前的房间走至上下左右四个房间的其中一个房间。after害怕变成超级YK机器人,所以要尽快拿到算法书然后从入口逃离。问after最少需要走多少步才可以在不变成超级YK机器人的情况下从入口出发取回算法书并逃离迷宫?输入描述:
第一行一个正整数T(T<=10),表示共有T组数据。 对于每组数据,第一行四个正整数N,M,r,c(1<=N,M<=1000;1<=r<=N;1<=c<=M)。 接下来N行,每行M个字符,每个表示房间的状态,“.”表示空房间,“*”表示无法进入的房间,“F”表示有墨菲斯托存在的房间,“M”表示有莉莉丝存在的房间。 数据保证(1,1)为“.”。
输出描述:
对每组数据输出一行,即after最少需要走的步数。若after无法取回算法书,则输出“IMPOSSIBLE”(不带引号)。
示例1
输入
1 4 4 4 3 ..** *F.. * be8f .*. *M.F
输出
14
#include<cstdio> #include<cstring> #include<queue> using namespace std; #include<iostream> struct node { int x,y,step,mark; }; const int maxn=1e3+7; int n,m; bool vis[maxn][maxn][12];//加一个mark vis 其实可以2进制压缩节省空间 char str[maxn][maxn]; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; char mp[]={'.','F','M'}; void bfs(int r,int c) { node now,next; queue<node>Q; memset(vis,false,sizeof(vis)); now.x=0; now.y=0; now.step=0; now.mark=0; vis[now.x][now.y][now.mark]=true; Q.push(now); while(!Q.empty()) { now=Q.front(); Q.pop(); if(now.x==r&&now.y==c){printf("%d\n",2*now.step);return ;} for(int i=0;i<4;i++) { int x1=now.x+dx[i]; int y1=now.y+dy[i]; if(x1>=0&&x1<n&&y1>=0&&y1<m&&str[x1][y1]!='*'&&!vis[x1][y1][now.mark]) { if(str[x1][y1]=='.') { next.x=x1; next.y=y1; next.step=now.step+1; next.mark=now.mark; vis[x1][y1][next.mark]=true; Q.push(next); } else { int z; if(now.mark%10==1&&str[x1][y1]=='M') continue;//之前到过F,不能到M if(now.mark/10==1&&str[x1][y1]=='F') continue;//之前到过M,不能到F for(int i=0;i<3;i++) if(mp[i]==str[x1][y1]) z=i; if(mp[z]==str[x1][y1]) { next.x=x1; next.y=y1; next.step=now.step+1; //next.mark=now.mark; if(str[x1][y1]=='M') next.mark=10;//做标记 else next.mark=1; if(vis[x1][y1][next.mark]) continue;//之前此状态到达过 vis[x1][y1][next.mark]=true; Q.push(next); } } } } } printf("IMPOSSIBLE\n"); } int main () { int t; scanf("%d",&t); while(t--) { int r,c; scanf("%d %d %d %d",&n,&m,&r,&c); getchar(); for(int i=0;i<n;i++) gets(str[i]); bfs(r-1,c-1); } return 0; }
相关文章推荐
- 【牛客网】2017年浙江工业大学大学生程序设计迎新赛热身赛 F题 方块 I【证明题】【想法题】
- 2017年浙江工业大学大学生程序设计迎新赛热身赛 部分题解
- 2017年浙江工业大学大学生程序设计迎新赛热身赛
- 2017年浙江工业大学大学生程序设计迎新赛热身赛-E:递推(矩阵快速幂)
- 2017年浙江工业大学大学生程序设计迎新赛热身赛-J-Forever97与寄信
- 2017年浙江工业大学大学生程序设计迎新赛预赛 J 裁缝大师【简单几何】
- 2017年浙江工业大学大学生程序设计迎新赛预赛
- 2017年浙江工业大学大学生程序设计迎新赛预赛E 最大化平均值
- 2017年浙江工业大学大学生程序设计迎新赛预赛
- 2017年浙江工业大学大学生程序设计迎新赛预赛
- 2017年浙江工业大学大学生程序设计迎新赛决赛 F-栗酱的不等式 [KMP]
- 【2017年浙江工业大学大学生程序设计迎新赛决赛】E 栗酱的数列【思维转化+KMP】
- nowcoder 2017年浙江工业大学大学生程序设计迎新赛预赛-栗酱的异或和
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 I 栗酱和仙剑【模拟】
- 【2017年浙江工业大学大学生程序设计迎新赛决赛】D 序列 【离散化+分块+莫队】
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 J 栗酱和火柴【water】
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 G-取数游戏(区间dp)
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 K qwb的骚扰【分类讨论】
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 L qwb与电阻【dfs】
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 E-栗酱的数列(KMP)