那个人第一步肯定要么能向下走,要么能向右走。于是一定可以判断出上下是否对调,或者左右是否对调。

然后他往这个方向再走一走就能发现一定可以再往旁边走,此时就可以判断出另一个方向是否对调。

都判断出来以后,跑个spfa或者bfs就行了。

细节较多……有一些边界情况需要处理。比如终点在第一行或者第一列的情况。

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int,int> Point;
Point ma[110*110*10];
const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int n,m,End;
char a[110][110];
int num[110][110],pen;
queue<int>Q;
int dis[110*110*10],v[110*110*4*10],e,__next[110*110*4*10],first[110*110*10];
int pre[110*110*10];
bool inq[110*110*10];
void AddEdge(int U,int V){
v[++e]=V;
__next[e]=first[U];
first[U]=e;
}
void spfa(const int &s)
{
memset(dis,0x7f,sizeof(dis));
dis[s]=0; Q.push(s); inq[s]=1;
while(!Q.empty())
{
int U=Q.front();
for(int i=first[U];i;i=__next[i])
if(dis[v[i]]>dis[U]+1)
{
dis[v[i]]=dis[U]+1;
pre[v[i]]=U;
if(!inq[v[i]])
{
Q.push(v[i]);
inq[v[i]]=1;
}
}
Q.pop(); inq[U]=0;
}
}
Point path[110*110*10];
int p;
bool lr,ud;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%s",a[i]+1);
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
num[i][j]=++pen;
ma[pen]=make_pair(i,j);
if(a[i][j]=='F'){
End=num[i][j];
}
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j)if(a[i][j]=='.' || a[i][j]=='F'){
for(int k=0;k<4;++k){
int tx=i+dx[k],ty=j+dy[k];
if(a[tx][ty]=='.' || a[tx][ty]=='F'){
AddEdge(num[i][j],num[tx][ty]);
}
}
}
} if(End==1){
return 0;
}
int x,y;
if((a[2][1]=='*' || n==1) && (a[1][2]=='.' || a[1][2]=='F')){
puts("R");
fflush(stdout);
scanf("%d%d",&x,&y);
if(x==1 && y==1){
lr=1;
}
while(a[x][y]!='F' && a[x+1][y]=='*'){
puts(lr ? "L" : "R");
fflush(stdout);
scanf("%d%d",&x,&y);
}
if(a[x][y]=='F'){
return 0;
}
puts("D");
fflush(stdout);
scanf("%d%d",&x,&y);
if(a[x][y]=='F'){
return 0;
}
if(x==1){
ud=1;
}
}
else if((a[1][2]=='*' || m==1) && (a[2][1]=='.' || a[2][1]=='F')){
puts("D");
fflush(stdout);
scanf("%d%d",&x,&y);
if(x==1 && y==1){
ud=1;
}
while(a[x][y]!='F' && a[x][y+1]=='*'){
puts(ud ? "U" : "D");
fflush(stdout);
scanf("%d%d",&x,&y);
}
if(a[x][y]=='F'){
return 0;
}
puts("R");
fflush(stdout);
scanf("%d%d",&x,&y);
if(a[x][y]=='F'){
return 0;
}
if(y==1){
lr=1;
}
}
else if((a[1][2]=='.' || a[1][2]=='F') && (a[2][1]=='.' || a[2][1]=='F')){
puts("R");
fflush(stdout);
scanf("%d%d",&x,&y);
if(x==1 && y==1){
lr=1;
puts("D");
fflush(stdout);
scanf("%d%d",&x,&y);
if(num[x][y]==End){
return 0;
}
if(x==1 && y==1){
ud=1;
}
}
else{
if(num[x][y]==End){
return 0;
}
puts("L");
fflush(stdout);
scanf("%d%d",&x,&y);
puts("D");
fflush(stdout);
scanf("%d%d",&x,&y);
if(num[x][y]==End){
return 0;
}
if(x==1 && y==1){
ud=1;
}
}
} spfa(num[x][y]);
int U=End;
while(U!=num[x][y]){
path[++p]=ma[U];
U=pre[U];
}
path[++p]=ma[num[x][y]];
for(int i=p;i>1;--i){
if(path[i-1].first-path[i].first==1){
puts(ud ? "U" : "D");
fflush(stdout);
scanf("%d%d",&x,&y);
}
if(path[i-1].first-path[i].first==-1){
puts(ud ? "D" : "U");
fflush(stdout);
scanf("%d%d",&x,&y);
}
if(path[i-1].second-path[i].second==1){
puts(lr ? "L" : "R");
fflush(stdout);
scanf("%d%d",&x,&y);
}
if(path[i-1].second-path[i].second==-1){
puts(lr ? "R" : "L");
fflush(stdout);
scanf("%d%d",&x,&y);
}
}
return 0;
}

【分类讨论】【spfa】【BFS】Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game的更多相关文章

  1. Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game

    地址:http://codeforces.com/contest/811/problem/D 题目: D. Vladik and Favorite Game time limit per test 2 ...

  2. Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C 题意: 给出一行序列,现在要选出一些区间来(不必全部选完),但是相同的数必须出现在同一个区间中,也就是说该数要么 ...

  3. Codeforces Round #416 (Div. 2) B. Vladik and Complicated Book

    B. Vladik and Complicated Book time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  4. 【动态规划】 Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip

    划分那个序列,没必要完全覆盖原序列.对于划分出来的每个序列,对于某个值v,要么全都在该序列,要么全都不在该序列.  一个序列的价值是所有不同的值的异或和.整个的价值是所有划分出来的序列的价值之和.   ...

  5. Codeforces Round #416 (Div. 2) A. Vladik and Courtesy【思维/模拟】

    A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

  7. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  8. Codeforces Round#416 Div.2

    A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...

  9. Codeforces Round #416 (Div. 2) A+B

    A. Vladik and Courtesy 2 seconds 256 megabytes   At regular competition Vladik and Valera won a and  ...

随机推荐

  1. bzoj 1052 dfs

    首先可以二分答案,将最优性问题转化为判定性问题. 对于二分到的边长,我们可以把所有的点看成一个大的矩形,这个矩形为包括所有点的最小矩形,那么贪心的想,3个正方形,第一个肯定放在这个矩形其中的一角,然后 ...

  2. python 学习笔记 sqlalchemy

    数据库表是一个二维表,包含多行多列.把一个表的内容用Python的数据结构表示出来的话,可以用一个list表示多行,list的每一个元素是tuple,表示一行记录,比如,包含id和name的user表 ...

  3. PHP的数据类型

      原始类型共8种: 1,  4种标量类型:boolean(布尔型).integer(整形).float/double(浮点型).string(字符串型): 2,  2种复合型:array(数组).o ...

  4. django给视图添加缓存功能

    在开发过程中,有些视图只是查询数据,而且查询的数据一般不会变化.例如,做地址模块时,省市县都是不会变的.如果用户每次请求地址视图时,都要执行视图返回数据,会给服务端带来不必要的压力.这时候可以用到缓存 ...

  5. Freemarker的页面和JS遍历后台传入的Map

    后端传到前端的Map Freemarker页面遍历Map: JS遍历Map:

  6. Http请求加签、验证操作

    加签.验签的作用 常见的http请求交互过程中,请求参数通过url或者request body等形式传输.但是由于http请求的开放性,使得请求参数很容易被拦截篡改.因此,需要对请求参数进行加签,然后 ...

  7. Django在根据models生成数据库表时报错

    报错信息: E:\Python\s6day103>python manage.py makemigrations Traceback (most recent call last): File ...

  8. 【剑指offer】面试题 9. 用两个栈实现队列

    面试题 9. 用两个栈实现队列 题目描述 题目:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解答过程 import java.util.Stack; publ ...

  9. SpringMVC一些功能

    1.日期格式转换 当页面提交日期格式时 默认的格式为2017/10/1 如果指定日期格式为2017-10-1 //初始化绑定日期格式--不定义初始化格式时只能默认用yyyy/MM/dd格式 @Init ...

  10. VS Code开发技巧集锦

    2016 年 9 月 23-24 日,由 CSDN 和创新工场联合主办的“MDCC 2016 移动开发者大会? 中国”(Mobile Developer Conference China)将在北京? ...