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

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

都判断出来以后,跑个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. java 深度拷贝 复制 深度复制

    1.深度拷贝.复制代码实现 最近需要用到比较两个对象属性的变化,其中一个是oldObj,另外一个是newObj,oldObj是newObj的前一个状态,所以需要在newObj的某个状态时,复制一个一样 ...

  2. 关于scala 集合 List Map Set

    1,数组 2,List,ListBuffer 3, Map , mutable.Map

  3. python初步学习-python数据类型之strings(字符串)

    数据类型-字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号(''或者"")来创建字符串 var1 = 'Hello World!' var2 = "P ...

  4. 20、redis和memcached比较?

    1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...

  5. 嵌入式 uboot引导kernel,kernel引导fs【转】

    转自:http://www.cnblogs.com/lidabo/p/5383934.html#3639633 1.uboot引导kernel: u-boot中有个bootm命令,它可以引导内存中的应 ...

  6. UVA 11076 Add Again

    题目链接:UVA-33478 题意为给定n个数,求这n个数能组成的所有不同的排列组成的数字的和. 思路:发现对于任意一个数字,其在每一位出现的次数是相同的.换言之,所有数字的每一位相加的和是相同的. ...

  7. Redis 集群使用(2)

    Redis包含三种集群策略: 主从复制 哨兵模式 redis cluster 主从复制 在主从复制中,数据分为两类:主数据库(master)和 从数据库(slave).其中主从复制有如下特点: 主数据 ...

  8. C语言写随机数

    #include <stdio.h> #include <stdlib.h> #include <time.h> ; unsigned int rand0(); v ...

  9. Windows7 + OSG3.6 + VS2017 + Qt5.11

    一.准备工作 下载需要的材料: 1. OSG稳定版源代码, 3.6.3版本 2. 第三方库,选择VS2017对应的版本  https://download.osgvisual.org/3rdParty ...

  10. ubuntu wine 使用

    运行程序 wine xxx.exe 图形界面程序(普通程序):直接使用 wine 命令行的DOS程序:wineconsole 代替 wine.这才是正常的运行方式.不使用wineconsole运行命令 ...