This is an interactive problem.
Vladik has favorite game, in which he plays all his free time.
Game field could be represented as n × m matrix which consists of cells of three types:
- «.» — normal cell, player can visit it.
- «F» — finish cell, player has to finish his way there to win. There is exactly one cell of this type.
- «*» — dangerous cell, if player comes to this cell, he loses.
Initially player is located in the left top cell with coordinates (1, 1).
Player has access to 4 buttons "U", "D", "L", "R", each of them move player up, down, left and right directions respectively.
But it’s not that easy! Sometimes friends play game and change functions of buttons. Function of buttons "L" and "R" could have been swapped, also functions of buttons "U" and "D" could have been swapped. Note that functions of buttons can be changed only at the beginning of the game.
Help Vladik win the game!
First line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — number of rows and columns respectively.
Each of next n lines contains m characters describing corresponding row of field. Set of characters in field is described above.
Guaranteed that cell with coordinates (1, 1) is normal and there is at least one way from initial cell to finish cell without dangerous cells.
You can press buttons no more than 2·n·m times.
To press a button you should print "U", "D", "L", "R" in new line. It’s necessary to print newline character and flush output. After flushing buffer you should read answer from input data. Answer is the pair of space-separated integers x, y — new position of player. In case, if there is no cell in direction of moving, position will not change. If after any move player lost, in other words player move to dangerous cell, then x and y will be equal to - 1.
If after any move player is in finish or dangerous cell, then you should terminate your program.
To finish output buffer (i. e. for operation flush) right after printing direction and newline you should do next:
- fflush(stdout) in C++
- System.out.flush() in Java
- stdout.flush() in Python
- flush(output) in Pascal
- read documentation for other languages.
Hacks
To perform a hack you should use this format:
n m swapLR swapUD a_1 a_2 ... a_n
Where n, m — number of rows and columns in game field. swapLR is equal to 1 in case, when directions "L’’ and "R’’ is swapped, and equal to 0 otherwise. swapUD is equal to 1, when directions "U’’ and "D’’ is swapped, and equal to 0 otherwise. a1, a2, ..., an — description of corresponding rows of game field.
4 3 ... **. F*. ... 1 1 1 2 1 3 1 3 2 3 3 3 4 3 4 2 4 1 3 1
R L L D U U U R R D
思路非常简单,写起来稍微有点麻烦。
直接bfs一遍,把路径还原出来。遇到方向和预定方向不一致的就交换一下。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define _ ios::sync_with_stdio(false)
const int MAXN = 1010;
const int INF = 0xfffffff;
typedef long long ll;
char a[MAXN][MAXN];
int n,m;
char op[4]={'L','R','D','U'};
int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};
struct pos{
int x,y;
}dd[MAXN][MAXN];
typedef pair <int,int> P;
int d[MAXN][MAXN];
int ex,ey;
void bfs(){
queue<P> que;
for(int i=0;i<MAXN;i++)
for(int j=0;j<MAXN;j++)
d[i][j]=INF;
d[1][1]=0;
que.push(P(1,1));
while(que.size()){
P p=que.front();
que.pop();
if(a[p.first][p.second]=='F'){
ex=p.first;
ey=p.second;
break;
}
for(int i=0;i<4;i++){
int nx=p.first+dx[i],ny=p.second+dy[i];
if(1<=nx&&nx<=n&&1<=ny&&ny<=m&&a[nx][ny]!='*'&&d[nx][ny]==INF){
que.push(P(nx,ny));
dd[nx][ny].x=p.first;
dd[nx][ny].y=p.second;
d[nx][ny]=d[p.first][p.second]+1;
}
}
}
}
struct path{
int x,y;
path(){}
path(int xx,int yy){
x=xx;
y=yy;
}
}pa[MAXN];
int tot=1;
void dfs(int x,int y){
if(x==1&&y==1){
pa[tot++]=path(x,y);
return;
}
dfs(dd[x][y].x,dd[x][y].y);
pa[tot++]=path(x,y);
return;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",a[i]+1);
}
bfs();
dfs(ex,ey);
int ok=0;
for(int i=1;i<tot;i++){
if(ok){
i--;
ok=0;
}
for(int j=0;j<4;j++){
if(pa[i].x+dx[j]==pa[i+1].x&&pa[i].y+dy[j]==pa[i+1].y){
printf("%c\n",op[j]);
fflush(stdout);
int x,y;
scanf("%d%d",&x,&y);
int qx,qy;
if(x!=pa[i+1].x){
if(x==pa[i].x){
swap(op[2],op[3]);
ok=1;
}else{
swap(op[2],op[3]);
printf("%c\n",op[j]);
fflush(stdout);
scanf("%d%d",&qx,&qy);
if(a[qx][qy]=='*'){
return 0;
}
ok=1;
}
}
if(y!=pa[i+1].y){
if(y==pa[i].y){
swap(op[0],op[1]);
ok=1;
}else{
swap(op[0],op[1]);
printf("%c\n",op[j]);
fflush(stdout);
scanf("%d%d",&qx,&qy);
if(a[qx][qy]=='*'){
return 0;
}
ok=1;
}
}
}
}
}
}