Codeforces 769C Cycle In Maze【Bfs+思维】这题有点劲啊
来源:互联网 发布:彻底清除kingroot软件 编辑:程序博客网 时间:2024/05/04 07:22
The Robot is in a rectangular maze of size n × m. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is empty. Initially, the Robot is in the empty cell.
Your task is to find lexicographically minimal Robot's cycle with lengthexactly k, which begins and ends in the cell where the Robot was initially. It is allowed to the Robot to visit any cell many times (including starting).
Consider that Robot's way is given as a line which consists of symbols "L", "R", "U" and "D". For example, if firstly the Robot goes down, then left, then right and up, it means that his way is written as "DLRU".
In this task you don't need to minimize the length of the way. Find the minimum lexicographical (in alphabet order as in the dictionary) line which satisfies requirements above.
The first line contains three integers n,m and k (1 ≤ n, m ≤ 1000,1 ≤ k ≤ 106) — the size of the maze and the length of the cycle.
Each of the following n lines contains m symbols — the description of the maze. If the symbol equals to "." the current cell is empty. If the symbol equals to "*" the current cell is occupied by an obstacle. If the symbol equals to "X" then initially the Robot is in this cell and it is empty. It is guaranteed that the symbol "X" is found in the maze exactly once.
Print the lexicographically minimum Robot's way with the length exactly k, which starts and ends in the cell where initially Robot is. If there is no such way, print "IMPOSSIBLE"(without quotes).
2 3 2.**X..
RL
5 6 14..***.*...X...*.....*.**....*.
DLDDLLLRRRUURU
3 3 4****X****
IMPOSSIBLE
In the first sample two cyclic ways for the Robot with the length 2 exist — "UD" and "RL". The second cycle is lexicographically less.
In the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up.
In the third sample the Robot can't move to the neighboring cells, because they are occupied by obstacles.
题目大意:
要求走出一个圈出来,使得走的路径是字典序最小的,一共要走K步,其中‘*’表示不能走,‘.’表示能走;
思路:
首先预处理出来dis【i】【j】表示从起点到点(i,j)的最短距离。
因为记录路径需要强大的内存空间,所以直接暴力记录路径是很难成功的。
我们与其一直想要如何记录路径,其实不妨再跑一遍。
跑最短路的时候按照DLRU的优先级贪心。
同理,再跑一次记录路径的时候,也要按照DLRU的优先级来贪心。
Ac代码:
#include<stdio.h>#include<string.h>#include<queue>#include<iostream>#include<algorithm>using namespace std;struct node{ int x,y;}now,nex;int n,m,k;char a[1005][1005];int dis[1005][1005];char str[5]={'D','L','R','U'};char ans[2000800];int fx[4]={1,0,0,-1};int fy[4]={0,-1,1,0};void Slove(int sx,int sy){ int tmp=k; int tot=0; for(int i=0;i<k;i++) { int dir=-1; for(int j=0;j<4;j++) { int xx=sx+fx[j]; int yy=sy+fy[j]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!='*'&&dis[xx][yy]<tmp) { dir=j; break; } } tmp--; if(dir<0)while(1); int xx=sx+fx[dir]; int yy=sy+fy[dir]; sx=xx; sy=yy; ans[tot++]=str[dir]; } ans[tot]='\0'; printf("%s\n",ans);}void Bfs(int sx,int sy){ for(int i=0;i<=1003;i++) { for(int j=0;j<=1003;j++) { dis[i][j]=0x3f3f3f3f; } } queue<node>s; dis[sx][sy]=0; now.x=sx,now.y=sy; s.push(now); int cnt=0; while(!s.empty()) { cnt++; now=s.front(); s.pop(); for(int i=0;i<4;i++) { nex.x=now.x+fx[i]; nex.y=now.y+fy[i]; if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]!='*') { if(dis[nex.x][nex.y]>dis[now.x][now.y]+1) { dis[nex.x][nex.y]=dis[now.x][now.y]+1; s.push(nex); } } } } if(cnt==1) { printf("IMPOSSIBLE\n"); return ; } else Slove(sx,sy);}int main(){ while(~scanf("%d%d%d",&n,&m,&k)) { int x,y; for(int i=0;i<n;i++) { scanf("%s",a[i]); for(int j=0;j<m;j++) { if(a[i][j]=='X')x=i,y=j; } } if(k%2==1) { printf("IMPOSSIBLE\n"); continue; } Bfs(x,y); }}
- Codeforces 769C Cycle In Maze【Bfs+思维】这题有点劲啊
- Codeforces 377A Maze【Bfs+逆向思维】
- Codeforces 672D Robin Hood【思维+二分】这题思路有点劲啊
- Hero In Maze(bfs)
- Robot in Maze----BFS
- BFS 2470. Robot in Maze
- codeforces 378C MAZE
- BFS之基础题 Hero in Maze 简单版
- Codeforces 117C Cycle
- AOJ 6 Hero In Maze 【BFS】
- 哈理工OJ 2034 Fire Maze【双BFS】【思维】
- Codeforces 586D Phillip and Trains【思维+Bfs】好题~!
- Codeforces 190E Counter Attack【思维+Bfs】好题!
- CodeForces Gym101158H Animal Companion in Maze
- Codeforces 733C Epidemic in Monstropolis 构造+思维
- CodeForces 117C Cycle 搜索
- CodeForces 197D Infinite Maze (BFS+状态压缩)
- Codeforces 557D Vitaly and Cycle【二分图染色+思维】好题~
- vue.js学习笔记之数组的变异方法
- 51nod 1116 K进制下的大数 暴力/数论
- 题目1009:二叉搜索树
- VC++ MFC文件的移动复制删除更名遍历操作
- 视频数据搬运管理
- Codeforces 769C Cycle In Maze【Bfs+思维】这题有点劲啊
- L2-009. 抢红包 结构体排序
- android开发艺术探索 1 Activity典型情况下的生命周期
- Java中的常用语句
- MyBatis批量插入数据
- 无名管道pipe和有名管道FIFO
- JVM体系结构和工作方式
- 剑指Offer系列-面试题57:删除链表中重复的结点
- 一.makefile的初体验