#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N =14;
const int inf=0x7f7f7f7f7f;
struct pos{int x, y, dir;};
char ch,op[2];
int mx, my;
int d[N][N][3];
int nextmx[3][4] = {{-2, 1, 0, 0}, {-1, 1, 0, 0}, {-1, 2, 0, 0}};
int nextmy[3][4] = {{0, 0, -2, 1}, {0, 0, -1, 2}, {0, 0, -1, 1}};
int ndir[3][4] = {{2, 2, 1, 1}, {1, 1, 0, 0}, {0, 0, 2, 2}};
inline int read(){
int x=0,f=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-')f*=-1;
c=getchar();
}
while(isdigit(c)){
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}
return x*f;
}
int min(int a,int b){
if(a>b)return b;
else return a;
}
inline int bfs(int x, int y, int z1)
{
d[x][y][z1] = 0;
queue<pos> q;
q.push({x, y, z1});
int ans = inf;
while (q.size())
{
pos t = q.front();
q.pop();
int x = t.x, y = t.y, z = t.dir;
if (!(x%3)&&!(y%3)&&!z)
{
int nx=mx+x-3, ny=my+y-3;
int xd=nx/3*2,yd=ny/3*2;
if (xd<=-1||yd<=-1)
continue;
else ans=min(ans, d[x][y][z]+xd+yd);
}
for (int i=0;i<4;i++)
{
int a=x+nextmx[z][i], b=y+nextmy[z][i];
int _dir= ndir[z][i];
if (a<0||a>=N||b<0||b>=N)
continue;
if (d[a][b][_dir]==-1)
d[a][b][_dir]=d[x][y][z] + 1,q.push({a,b, _dir});
}
}
return ans;
}
signed main()
{ //freopen("data.in","r",stdin);
//freopen("baoli.out","w",stdout);
int t;
while (cin >> op >> mx >> my&&op[0]!=EOF)
{ memset(d, -1, sizeof d);
int z;
if (op[0] == 'U')
cout<<bfs(mx%3+3,my%3+3,0)<<"\n";
else if (op[0] == 'H')
cout<<bfs(mx%3+3,my%3+3,1)<<"\n";
else
cout<<bfs(mx%3+3,my%3+3,2)<<"\n";
}
return 0;
}
我的代码并没判负数坐标也可以过这题,理论上数据中没有负数坐标,因此数据过水,卡不掉一些代码。
Hack
1
H -8 -113
81