Problem:
全探索お姉さんの休日
全探索お姉さんはとても優秀な女性である。
お姉さんは格子状の道の経路の数え上げを数千程度なら簡単に数え上げてしまう。

あなたと全探索お姉さんは今、六角形のタイルが敷き詰められた部屋にいる。
お姉さんは初めて見る六角形にとても興奮している様子である。
六角形の並びを座標に表すことに不慣れなお姉さんは図1のような座標系で部屋を表した。
図1
あなたはこの座標系上である地点からある地点まで移動したい。
しかし、お姉さんは 1 分ごとに動きたい方向をあなたに指示する。
いつものお姉さんなら同じ座標の場所を通らないようにあなたに移動の指示を出すだろう。
しかし、この座標系に不慣れなお姉さんは
|x×y×t|
(
x
：
x
座標、
y
：
y
座標、
t
:最初の指示からの経過時間[分])を 6 で割った余り
に対応する方向（図で示す番号と対応）を指示するだけで、まったくのでたらめな方向にあなたを誘導する。
図2
お姉さんを傷つけたくないあなたは、お姉さんの指示をできるだけ守りつつ目的地までたどり着きたい。
あなたは許された行動は下の 7 つの行動である。
方向 0 へ 1 タイル移動
方向 1 へ 1 タイル移動
方向 2 へ 1 タイル移動
方向 3 へ 1 タイル移動
方向 4 へ 1 タイル移動
方向 5 へ 1 タイル移動
その場に留まる
お姉さんが指示を出した直後にあなたはこれら行動のうちの必ず1つを行う。

部屋には家具があり家具が配置されているタイルの中に移動することはできない。
また、
y
座標の絶対値が
ly
を超えたり
x
座標の絶対値が
lx
を超えるような移動は許されていない。
しかし、お姉さんはそのような移動を指示することがある。
指示を無視するとはお姉さんが示した方向と異なる方向へ移動するか、
もしくは、その場に留まることである。

目的地にたどり着くために最小で何度指示を無視すれば良いかを出力せよ。
目的地にたどり着くことが不可能な場合は -1 を出力せよ。
Input
入力は以下の形式で与えられる。
sx
sy
gx
gy
n
x
1
y
1
...
x
i
y
i
...
x
n
y
n
lx
ly
ここで、
sx
,
sy
は出発地点の座標
gx
,
gy
は目的地の座標
n
は部屋に配置されている家具の数
x
i
,
y
i
は家具があるタイルの座標
lx
,
ly
は移動できる
X
,
Y
それぞれの座標の絶対値の上限
である。
Constraints
入力はすべて整数
-lx ≤ sx
,
gx ≤ lx
-ly ≤ sy
,
gy ≤ ly
(sx, sy) ≠ (gx, gy)
(xi, yi) ≠ (sx, sy)(1≤i≤n)
(xi, yi) ≠ (gx, gy)(1≤i≤n)
(xi, yi) ≠ (xj, yj)(i ≠ j)
0≤ n ≤ 1000
-lx ≤ xi ≤ lx (1≤i≤n)
-ly ≤ yi ≤ ly (1≤i≤n)
0 < lx, ly ≤ 100
Output
出力は1つの整数を含む1行で出力せよ。
目的地にたどり着ける場合は、最小の指示を無視する回数を出力せよ。
目的地にたどり着くことが不可能な場合は-1を出力せよ。
Sample Input 1
0 0 0 2
0
2 2
Output for the Sample Input 1
0
Sample Input 2
0 0 0 2
6
0 1
1 0
1 -1
0 -1
-1 -1
-1 0
2 2
Output for the Sample Input 2
-1
Sample Input 3
0 0 0 2
1
0 1
2 2
Output for the Sample Input 3
1

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = (r)-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}

int dx[] = {0,1,1,0,-1,-1,0};
int dy1[] = {1,0,-1,-1,-1,0,0};
int dy2[] = {1,1,0,-1,0,1,0};


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  pii S,T;cin >> S >> T;
  int n;cin >> n;
  vvb blk(300,vb(300));
  const int MD = 150;
  rep(i,0,n){
    int x,y;cin >> x >> y;
    blk[MD+x][MD+y] = 1;
  }
  pii L;cin >> L;
  deque<tuple<int,int,int>> dq;
  vvvi d(300,vvi(300,vi(6,MOD)));
  d[MD+S.first][MD+S.second][0] = 0;
  dq.push_back({S.first,S.second,0});
  while(sz(dq)){
    auto [ox,oy,ot] = dq.front();dq.pop_front();
    int k = abs(ox*oy)*ot%6;
    rep(dir,0,7){
      int nx = ox + dx[dir],ny = oy + dy1[dir],nt = (ot+1)%6;
      if(ox & 1)ny = oy + dy2[dir];
      if(blk[MD+nx][MD+ny])continue;
      if(abs(nx) > L.first || abs(ny) > L.second)continue;
      if(dir == k){
        if(d[MD+ox][MD+oy][ot] < d[MD+nx][MD+ny][nt]){
          d[MD+nx][MD+ny][nt] = d[MD+ox][MD+oy][ot];
          dq.push_front({nx,ny,nt});
        }
      }else{
        if(d[MD+ox][MD+oy][ot]+1 < d[MD+nx][MD+ny][nt]){
          d[MD+nx][MD+ny][nt] = d[MD+ox][MD+oy][ot]+1;
          dq.push_back({nx,ny,nt});
        }
      }
    }
  }
  ll res = MOD;
  rep(i,0,6)chmin(res,d[MD+T.first][MD+T.second][i]);
  if(res == MOD)res = -1;
  cout << res << endl;
  

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11292
final_rank: 6

code_snippet: #include <queue>
#include <vector>
#include <iostream>
using namespace std;
vector<int> dx = { 0, 1, 1, 0, -1, -1, 0 };
vector<vector<int> > dy = {
	{ 1, 0, -1, -1, -1, 0, 0 },
	{ 1, 1, 0, -1, 0, 1, 0 }
};
int sx, sy, gx, gy, lx, ly, n;
int main() {
	cin >> sx >> sy >> gx >> gy >> n;
	vector<int> ex(n), ey(n);
	for (int i = 0; i < n; i++) cin >> ex[i] >> ey[i];
	cin >> lx >> ly;
	vector<vector<bool> > ok(ly * 2 + 1, vector<bool>(lx * 2 + 1, true));
	for (int i = 0; i < n; i++) ok[ey[i] + ly][ex[i] + lx] = false;
	vector<vector<vector<int> > > dist(ly * 2 + 1, vector<vector<int> >(lx * 2 + 1, vector<int>(6, 999999999))); dist[sy + ly][sx + lx][0] = 0;
	priority_queue<vector<int> > que; que.push({ 0, sx, sy, 0 });
	while (!que.empty()) {
		vector<int> u = que.top(); que.pop();
		int fx = u[1], fy = u[2], ft = u[3], cur = dist[fy + ly][fx + lx][ft];
		for (int i = 0; i < 7; i++) {
			int tx = fx + dx[i], ty = fy + dy[abs(fx) % 2][i], tt = (ft + 1) % 6;
			int cost = (abs(fx * fy * ft) % 6 == i ? 0 : 1);
			if (abs(tx) <= lx && abs(ty) <= ly && ok[ty + ly][tx + lx] && dist[ty + ly][tx + lx][tt] > cur + cost) {
				dist[ty + ly][tx + lx][tt] = cur + cost;
				que.push({ -dist[ty + ly][tx + lx][tt], tx, ty, tt });
			}
		}
	}
	int ret = 999999999;
	for (int i = 0; i < 6; i++) ret = min(ret, dist[gy + ly][gx + lx][i]);
	cout << (ret != 999999999 ? ret : -1) << endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 5820
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int LX, LY;

pair<int,int> Move(int X, int Y, int dir) {
    if (dir == 6) return {X,Y};
    if (X %2 == LX % 2) {
        if (dir == 0) return {X,Y+1};
        if (dir == 1) return {X+1,Y};
        if (dir == 2) return {X+1,Y-1};
        if (dir == 3) return {X,Y-1};
        if (dir == 4) return {X-1,Y-1};
        if (dir == 5) return {X-1,Y};
    }
    else {
        if (dir == 0) return {X,Y+1};
        if (dir == 1) return {X+1,Y+1};
        if (dir == 2) return {X+1,Y};
        if (dir == 3) return {X,Y-1};
        if (dir == 4) return {X-1,Y};
        if (dir == 5) return {X-1,Y+1};
    }
    return {0,0};
}

int Dir(int X, int Y, int T) {
    return (abs(X-LX)*abs(Y-LY)*T)%6;
}

int main() {
    int SX, SY, GX, GY, N;
    cin >> SX >> SY >> GX >> GY >> N;
    vector<int> X(N), Y(N);
    rep(i,0,N) cin >> X[i] >> Y[i];
    cin >> LX >> LY;
    SX += LX, SY += LY, GX += LX, GY += LY;
    int MX = LX*2+1, MY = LY*2+1;
    vector<vector<bool>> B(MX,vector<bool>(MY,true));
    rep(i,0,N) X[i] += LX, Y[i] += LY, B[X[i]][Y[i]] = false;
    auto in = [&](int i, int j) -> bool {return (0 <= i && i < MX && 0 <= j && j < MY && B[i][j]);};
    auto encode = [&](int i, int j, int k) -> int {return i*MY*6+j*6+k;};
    auto decode = [&](int i) -> tuple<int,int,int> {return {i/(MY*6),(i/6)%MY,i%6};};
    vector<int> Dist(MX*MY*6,inf);
    Dist[encode(SX,SY,0)] = 0;
    deque<int> Q;
    Q.push_back(encode(SX,SY,0));
    while(!Q.empty()) {
        int P = Q.front();
        auto [X,Y,T] = decode(P);
        Q.pop_front();
        rep(i,0,7) {
            auto [NX,NY] = Move(X,Y,i);
            if (!in(NX,NY)) continue;
            int ND;
            if (i == Dir(X,Y,T)) ND = 0;
            else ND = 1;
            if (chmin(Dist[encode(NX,NY,(T+1)%6)],Dist[P]+ND)) {
                if (ND == 0) Q.push_front(encode(NX,NY,(T+1)%6));
                else Q.push_back(encode(NX,NY,(T+1)%6));
            }
        }
    }
    int ANS = inf;
    rep(i,0,6) chmin(ANS,Dist[encode(GX,GY,i)]);
    cout << (ANS == inf ? -1 : ANS) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3976
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(), x.end()
template <class A, class B>
inline bool chmin(A &a, const B &b) {
    return b < a && (a = b, true);
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
    return b > a && (a = b, true);
}

int main() {
    const int OFFSET = 105;
    const int H = 2 * OFFSET;
    const int W = 2 * OFFSET;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    int n;
    cin >> n;
    vector ng(H, vector<bool>(W));
    rep(i, n) {
        int x, y;
        cin >> x >> y;
        ng[x + OFFSET][y + OFFSET] = true;
    }
    int lx, ly;
    cin >> lx >> ly;
    const int DX[2][7] = {
        {0, 1, 1, 0, -1, -1, 0}, 
        {0, 1, 1, 0, -1, -1, 0}
    };
    const int DY[2][7] = {
        {1, 0, -1, -1, -1, 0, 0},
        {1, 1, 0, -1, 0, 1, 0}
    };
    // 補正する
    auto encode = [&](int x, int y) {
        return pair<int, int>(x + OFFSET, y + OFFSET);
    };
    // 補正を消す
    auto decode = [&](int X, int Y) {
        return pair<int, int>(X - OFFSET, Y - OFFSET);
    };
    // 補正ありで条件を満たしているか判定
    auto valid = [&](int X, int Y) {
        auto [x, y] = decode(X, Y);
        return abs(x) <= lx && abs(y) <= ly;
    };
    // 補正ありで障害物があるか判定
    auto obstacle = [&](int X, int Y) {
        assert(valid(X, Y));
        return ng[X][Y];
    };
    auto sister = [&](int X, int Y, int t) {
        auto [x, y] = decode(X, Y);
        return abs(x * y * t) % 6;
    };
    auto parity = [&](int X) {
        return (X - OFFSET) % 2 != 0;
    };

    const int INF = 1e9;
    // dist[X][Y][t]: (X, Y)にいて、時間を6で割ったあまりが6であるときの最小無視回数
    vector dist(H, vector(W, vector<int>(6, INF)));
    // (X, Y, t): 補正ありの座標と時間を6で割ったあまり
    priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>, greater<tuple<int, int, int, int>>> que;
    auto [sX, sY] = encode(sx, sy);
    auto [gX, gY] = encode(gx, gy);
    dist[sX][sY][0] = 0;
    que.emplace(0, sX, sY, 0);
    while (!que.empty()) {
        auto [cd, cX, cY, ct] = que.top();
        que.pop();
        if (dist[cX][cY][ct] < cd) continue;
        assert(dist[cX][cY][ct] != INF);
        assert(0 <= ct && ct < 6);
        int sug = sister(cX, cY, ct);
        for (int d = 0; d < 7; d++) {
            int nX = cX + DX[parity(cX)][d];
            int nY = cY + DY[parity(cX)][d];
            int nt = (ct + 1) % 6;
            int w = (d != sug);
            if (valid(nX, nY) && !obstacle(nX, nY) && dist[nX][nY][nt] > dist[cX][cY][ct] + w) {
                dist[nX][nY][nt] = dist[cX][cY][ct] + w;
                que.emplace(dist[nX][nY][nt], nX, nY, nt);
            }
        }
    }
    int ans = INF;
    rep(i, 6) chmin(ans, dist[gX][gY][i]);
    cout << (ans == INF ? -1 : ans) << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5992
final_rank: 4

code_snippet: #include <bits/stdc++.h>

int dx[6]{ 0, 1, 1, 0, -1, -1 };
int dy1[6]{ 1, 0, -1, -1, -1, 0 };
int dy2[6]{ 1, 1, 0, -1, 0, 1 };

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int sx, sy, gx, gy;
    std::cin >> sx >> sy >> gx >> gy;
    int N;
    std::cin >> N;
    std::set<std::pair<int, int>> set;
    for (int i{} ; i < N ; i++) {
        int x, y;
        std::cin >> x >> y;
        set.emplace(x, y);
    }
    int lx, ly;
    std::cin >> lx >> ly;
    int width{2 * lx + 1}, height{2 * ly + 1};
    auto f{[&](int time, int x, int y) -> int {
        x += lx;
        y += ly;
        assert(0 <= time and time < 6);
        assert(0 <= x and x < width);
        assert(0 <= y and y < height);
        return time * width * height + x * height + y;
    }};
    auto in{[&](int x, int y) -> int {
        return -lx <= x and x <= lx and 
            -ly <= y and y <= ly and 
            set.find(std::pair{ x, y }) == set.end();
    }};
    auto command{[&](int time, int x, int y) -> int {
        return std::abs(time * x * y) % 6;
    }};
    std::vector<std::vector<std::pair<int, int>>> g(6 * width * height);
    for (int x{-lx} ; x <= lx ; x++) {
        for (int y{-ly} ; y <= ly ; y++) {
            if (!in(x, y)) continue;
            for (int t{} ; t < 6 ; t++) {
                // 留まる
                g[f(t, x, y)].emplace_back(f((t + 1) % 6, x, y), 1);
                int com{command(t, x, y)};
                for (int d{} ; d < 6 ; d++) {
                    int nx{x + dx[d]}, ny{y + (x % 2 ? dy2[d] : dy1[d])}; 
                    if (!in(nx, ny)) continue;
                    int cost{com == d ? 0 : 1};
                    //if (t == 2 and x == 1 and y == 1) {
                    //    std::cout << d << ' ' << nx << ' ' << ny << ' ' << cost << std::endl;
                    //}
                    g[f(t, x, y)].emplace_back(f((t + 1) % 6, nx, ny), cost);
                }
            }
        }
    }
    const int INF{(int)1e9};
    std::vector<int> dist(6 * width * height, INF);
    dist[f(0, sx, sy)] = 0;
    std::deque<int> deq;
    deq.emplace_back(f(0, sx, sy));
    while (deq.size()) {
        auto v{deq.front()};
        deq.pop_front();
        for (auto [x, w] : g[v]) {
            if (dist[x] > dist[v] + w) {
                dist[x] = dist[v] + w;
                if (w == 1) deq.emplace_back(x);
                else if (w == 0) deq.emplace_front(x);
                else assert(false);
            }
        }
    }
    int ans{INF};
    for (int t{} ; t < 6 ; t++) {
        ans = std::min(ans, dist[f(t, gx, gy)]);
    }
    if (ans == INF) {
        std::cout << -1 << '\n';
    }
    else {
        std::cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 40
memory_kb: 28184
final_rank: 7

code_snippet: #include <bits/stdc++.h>

int dx[6]{ 0, 1, 1, 0, -1, -1 };
int dy1[6]{ 1, 0, -1, -1, -1, 0 };
int dy2[6]{ 1, 1, 0, -1, 0, 1 };

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int sx, sy, gx, gy;
    std::cin >> sx >> sy >> gx >> gy;
    int N;
    std::cin >> N;
    std::set<std::pair<int, int>> set;
    for (int i{} ; i < N ; i++) {
        int x, y;
        std::cin >> x >> y;
        set.emplace(x, y);
    }
    int lx, ly;
    std::cin >> lx >> ly;
    int width{2 * lx + 1}, height{2 * ly + 1};
    auto f{[&](int time, int x, int y) -> int {
        x += lx;
        y += ly;
        assert(0 <= time and time < 6);
        assert(0 <= x and x < width);
        assert(0 <= y and y < height);
        return time * width * height + x * height + y;
    }};
    auto in{[&](int x, int y) -> int {
        return -lx <= x and x <= lx and 
            -ly <= y and y <= ly and 
            set.find(std::pair{ x, y }) == set.end();
    }};
    auto command{[&](int time, int x, int y) -> int {
        return std::abs(time * x * y) % 6;
    }};
    std::vector<std::vector<std::pair<int, int>>> g(6 * width * height);
    for (int x{-lx} ; x <= lx ; x++) {
        for (int y{-ly} ; y <= ly ; y++) {
            if (!in(x, y)) continue;
            for (int t{} ; t < 6 ; t++) {
                // 留まる
                g[f(t, x, y)].emplace_back(f((t + 1) % 6, x, y), 1);
                int com{command(t, x, y)};
                for (int d{} ; d < 6 ; d++) {
                    int nx{x + dx[d]}, ny{y + (y % 2 ? dy2[d] : dy1[d])}; 
                    if (!in(nx, ny)) continue;
                    int cost{com == d ? 0 : 1};
                    //if (t == 2 and x == 1 and y == 1) {
                    //    std::cout << d << ' ' << nx << ' ' << ny << ' ' << cost << std::endl;
                    //}
                    g[f(t, x, y)].emplace_back(f((t + 1) % 6, nx, ny), cost);
                }
            }
        }
    }
    const int INF{(int)1e9};
    std::vector<int> dist(6 * width * height, INF);
    dist[f(0, sx, sy)] = 0;
    std::deque<int> deq;
    deq.emplace_back(f(0, sx, sy));
    while (deq.size()) {
        auto v{deq.front()};
        deq.pop_front();
        for (auto [x, w] : g[v]) {
            if (dist[x] > dist[v] + w) {
                dist[x] = dist[v] + w;
                if (w == 1) deq.emplace_back(x);
                else if (w == 0) deq.emplace_front(x);
                else assert(false);
            }
        }
    }
    int ans{INF};
    for (int t{} ; t < 6 ; t++) {
        ans = std::min(ans, dist[f(t, gx, gy)]);
    }
    if (ans == INF) {
        std::cout << -1 << '\n';
    }
    else {
        std::cout << ans << '\n';
    }
}
accuracy: 0.25
time_ms: 40
memory_kb: 28152
final_rank: 13

code_snippet: #include <bits/stdc++.h>

const int P = 6;
const int MAX = 400;
bool already[205][205][MAX][P];

int main() {
    short si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<short, short>> obs;
    for (int i = 0; i < n; i++) {
        short x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    short offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    already[si][sj][0][0] = true;
    using Index = std::tuple<short, short, short, short>;
    std::queue<Index> que;
    que.push({si, sj, 0, 0});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const short DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const short DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        if (k < 0) {
            return ((6 - (((k % 6) + 6) % 6)) * p) % 6;
        }
        return (k * p) % 6;
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((i - offset_i) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            short ni = i + (flag ? DI1[d] : DI2[d]);
            short nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            short nc = c + (sister != d);
            short np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 1
time_ms: 2680
memory_kb: 102520
final_rank: 11

code_snippet: #include <bits/stdc++.h>

const int P = 6;
const int MAX = 400;
bool already[205][205][MAX][P];

int main() {
    short si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<short, short>> obs;
    for (int i = 0; i < n; i++) {
        short x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    short offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    already[si][sj][0][0] = true;
    using Index = std::tuple<short, short, short, short>;
    std::queue<Index> que;
    que.push({si, sj, 0, 0});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const short DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const short DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        if (k < 0) {
            return ((5 - (((k % 6) + 6) % 6)) * p) % 6;
        }
        return (k * p) % 6;
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((i - offset_i) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            short ni = i + (flag ? DI1[d] : DI2[d]);
            short nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            short nc = c + (sister != d);
            short np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.25
time_ms: 2480
memory_kb: 102648
final_rank: 17

code_snippet: #include <bits/stdc++.h>

const int P = 6;
const int MAX = 500;
bool already[205][205][MAX][P];

int main() {
    short si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<short, short>> obs;
    for (int i = 0; i < n; i++) {
        short x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    short offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    already[si][sj][0][0] = true;
    using Index = std::tuple<short, short, short, short>;
    std::queue<Index> que;
    que.push({si, sj, 0, 0});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const short DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const short DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        if (k < 0) {
            return ((5 - (((k % 6) + 6) % 6)) * p) % 6;
        }
        return (k * p) % 6;
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((j - offset_j) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            short ni = i + (flag ? DI1[d] : DI2[d]);
            short nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            short nc = c + (sister != d);
            short np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.17857142857142858
time_ms: 40
memory_kb: 13892
final_rank: 18

code_snippet: #include <bits/stdc++.h>

const int P = 6;
const int MAX = 210;
bool already[MAX][MAX][MAX][P];

int main() {
    short si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<short, short>> obs;
    for (int i = 0; i < n; i++) {
        short x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    short offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    already[si][sj][0][0] = true;
    using Index = std::tuple<short, short, short, short>;
    std::queue<Index> que;
    que.push({si, sj, 0, 0});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const short DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const short DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        return std::abs((k * p) % 6);
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((j - offset_j) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            short ni = i + (flag ? DI1[d] : DI2[d]);
            short nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            short nc = c + (sister != d);
            short np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.25
time_ms: 700
memory_kb: 55056
final_rank: 15

code_snippet: #include <bits/stdc++.h>

const int P = 6;
const int MAX = 210;
bool already[MAX][MAX][MAX][P];

int main() {
    short si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<short, short>> obs;
    for (int i = 0; i < n; i++) {
        int x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    short offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    already[si][sj][0][0] = true;
    using Index = std::tuple<short, short, short, short>;
    std::queue<Index> que;
    que.push({si, sj, 0, 0});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const short DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const short DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        return std::abs((k * p) % 6);
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((j - offset_j) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            short ni = i + (flag ? DI1[d] : DI2[d]);
            short nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            short nc = c + (sister != d);
            short np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.25
time_ms: 710
memory_kb: 54976
final_rank: 16

code_snippet: #include <bits/stdc++.h>

const int P = 6;
const int MAX = 205;
bool already[MAX][MAX][MAX][P];

int main() {
    short si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<short, short>> obs;
    for (int i = 0; i < n; i++) {
        int x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    short offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    already[si][sj][0][0] = true;
    using Index = std::tuple<short, short, short, char>;
    std::queue<Index> que;
    que.push({si, sj, 0, 0});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const short DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const short DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const short DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        return std::abs((k * p) % 6);
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((j - offset_j) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            short ni = i + (flag ? DI1[d] : DI2[d]);
            short nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            short nc = c + (sister != d);
            short np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.25
time_ms: 610
memory_kb: 49028
final_rank: 14

code_snippet: #include <bits/stdc++.h>

int main() {
    int si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<int, int>> obs;
    for (int i = 0; i < n; i++) {
        int x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    int offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    const int P = 6;
    const int MAX = 500;
    std::vector already(h, std::vector(w, std::vector(MAX, std::vector<bool>(P))));
    already[si][sj][0][1] = true;
    using Index = std::array<int, 4>;
    std::queue<Index> que;
    que.push({si, sj, 0, 1});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const int DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const int DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const int DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const int DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        return std::abs((k * p) % 6);
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((j - offset_j) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            int ni = i + (flag ? DI1[d] : DI2[d]);
            int nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            int nc = c + (sister != d);
            int np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.14285714285714285
time_ms: 20
memory_kb: 9376
final_rank: 20

code_snippet: #include <bits/stdc++.h>

int main() {
    int si, sj, gi, gj;
    std::cin >> si >> sj >> gi >> gj;
    int n;
    std::cin >> n;
    std::vector<std::pair<int, int>> obs;
    for (int i = 0; i < n; i++) {
        int x, y;
        std::cin >> x >> y;
        obs.emplace_back(x, y);
    }
    int offset_i, offset_j;
    std::cin >> offset_i >> offset_j;
    si += offset_i;
    sj += offset_j;
    gi += offset_i;
    gj += offset_j;
    int h = offset_i * 2 + 1;
    int w = offset_j * 2 + 1;
    std::vector ng(h, std::vector<bool>(w));
    for (auto [i, j]: obs) {
        ng[i + offset_i][j + offset_j] = true;
    }
    const int P = 6;
    const int MAX = 205;
    std::vector already(h, std::vector(w, std::vector(MAX, std::vector<bool>(P))));
    already[si][sj][0][1] = true;
    using Index = std::array<int, 4>;
    std::queue<Index> que;
    que.push({si, sj, 0, 1});

    //   0
    // 5   1
    //   6
    // 4   2
    //   3
    // (i, j)
    const int DI1[] = {0, 1, 1, 0, -1, -1, 0};
    const int DJ1[] = {1, 0, -1, -1, -1, 0, 0};
    const int DI2[] = {0, 1, 1, 0, -1, -1, 0};
    const int DJ2[] = {1, 1, 0, -1, 0, 1, 0};

    auto valid = [&](int i, int j) {
        return 0 <= i && i < h && 0 <= j && j < w;
    };

    auto sug = [&](int i, int j, int p) {
        int k = (i - offset_i) * (j - offset_j);
        return std::abs((k * p) % 6);
    };

    while (!que.empty()) {
        auto [i, j, c, p] = que.front();
        que.pop();
        // if (c > 1) continue;
        // std::cerr << i << ' ' << j << ' ' << c << ' ' << p << std::endl;
        assert(already[i][j][c][p]);
        assert(!ng[i][j]);
        if (c + 1 == MAX) continue;
        const int sister = sug(i, j, p);
        bool flag = ((j - offset_j) % 2 == 0);
        for (int d = 0; d < 7; d++) {
            int ni = i + (flag ? DI1[d] : DI2[d]);
            int nj = j + (flag ? DJ1[d] : DJ2[d]);
            if (!valid(ni, nj)) continue;
            if (ng[ni][nj]) continue;
            int nc = c + (sister != d);
            int np = (p + 1) % P;
            if (!already[ni][nj][nc][np]) {
                already[ni][nj][nc][np] = true;
                que.push({ni, nj, nc, np});
            }
        }
    }
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < 6; j++) {
            if (already[gi][gj][i][j]) {
                std::cout << i << '\n';
                return 0;
            }
        }
    }
    std::cout << -1 << '\n';
}
accuracy: 0.14285714285714285
time_ms: 10
memory_kb: 5508
final_rank: 19

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    int sx = in(), sy = in(), gx = in(), gy = in();
    set<pair<int,int>> ng;
    int n = in();
    for(int i : rep(n)) {
        int x = in(), y = in();
        ng.insert({x, y});
    }
    int lx = in(), ly = in();

    map<tuple<int,int,int>, int> dist;
    deque<tuple<int,int,int>> dq;
    dist[{sx, sy, 0}] = 0;
    dq.push_back({sx, sy, 0});

    vector<int> dx = {0, +1, +1, 0, -1, -1};
    vector<vector<int>> dy = {
        {+1, 0, -1, -1, -1, 0},
        {+1, +1, 0, -1, 0, +1}
    };
    
    while(not dq.empty()) {
        auto u = dq.front(); dq.pop_front();
        auto [x, y, t] = u;
        for(int d : rep(6)) {
            int nx = x + dx[d], ny = y + dy[abs(x) % 2][d];
            if(abs(nx) <= lx and abs(ny) <= ly and !ng.count({nx, ny})) {
                int cost = abs(x * y * t) % 6 != d;
                auto v = make_tuple(nx, ny, (t + 1) % 6);
                if(!dist.count(v) or dist[v] > dist[u] + cost) {
                    dist[v] = dist[u] + cost;
                    if(cost == 0) {
                        dq.push_front(v);
                    } else {
                        dq.push_back(v);
                    }
                }
            }
        }

        int cost = 1;
        auto v = make_tuple(x, y, (t + 1) % 6);
        if(!dist.count(v) or dist[v] > dist[u] + cost) {
            dist[v] = dist[u] + cost;
            dq.push_back(v);
        }
    }

    int ans = 1e9;
    for(int t : rep(6)) if(dist.count({gx, gy, t})) chmin(ans, dist[{gx, gy, t}]);
    print(ans < 1e9 ? ans : -1);
}
accuracy: 1
time_ms: 330
memory_kb: 18196
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

const int inf=1<<30;
const vector<int> dx[]={{0,1,1,0,-1,-1},{0,1,1,0,-1,-1}};
const vector<int> dy[]={{1,0,-1,-1,-1,0},{1,1,0,-1,0,1}};

bool solve(){
    int sx,sy,gx,gy;
    cin>>sx>>sy>>gx>>gy;
    int N;
    cin>>N;
    vector<pair<int,int>>vp;
    for(int i=0;i<N;i++){
        int x,y;
        cin>>x>>y;
        vp.push_back(make_pair(x,y));
    }
    int lx,ly;
    cin>>lx>>ly;
    sx+=lx;
    sy+=ly;
    gx+=lx;
    gy+=ly;
    vector block(2*lx+1,vector<bool>(2*ly+1));
    for(auto[x,y]:vp){
        x+=lx;
        y+=ly;
        block[x][y]=true;
    }
    vector D(2*lx+1,vector(2*ly+1,vector<int>(6,inf)));
    deque<tuple<int,int,int,int>>dq;
    D[sx][sy][0]=0;
    dq.push_back(make_tuple(0,sx,sy,0));
    while(dq.size()){
        auto[c,x,y,t]=dq.front();
        dq.pop_front();
        if(D[x][y][t]<c)continue;
        for(int i=0;i<6;i++){
            int ex=x+dx[(x-lx)%2!=0][i],ey=y+dy[(x-lx)%2!=0][i];
            if(0<=ex&&ex<=2*lx&&0<=ey&&ey<=2*ly&&!block[ex][ey]){
                if(D[ex][ey][(t+1)%6]>c&&i==abs((x-lx)*(y-ly)*t)%6){
                    D[ex][ey][(t+1)%6]=c;
                    dq.push_front(make_tuple(D[ex][ey][(t+1)%6],ex,ey,(t+1)%6));
                }
                if(D[ex][ey][(t+1)%6]>c+1&&i!=abs((x-lx)*(y-ly)*t)%6){
                    D[ex][ey][(t+1)%6]=c+1;
                    dq.push_back(make_tuple(D[ex][ey][(t+1)%6],ex,ey,(t+1)%6));
                }
            }
        }
        if(D[x][y][(t+1)%6]>c+1){
            D[x][y][(t+1)%6]=c+1;
            dq.push_back(make_tuple(D[x][y][(t+1)%6],x,y,(t+1)%6));
        }
    }
    int ans=inf;
    for(int i=0;i<6;i++){
        ans=min(ans,D[gx][gy][i]);
    }
    if(ans==inf)cout<<-1<<"\n";
    else cout<<ans<<"\n";
    return true;
}

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q=1;
    while(Q--){
        if(!solve()){
            return 0;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 5512
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;

const int inf=1<<30;
const vector<int> dx[]={{0,1,1,0,-1,-1},{0,1,1,0,-1,-1}};
const vector<int> dy[]={{1,0,-1,-1,-1,0},{1,1,0,-1,0,1}};

bool solve(){
    int sx,sy,gx,gy;
    cin>>sx>>sy>>gx>>gy;
    int N;
    cin>>N;
    vector<pair<int,int>>vp;
    for(int i=0;i<N;i++){
        int x,y;
        cin>>x>>y;
        vp.push_back(make_pair(x,y));
    }
    int lx,ly;
    cin>>lx>>ly;
    sx+=lx;
    sy+=ly;
    gx+=lx;
    gy+=ly;
    vector block(2*lx+1,vector<bool>(2*ly+1));
    for(auto[x,y]:vp){
        x+=lx;
        y+=ly;
        block[x][y]=true;
    }
    vector D(2*lx+1,vector(2*ly+1,vector<int>(6,inf)));
    deque<tuple<int,int,int,int>>dq;
    D[sx][sy][0]=0;
    dq.push_back(make_tuple(0,sx,sy,0));
    while(dq.size()){
        auto[c,x,y,t]=dq.front();
        dq.pop_front();
        if(D[x][y][t]<c)continue;
        for(int i=0;i<6;i++){
            int ex=x+dx[x%2!=0][i],ey=y+dy[x%2!=0][i];
            if(0<=ex&&ex<=2*lx&&0<=ey&&ey<=2*ly&&!block[ex][ey]){
                if(D[ex][ey][(t+1)%6]>c&&i==abs((x-lx)*(y-ly)*t)%6){
                    D[ex][ey][(t+1)%6]=c;
                    dq.push_front(make_tuple(D[ex][ey][(t+1)%6],ex,ey,(t+1)%6));
                }
                if(D[ex][ey][(t+1)%6]>c+1&&i!=abs((x-lx)*(y-ly)*t)%6){
                    D[ex][ey][(t+1)%6]=c+1;
                    dq.push_back(make_tuple(D[ex][ey][(t+1)%6],ex,ey,(t+1)%6));
                }
            }
        }
        if(D[x][y][(t+1)%6]>c+1){
            D[x][y][(t+1)%6]=c+1;
            dq.push_back(make_tuple(D[x][y][(t+1)%6],x,y,(t+1)%6));
        }
    }
    int ans=inf;
    for(int i=0;i<6;i++){
        ans=min(ans,D[gx][gy][i]);
    }
    if(ans==inf)cout<<-1<<"\n";
    else cout<<ans<<"\n";
    return true;
}

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q=1;
    while(Q--){
        if(!solve()){
            return 0;
        }
    }
}
accuracy: 0.4642857142857143
time_ms: 10
memory_kb: 5828
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128_t lll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef tuple<ll, ll, ll> tt;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pl> vp;
typedef vector<tt> vt;
typedef vector<string> vs;

const ll INF = 1000000010;
const ll INFL = INF * INF;
const double MYPI = acos(-1);
const ld epsilon = 1e-10;

#define ovl(a, b, c, d, e, ...) e
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define DEBUG(...) DEBUG_(#__VA_ARGS__, __VA_ARGS__)
#define REP1(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, l, r) for (int i = (l); i < (r); i++)
#define REP3(i, l, r, d) for (int i = (l); i < (r); i += (d))
#define REP(...) ovl(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)
#define RREP1(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define RREP3(i, l, r, d) for (int i = (r)-1; i >= (l); i -= (d))
#define RREP(...) ovl(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define EACH1(e, a) for (auto &e : a)
#define EACH2(x, y, a) for (auto &[x, y] : a)
#define EACH3(x, y, z, a) for (auto &[x, y, z] : a)
#define EACH(...) ovl(__VA_ARGS__, EACH3, EACH2, EACH1)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define sz(x) (ll) x.size()
#define LB(a, x) (lower_bound(ALL(a), x) - a.begin())
#define UB(a, x) (upper_bound(ALL(a), x) - a.begin())
#define FLG(x, i) (((x) >> (i)) & 1)
#define CNTBIT(x) __builtin_popcountll(x)
#define TOPBIT(t) (t == 0 ? -1 : 63 - __builtin_clzll(t))
#define IN(x, a, b) ((a) <= (x) && (x) < (b))

template <typename T, typename... Ts>
void OUT(const T &a, const Ts &...b) {
    cout << a;
    (cout << ... << (cout << " ", b));
    cout << endl;
}

template <typename T>
void DEBUG_(string_view name, const T &a) {
    cout << name << ": " << a << endl;
}

template <typename T>
void DEBUG_(string_view name, const vector<T> &a) {
    cout << name << ": ";
    REP(i, sz(a)) cout << a[i] << " ";
    cout << endl;
}

template <typename T1, typename T2>
bool chmax(T1 &x, const T2 &y) {
    return x < y ? x = y, 1 : 0;
}
template <typename T1, typename T2>
bool chmin(T1 &x, const T2 &y) {
    return x > y ? x = y, 1 : 0;
}

ll N, lx, ly;

int toIdx(int t, int x, int y) {
    assert(0 <= t && t < 6);
    assert(0 <= x && x < lx);
    assert(0 <= y && y < ly);
    return t * lx * ly + y * lx + x;
}

int DX[6] = {0, 1, 1, 0, -1, -1};
int DY(int d, int x) {
    int DY1[6] = {1, 0, -1, -1, -1, 0};
    int DY2[6] = {1, 1, 0, -1, 0, 1};
    if ((x - lx / 2) % 2 == 0) {
        return DY1[d];
    } else {
        return DY2[d];
    }
}

// Ο((E+V)logV)  cannot reach: INFll
vector<long long> dijkstra(const vector<vector<pair<int, long long>>> &adj, int s) {
    vector<long long> dist(int(adj.size()), INFL);
    priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                   greater<pair<long long, int>>>
        que;
    dist[s] = 0;
    que.emplace(dist[s], s);
    while (!que.empty()) {
        pair<long long, int> p = que.top();
        que.pop();
        if (dist[p.second] < p.first) continue;
        for (pair<int, long long> nv : adj[p.second]) {
            if (dist[nv.first] > dist[p.second] + nv.second) {
                dist[nv.first] = dist[p.second] + nv.second;
                que.emplace(dist[nv.first], nv.first);
            }
        }
    }
    return dist;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;

    cin >> N;

    vp _XYs(N);
    REP(i, N) {
        ll x, y;
        cin >> x >> y;
        _XYs[i] = {x, y};
    }

    cin >> lx >> ly;

    sx += lx;
    sy += ly;
    gx += lx;
    gy += ly;
    REP(i, N) { _XYs[i] = {_XYs[i].first + lx, _XYs[i].second + ly}; }
    set<pl> XYs(ALL(_XYs));

    lx *= 2;
    ly *= 2;
    lx += 1;
    ly += 1;

    vector<vector<pair<int, long long>>> adj(6 * lx * ly + 2);

    REP(t, 6) {
        REP(x, lx) {
            REP(y, ly) {
                if (XYs.find({x, y}) != XYs.end()) continue;
                int v = toIdx(t, x, y);
                REP(d, 6) {
                    int nt = (t + 1) % 6;
                    int nx = x + DX[d];
                    int ny = y + DY(d, x);
                    if (!IN(nx, 0, lx) || !IN(ny, 0, ly)) continue;
                    if (XYs.find({nx, ny}) != XYs.end()) continue;
                    int nv = toIdx(nt, nx, ny);
                    if (abs(t * (x - lx / 2) * (y - ly / 2)) % 6 == d)
                        adj[v].emplace_back(nv, 0);
                    else
                        adj[v].emplace_back(nv, 1);
                }
                int nv_same_place = toIdx((t + 1) % 6, x, y);
                adj[v].emplace_back(nv_same_place, 1);
            }
        }
    }

    adj[6 * lx * ly].emplace_back(toIdx(0, sx, sy), 0);
    REP(t, 6) { adj[toIdx(t, gx, gy)].emplace_back(6 * lx * ly + 1, 0); }

    auto dists = dijkstra(adj, 6 * lx * ly);
    OUT(dists[6 * lx * ly + 1] == INFL ? -1 : dists[6 * lx * ly + 1]);

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 44056
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
#include <unordered_set>
#include <random>
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=INF;
const ll inf=INF*INF;
const ll mod=MOD;
const ll inv2=499122177;
const ll MAX=10000010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;

//ll dx[]={0,1,0,-1,-1,-1,1,1};
//ll dy[]={1,0,-1,0,-1,1,-1,1};

int Dx[]={0,1,1,0,-1,-1};
int Dy[]={1,0,-1,-1,-1,0};



void solve(){
    int sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;
    vector<vector<int> >dx(2,vector<int>(6)),dy(2,vector<int>(6));
    rep(i,6)dx[0][i]=Dx[i],dy[0][i]=Dy[i];
    dx[1]=dx[0],dy[1]=dy[0];
    dy[1][1]++,dy[1][2]++;
    dy[1][4]++,dy[1][5]++;
    //01-BFS で解く
    int n;
    cin >> n;
    set<pair<int,int> >st;
    rep(i,n){
        int x,y;cin>>x>>y;
        st.insert(mp(x,y));
    }
    int lx,ly;
    cin >> lx >> ly;
    map<pair<pair<int,int>,int>, int>idx;

    int now = 0;
    vector<int>x(0),y(0),t(0);
    REP(i,-lx,lx+1){
        REP(j,-ly,ly+1){
            rep(k,6){
                idx[mp(mp(i,j),k)]=now;
                x.pb(i);
                y.pb(j);
                t.pb(k);
                now++;
            }
        }
    }


    vector<int>d(now,INF);
    deque<pair<int,int> > dq;
    int s=idx[mp(mp(sx,sy),0)];
    d[s]=0;
    dq.push_front(mp(s,0));
    while(!dq.empty()){
        pair<int,int> k = dq.front();
        dq.pop_front();
        if(d[k.fi]<k.se)continue;
        int X=x[k.fi],Y=y[k.fi],T=t[k.fi];
        int dir=abs(X*Y*T)%6;
        int nt=(T+1)%6;
        //cout<<X<<' '<<Y<<' '<<T<<' '<<d[k.fi]<<endl;
        rep(l,6){
            if(l==dir)continue;
            int nx=X+dx[abs(X)%2][l],ny=Y+dy[abs(X)%2][l];
            if(abs(nx)>lx||abs(ny)>ly||st.find(mp(nx,ny))!=st.end())continue;
            int nid=idx[mp(mp(nx,ny),nt)];
            if(d[nid]>d[k.fi]+1){
                d[nid]=d[k.fi]+1;
                dq.push_back(mp(nid,d[nid]));
            }
        }
        int nid=idx[mp(mp(X,Y),nt)];
        if(d[nid]>d[k.fi]+1){
            d[nid]=d[k.fi]+1;
            dq.push_back(mp(nid,d[nid]));
        }
        int nx=X+dx[abs(X)%2][dir],ny=Y+dy[abs(X)%2][dir];
        if(abs(nx)>lx||abs(ny)>ly||st.find(mp(nx,ny))!=st.end())continue;
        nid=idx[mp(mp(nx,ny),nt)];
        if(d[nid]>d[k.fi]){
            d[nid]=d[k.fi];
            dq.push_front(mp(nid,d[nid]));
        }
    }
    int ans=INF;
    rep(i,6){
        int gid=idx[mp(mp(gx,gy),i)];
        ans=min(ans,d[gid]);
    }
    if(ans==INF)ans=-1;
    cout<<ans<<endl;
}

signed main(){
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    solve();
}
accuracy: 1
time_ms: 240
memory_kb: 22220
final_rank: 9

code_snippet: #include <stdio.h>
#include <stdlib.h>
#include <deque>
#include <tuple>
#define Z 128
#define INF 1012345678
using namespace std;

int block[256][256], dist[256][256][6];
deque<tuple<int, int, int>> dq;
const int dxy[7][2] = { {0, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {0, 0} };
int lx, ly;

int isvalid(int x, int y, int t) {
    if (x < -lx || y < -ly || x > lx || y > ly) return 0;
    if (block[x + Z][y + Z]) return 0;
    return 1;
}

int main(void) {
    int sx, sy, gx, gy, n, x, y, t, nx, ny, nt, nd, md, res = INF;
    scanf("%d %d %d %d", &sx, &sy, &gx, &gy);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &x, &y);
        block[x + Z][y + Z] = 1;
    }
    scanf("%d %d", &lx, &ly);
    for (int i = 0; i < 256; i++) {
        for (int j = 0; j < 256; j++) {
            for (int k = 0; k < 6; k++) dist[i][j][k] = INF;
        }
    }
    dq.push_back({ sx, sy, 0 });
    dist[sx + Z][sy + Z][0] = 0;

    while (dq.size()) {
        x = get<0>(dq.front());
        y = get<1>(dq.front());
        t = get<2>(dq.front());
        dq.pop_front();
        nt = t + 1;
        if (nt == 6) nt = 0;
        md = abs(x * y * t) % 6;

        for (int i = 0; i < 7; i++) {
            nx = x + dxy[i][0];
            ny = y + dxy[i][1];
            if (i != 0 && i != 3 && i != 6 && (x & 1)) ny++;
            if (!isvalid(nx, ny, nt)) continue;
            nd = dist[x + Z][y + Z][t];
            if (md != i) nd++;
            if (nd >= dist[nx + Z][ny + Z][nt]) continue;

            if (md == i) dq.push_front({ nx, ny, nt });
            else dq.push_back({ nx, ny, nt });
            dist[nx + Z][ny + Z][nt] = nd;
        }
    }
    for (int i = 0; i < 6; i++) {
        if (dist[gx + Z][gy + Z][i] < res) res = dist[gx + Z][gy + Z][i];
    }
    printf("%d\n", res == INF ? -1 : res);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4660
final_rank: 2