【分类讨论】【spfa】【BFS】Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
那个人第一步肯定要么能向下走,要么能向右走。于是一定可以判断出上下是否对调,或者左右是否对调。
然后他往这个方向再走一走就能发现一定可以再往旁边走,此时就可以判断出另一个方向是否对调。
都判断出来以后,跑个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的更多相关文章
- 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 ...
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
http://codeforces.com/contest/811/problem/C 题意: 给出一行序列,现在要选出一些区间来(不必全部选完),但是相同的数必须出现在同一个区间中,也就是说该数要么 ...
- 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 ...
- 【动态规划】 Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
划分那个序列,没必要完全覆盖原序列.对于划分出来的每个序列,对于某个值v,要么全都在该序列,要么全都不在该序列. 一个序列的价值是所有不同的值的异或和.整个的价值是所有划分出来的序列的价值之和. ...
- 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 ...
- DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- Codeforces Round#416 Div.2
A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...
- Codeforces Round #416 (Div. 2) A+B
A. Vladik and Courtesy 2 seconds 256 megabytes At regular competition Vladik and Valera won a and ...
随机推荐
- node起本地服务器以及实现代理,前端接口转发
上一篇文章写了使用docker来做nginx镜像实现本地的页面代理以及接口转发,但是需要下载docker,这个对于很多人来说还是显得比较麻烦,于是这个文章就是介绍如何只用node就可以代理本地的页面和 ...
- in_device结构和in_ifaddr结构
/* ip配置块 */ struct in_device { /* 二层设备 */ struct net_device *dev; /* 引用计数 */ atomic_t refcnt; /* 是否正 ...
- JS实现判断滚动条滚到页面底部并执行事件的方法
需要了解三个dom元素,分别是:clientHeight.offsetHeight.scrollTop. clientHeight:这个元素的高度,占用整个空间的高度,所以,如果一个div有滚动条,那 ...
- 【python】msgpack使用
1.存储数据 import msgpack var={'a':'this','b':'is','c':'a test'} with open('file.name','wb') as f: msgpa ...
- for in 对象时,属性为非负整数的情况
在我做一个需求的时候 for in 一个对象,对象的属性都是数字 但是我想给这个对象加一个默认的属性跟值 原对象是{5446:"广州市"}.....类似于下去 然后我想给我页面展示 ...
- ps -ef和ps aux的区别
ps -ef 是用标准格式(standard syntax)显示进程 ,ps aux 是用BSD格式(BSD syntax)来显示进程 ps -ef 结果如下: 部分含义如下: PPID //父进程I ...
- 【hdoj_2152】Fruit(母函数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2152 本题采用母函数模板求解,母函数模板如下: http://blog.csdn.net/ten_sory ...
- prometheus 结合 kubernetes时,提示User cannot list services at the cluster scope.如何解决?
这表示认证这一阶段已通过,赋权未到位. 但kube-prometheus是一体化,全家桶安装,我试过几次自定义clusterRole,binding,都解决不完全. 于是来个快刀. 搞定. kubec ...
- oracle 导入dmp文件
/*第1步:创建临时表空间 */ create temporary tablespace webdata_temp tempfile 'D:\oracle\product\10.2.0\oradat ...
- AC日记——餐巾计划问题 洛谷 P1084
餐巾计划问题 思路: 氧气优化水过: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 4005 #define ...