NOIP 模拟 $86\; \rm 宝可梦$
题解 \(by\;zj\varphi\)
题目保证每两个点之间只会有一条简单路径,这就是一棵树。
让求的就是按一定顺序的欧拉序。
将每个格子拆成四个方向,代表它是由哪个方向到这的,这些算是不同的状态。
每次走路拐弯时,先右转,如果不行再直行,不行再左转,最后不行那就回去。
这样所有的状态一定都会被遍历一遍,求答案时只需取当前点钦定的下一步的状态即可。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
static const int N=1e5+7;
int *id[4][N],vs[N*20],a,b,x,y,n,m,q,cnt,nx,ny,df,stp;
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
bool *vis[N];
char s[N],ch[3];
auto jud=[](int x,int y) {return x<=n&&x>0&&y<=m&&y>0&&vis[x][y];};
inline int main() {
FI=freopen("pokemon.in","r",stdin);
FO=freopen("pokemon.out","w",stdout);
scanf("%d%d",&n,&m);
for (ri i(1);i<=n;pd(i)) {
scanf("%s",s+1);
vis[i]=new bool[m+2];
id[0][i]=new int[m+2];
id[1][i]=new int[m+2];
id[2][i]=new int[m+2];
id[3][i]=new int[m+2];
for (ri j(1);j<=m;pd(j)) {
vis[i][j]=s[j]=='.';
for (ri k(0);k<4;pd(k)) id[k][i][j]=++cnt;
}
}
for (ri i(1);i<=n;pd(i)) {
for (ri j(1);j<=m;pd(j))
if (vis[i][j]) {
for (ri k(0);k<4;pd(k))
if (jud(i+dx[k],j+dy[k])) {
nx=i+dx[k],ny=j+dy[k];
df=k;
break;
}
if (nx) break;
}
if (nx) break;
}
while(!vs[id[df][nx][ny]]) {
vs[id[df][nx][ny]]=++stp;
for (ri i(df-1<0?3:df-1);;i=i+1>3?0:i+1)
if (jud(nx+dx[i],ny+dy[i])) {
nx+=dx[i],ny+=dy[i];
df=i;
break;
}
}
scanf("%d",&q);
for (ri i(1);i<=q;pd(i)) {
scanf("%d%d%d%d%s",&a,&b,&x,&y,ch+1);
if (a==x&&b==y) {printf("0\n");continue;}
int dt;
if (ch[1]=='U') dt=0;
else if (ch[1]=='L') dt=1;
else if (ch[1]=='D') dt=2;
else if (ch[1]=='R') dt=3;
int st=vs[id[dt][a+dx[dt]][b+dy[dt]]]-1,ed=1e9+7;
for (ri j(0);j<4;pd(j))
if (vs[id[j][x][y]])
ed=cmin(ed,vs[id[j][x][y]]<st?vs[id[j][x][y]]+stp:vs[id[j][x][y]]);
printf("%d\n",ed-st);
}
return 0;
}
}
int main() {return nanfeng::main();}