Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
地址:http://codeforces.com/contest/811/problem/D
题目:
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
In first test case all four directions swapped with their opposite directions. Protocol of interaction In more convenient form:
This test could be presenter for hack in following way:
4 3 1 1
...
**.
F*.
...
思路:
先跑一遍最短路,求出路径后先按正确方向走,如果返回的地点不对就是反向了。
Ps:然而还是t了几发,没看到上下反向和左右反向是分开的,盲人acmer
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 #define MP make_pair
6 #define PB push_back
7 typedef long long LL;
8 typedef pair<int,int> PII;
9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
13
14 int n,m;
15 int dx[]={0,0,1,-1};
16 int dy[]={1,-1,0,0};
17 char mp[200][200],ac[]={'U','D','L','R'};
18 PII dst,pre[200][200];
19 int vis[200][200];
20 void bfs(void)
21 {
22 queue<PII> q;
23 q.push(dst);
24 vis[dst.first][dst.second]=0;
25 while(q.size())
26 {
27 PII u=q.front();q.pop();
28 if(u==MP(1,1))
29 return;
30 for(int i=0;i<4;i++)
31 {
32 int x=u.first+dx[i],y=u.second+dy[i];
33 if(x<=n&&x>=1&&y<=m&&y>=1&&mp[x][y]!='*'&&!vis[x][y])
34 pre[x][y]=u,q.push(MP(x,y)),vis[x][y]=1;
35 }
36 }
37 }
38 int sc(PII &st,PII &se)
39 {
40 int l=se.first-st.first,r=se.second-st.second;
41 if(l==1) return 1;
42 else if(l==-1) return 0;
43 else if(r==1) return 3;
44 return 2;
45 }
46 int main(void)
47 {
48 scanf("%d%d",&n,&m);
49 for(int i=1;i<=n;i++)
50 {
51 scanf("%s",&mp[i][1]);
52 for(int j=1;j<=m;j++)
53 if(mp[i][j]=='F')
54 dst=MP(i,j);
55 }
56 bfs();
57 int x,y,ans,cnt=0;
58 bool op1=0,op2=0;
59 PII st,se;
60 st=MP(1,1);
61 while(1)
62 {
63 se=pre[st.first][st.second];
64 ans=sc(st,se);
65 if(op1 && ans==0) ans=1;
66 else if(op1 && ans==1) ans=0;
67 if(op2 && ans==2) ans=3;
68 else if(op2 && ans==3) ans=2;
69 printf("%c\n",ac[ans]);
70 fflush(stdout);
71 scanf("%d%d",&x,&y);
72 if(MP(x,y)!=se)
73 ans<=1?op1=1:op2=1;
74 else
75 st=se;
76 if(MP(x,y)==dst)
77 break;
78 }
79 return 0;
80 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。