Problem:
Problem E:
Dragon's Cruller
Dragon's Cruller is a sliding puzzle on a torus. The torus surface is partitioned into nine squares as shown in its development in Figure E.1. Here, two squares with one of the sides on the development labeled with the same letter are adjacent to each other, actually sharing the side. Figure E.2 shows which squares are adjacent to which and in which way. Pieces numbered from 1 through 8 are placed on eight out of the nine squares, leaving the remaining one empty.
Figure E.1. A 3 × 3 Dragon’s Cruller torus
A piece can be slid to an empty square from one of its adjacent squares. The goal of the puzzle is, by sliding pieces a number of times, to reposition the pieces from the given starting arrangement into the given goal arrangement. Figure E.3 illustrates arrangements directly reached from the arrangement shown in the center after four possible slides. The cost to slide a piece depends on the direction but is independent of the position nor the piece.
Your mission is to find the minimum cost required to reposition the pieces from the given starting
arrangement into the given goal arrangement.
Figure E.2. Adjacency
Figure E.3. Examples of sliding steps
Unlike some sliding puzzles on a flat square, it is known that any goal arrangement can be reached from any starting arrangements on this torus.
Input
The input is a sequence of at most 30 datasets.
A dataset consists of seven lines. The first line contains two positive integers
c
h
and
c
v
, which represent the respective costs to move a piece horizontally and vertically. You can assume that
both
c
h
and
c
v
are less than 100. The next three lines specify the starting arrangement and the last three the goal arrangement, each in the following format.
d
A
d
B
d
C
d
D
d
E
d
F
d
G
d
H
d
I
Each line consists of three digits separated by a space. The digit
d
X
(
X
is one of
A
through
I
) indicates the state of the square
X
as shown in Figure E.2. Digits
1
, . . . ,
8
indicate that the piece of that number is on the square. The digit
0
indicates that the square is empty.
The end of the input is indicated by two zeros separated by a space.
Output
For each dataset, output the minimum total cost to achieve the goal, in a line. The total cost is the sum of the costs of moves from the starting arrangement to the goal arrangement. No other characters should appear in the output.
Sample Input
4 9
6 3 0
8 1 2
4 5 7
6 3 0
8 1 2
4 5 7
31 31
4 3 6
0 1 5
8 2 7
0 3 6
4 1 5
8 2 7
92 4
1 5 3
4 0 7
8 2 6
1 5 0
4 7 3
8 2 6
12 28
3 4 5
0 2 6
7 1 8
5 7 1
8 6 2
0 3 4
0 0
Output for the Sample Input
0
31
96
312

Submitted solutions:

code_snippet: #include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<algorithm>
typedef long long LL;
#define HASH 1000003
#define MAXN 1000010
int first[10], e, next[40], v[40], w[40];
struct HN {
	LL s;
	int d;
	HN() {}
	HN(LL s, int d): s(s), d(d) {}
	bool operator < (const HN &t) const {
		return d > t.d;
	}
};
struct HM {
	int first[HASH], e, next[MAXN], d[MAXN];
	LL s[MAXN];
	void init() {
		memset(first, -1, sizeof(first)), e = 0;
	}
	int hash(LL v) {
		return v % HASH;
	}
	int find(LL v) {
		int h = hash(v);
		for(int i = first[h]; i != -1; i = next[i]) {
			if(s[i] == v) {
				return i;
			}
		}
		return -1;
	}
	void add(LL v, int dis) {
		int h = hash(v);
		s[e] = v, d[e] = dis;
		next[e] = first[h], first[h] = e ++;
	}
}hm;
void add(int x, int y, int z) {
	v[e] = y, w[e] = z;
	next[e] = first[x], first[x] = e ++;
}
void init() {
	memset(first, -1, sizeof(first)), e = 0;
	for(int i = 0; i < 9; i ++) {
		add(i, (i + 1) % 9, 0), add(i, (i - 1 + 9) % 9, 0);
		add(i, (i + 3) % 9, 1), add(i, (i - 3 + 9) % 9, 1);
	}
}
LL encode(int a[]) {
	LL ans = 0;
	for(int i = 0; i < 9; i ++) {
		ans = ans << 4 | a[i];
	}
	return ans;
}
void decode(int a[], LL s) {
	for(int i = 8; i >= 0; i --) {
		a[i] = s & 15;
		s >>= 4;
	}
}
int CH, CV, bm[10], em[10], a[10];
LL BM, EM;
void process() {
	hm.init();
	std::priority_queue<HN> q;
	hm.add(BM, 0);
	q.push(HN(BM, 0));
	while(!q.empty()) {
		HN hn = q.top();
		q.pop();
		if(hn.s == EM) {
			printf("%d\n", hn.d);
			break;
		}
		int k = hm.find(hn.s);
		if(hn.d > hm.d[k]) {
			continue;
		}
		decode(a, hn.s);
		for(int j = 0; j < 9; j ++) {
			if(a[j] == 0) {
				for(int i = first[j]; i != -1; i = next[i]) {
					int jj = v[i], dd = hn.d + (w[i] == 0 ? CH : CV);
					std::swap(a[j], a[jj]);
					LL v = encode(a);
					k = hm.find(v);
					if(k == -1) {
						hm.add(v, dd);
						q.push(HN(v, dd));
					} else if(dd < hm.d[k]) {
						hm.d[k] = dd;
						q.push(HN(v, dd));
					}
					std::swap(a[j], a[jj]);
				}
				break;
			}
		}
	}
}
int main() {
	//freopen("test.in", "rb", stdin);
	init();
	while(scanf("%d%d", &CH, &CV), CH) {
		for(int i = 0; i < 9; i ++) {
			scanf("%d", &bm[i]);
		}
		for(int i = 0; i < 9; i ++) {
			scanf("%d", &em[i]);
		}
		BM = encode(bm), EM = encode(em);
		process();
	}
	return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 24024
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()

template <typename T>
bool chmin(T& a, T b) {
    if (a <= b) return 0;
    a = b;
    return 1;
}

using Grid = array<int,9>;

const vector<vector<int>> edges_h = {
    {1, 8},
    {2, 0},
    {3, 1},
    {4, 2},
    {5, 3},
    {6, 4},
    {7, 5},
    {8, 6},
    {0, 7}
};

const vector<vector<int>> edges_v = {
    {6, 3},
    {7, 4},
    {8, 5},
    {0, 6},
    {1, 7},
    {2, 8},
    {3, 0},
    {4, 1},
    {5, 2}
};

int main() {
    while (true) {
        int ch, cv;
        cin >> ch >> cv;

        if (ch == 0 && cv == 0) {
            return 0;
        }
        Grid initial_grid;
        rep(i, 0, 9) {
            cin >> initial_grid[i];
        }
        Grid goal_grid;
        rep(i, 0, 9) {
            cin >> goal_grid[i];
        }
        map<Grid,int> costs;
        costs[initial_grid] = 0;
        priority_queue<pair<int,Grid>> todo;
        todo.push({0, initial_grid});
        while (true) {
            auto [cost, s] = todo.top();
            todo.pop();
            cost *= -1;
            if (costs[s] < cost) {
                continue;
            }
            if (s == goal_grid) {
                cout << cost << "\n";
                break;
            }
            int p = distance(s.begin(), find(s.begin(), s.end(), 0));
            for (int q : edges_h[p]) {
                swap(s[p], s[q]);
                int c = cost + ch;
                if (costs.count(s) == 0) {
                    costs[s] = c;
                    todo.push({-c, s});
                } else if (chmin(costs[s], c)) {
                    todo.push({-c, s});
                }
                swap(s[p], s[q]);
            }
            for (int q : edges_v[p]) {
                swap(s[p], s[q]);
                int c = cost + cv;
                if (costs.count(s) == 0) {
                    costs[s] = c;
                    todo.push({-c, s});
                } else if (chmin(costs[s], c)) {
                    todo.push({-c, s});
                }
                swap(s[p], s[q]);
            }
        }
    }
}
accuracy: 1
time_ms: 5040
memory_kb: 36628
final_rank: 16

code_snippet: #include "bits/stdc++.h"

using namespace std;

using ll=long long;

bool solve();

map<vector<int>,int>mp;
vector<vector<pair<ll,ll>>>adj;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n=9;
    vector<int>ord(n);
    std::iota(ord.begin(), ord.end(),0);
    std::sort(ord.begin(), ord.end());
    int now=0;
    do{
        mp[ord]=now++;
    }while(next_permutation(ord.begin(),ord.end()));
    std::sort(ord.begin(), ord.end());
    adj.resize(now);
    do{
        int at=mp[ord];
        for (int i = 0; i < n; ++i) {
            if(ord[i]==0){
                for(auto dy:{-1,1}){
                    auto i2=(i+dy+n)%n;
                    swap(ord[i],ord[i2]);
                    adj[at].emplace_back(mp[ord],0);
                    swap(ord[i],ord[i2]);
                }
                for(auto dx:{-3,3}){
                    auto i2=(i+dx+n)%n;
                    swap(ord[i],ord[i2]);
                    adj[at].emplace_back(mp[ord],1);
                    swap(ord[i],ord[i2]);
                }
            }
        }
    }while(next_permutation(ord.begin(),ord.end()));
    while(1){
        int cw,ch;cin>>cw>>ch;
        if(cw==0 and ch==0){
            return 0;
        }
        int n=9;
        vector<int>d(n);
        for (int i = 0; i < n; ++i) {
            cin>>d[i];
        }
        vector<int>tar(n);
        for (int i = 0; i < n; ++i) {
            cin>>tar[i];
        }
        auto target=mp[tar];
        const ll INF=1e15;
        vector<ll>dist(now,INF);
        using P=pair<ll,ll>;
        priority_queue<P,vector<P>,greater<P>>q;
        q.emplace(0,mp[d]);
        dist[mp[d]]=0;
        while(q.size()){
            auto [dd,v]=q.top();q.pop();
            if(dd!=dist[v])continue;
            if(v==target){
                cout<<dd<<"\n";
                break;
            }
            for(auto [to,ty]:adj[v]){
                ll cost=(ty==0)?cw:ch;
                if(dist[to]>dd+cost){
                    dist[to]=dd+cost;
                    q.emplace(dist[to],to);
                }
            }
        }
    }
    return 0;
}

bool solve(){
    int cw,ch;cin>>cw>>ch;
    if(cw==0 and ch==0){
        return false;
    }
    int n=9;
    vector<int>d(n);
    for (int i = 0; i < n; ++i) {
        cin>>d[i];
    }
    vector<int>tar(n);
    for (int i = 0; i < n; ++i) {
        cin>>tar[i];
    }
    auto target=mp[tar];
    vector<ll>dist(adj.size());
    using P=pair<ll,ll>;
    priority_queue<P,vector<P>,greater<P>>q;
    q.emplace(0,mp[d]);
    while(q.size()){
        auto [dd,v]=q.top();q.pop();
        if(dd!=dist[v])continue;
        if(v==target){
//            cerr<<dd<<"\n";
            cout<<dd<<"\n";
            return true;
        }
        for(auto [to,ty]:adj[v]){
            ll cost=(ty==0)?cw:ch;
            if(dist[to]>dd+cost){
                dist[to]=dd+cost;
                q.emplace(dist[to],to);
            }
        }
    }
    return true;
}
accuracy: 1
time_ms: 1010
memory_kb: 94752
final_rank: 19

code_snippet: #include "bits/stdc++.h"

using namespace std;

using ll=long long;

bool solve(){
    int cw,ch;cin>>cw>>ch;
    if(cw==0 and ch==0){
        return false;
    }
    int n=9;
    vector<int>d(n);
    for (int i = 0; i < n; ++i) {
        cin>>d[i];
    }
    vector<int>tar(n);
    for (int i = 0; i < n; ++i) {
        cin>>tar[i];
    }
    map<vector<int>,int>mp;
    using P=pair<ll,vector<int>>;
    priority_queue<P,vector<P>,greater<P>>q;
    q.emplace(0,d);
    auto add=[&](vector<int>&v,ll dd){
        if(!mp.count(v)){
            mp[v]=dd;
            q.emplace(dd,v);
        }
        else{
            if(mp[v]>dd){
                mp[v]=dd;
                q.emplace(dd,v);
            }
        }
    };
    while(q.size()){
        auto [dd,v]=q.top();q.pop();
        if(mp.count(v) and dd!=mp[v])continue;
        if(v==tar){
//            cerr<<dd<<"\n";
            cout<<dd<<"\n";
            return true;
        }
        for (int i = 0; i < n; ++i) {
            if(v[i]==0){
                for(auto dy:{-1,1}){
                    auto i2=(i+dy+n)%n;
                    swap(v[i],v[i2]);
                    add(v,dd+cw);
                    swap(v[i],v[i2]);
                }
                for(auto dx:{-3,3}){
                    auto i2=(i+dx+n)%n;
                    swap(v[i],v[i2]);
                    add(v,dd+ch);
                    swap(v[i],v[i2]);
                }
            }
        }
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(solve());
    return 0;
}
accuracy: 1
time_ms: 7660
memory_kb: 58176
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
int v[9][2];
int h[9][2];
ll ch, cv;
template <typename T>
bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
map<ll, ll> mp;
void print(vector<int> v) {
    for(int i = 0; i < 9; i++) {
        cerr << v[i] << " ";
        if(i % 3 == 2) cerr << endl;
    }
}

ll f(vector<int> v) {
    ll ret = 0;
    for(int i = v.size() - 1; i >= 0; i--) {
        ret *= 10;
        ret += v[i];
    }
    return ret;
}

vector<int> g(ll a) {
    vector<int> ret;
    for(int i = 0; i < 9; i++) {
        ret.push_back(a % 10);
        a /= 10;
    }
    return ret;
}

typedef pair<ll, vector<int>> lll;
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    for(int i = 0; i <= 8; i++) {
        h[i][0] = (i + 1) % 9;
        h[i][1] = (i + 8) % 9;
        v[i][0] = (i + 3) % 9;
        v[i][1] = (i + 6) % 9;
    }
    while(true) {
        cin >> ch >> cv;
        if(ch == 0) break;
        vector<int> start(9);
        vector<int> goal(9);
        for(int i = 0; i < 9; i++) cin >> start[i];
        for(int i = 0; i < 9; i++) cin >> goal[i];
        mp.clear();
        priority_queue<l_l, vector<l_l>, greater<l_l>> que;
        que.push({0, f(start)});
        while(true) {
            ll nowcost = que.top().first;
            ll nowval = que.top().second;
            vector<int> now = g(que.top().second);
            que.pop();
            if(now == goal) {
                cout << nowcost << endl;
                break;
            }
            int zeroindex;
            for(int i = 0; i < 9; i++) {
                if(now[i] == 0) zeroindex = i;
            }
            for(int i = 0; i < 2; i++) {
                ll newcost = nowcost + ch;
                vector<int> to = now;
                swap(to[zeroindex], to[h[zeroindex][i]]);
                auto a = f(to);
                if(mp.count(a) == 0 || mp[a] > newcost) {
                    mp[a] = newcost;
                    que.push({newcost, a});
                }
                /*
                cerr << "---------------" << endl;
                print(now);
                cerr << "to(horizon)" << endl;
                print(to);
                 */
            }
            for(int i = 0; i < 2; i++) {
                ll newcost = nowcost + cv;
                vector<int> to = now;
                swap(to[zeroindex], to[v[zeroindex][i]]);
                auto a = f(to);
                if(mp.count(a) == 0 || chmin(mp[a], newcost)) {
                    mp[a] = newcost;
                    que.push({newcost, a});
                }
                /*
                cerr << "---------------" << endl;
                print(now);
                cerr << "to(vertical)" << endl;
                print(to);
                 */
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 4730
memory_kb: 27452
final_rank: 15

code_snippet: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <cstring>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;

#define SIZE 400
#define LLINF 1000000000000000000LL

int swapH[][2] = {{0, 1}, {0, 8}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8} };

int swapV[][2] = {{0, 6}, {0, 3}, {1, 4}, {1, 7}, {2, 5}, {2, 8}, {3, 6}, {4, 7}, {5, 8} };

int b[9], g[9];
int gHash;

void toArray(int *b, int hash) {
  for (int i=8; i>=0; i--) {
    b[i] = hash % 9;
    hash /= 9;
  }
}

int toHash(int *b) {
  int res = 0;
  for (int i=0; i<9; i++)
    res = res * 9 + b[i];

  return res;
}

int H, V;

int base9[9];

int swapp(int hash, int a, int b) {
  int p = hash / base9[a] % 9;
  int q = hash / base9[b] % 9;

  return hash + (p - q) * base9[b] + (q - p) * base9[a];
}

int isOK(int hash, int a, int b) {
  int r = hash / base9[a] % 9;
  int s = hash / base9[b] % 9;
  return r == 0 || s == 0;
}

ll dfs(ll cost = 0) {
  int hash = toHash(b);

  priority_queue<pair<ll,int> > pq;
  set<int> memo;

  pq.push({0, hash});

  while(pq.size()) {
    auto p = pq.top();
    pq.pop();

    int hash = p.second;
    ll cost = -p.first;

    if (memo.find(hash) != memo.end()) continue;
    memo.insert(hash);

    if (hash == gHash) {
      return cost;
    }

    for (int i=0; i<9; i++) {
      if (isOK(hash, swapH[i][0], swapH[i][1]))
        pq.push({-cost-H, swapp(hash, swapH[i][0], swapH[i][1])});
    }

    for (int i=0; i<9; i++) {
      if (isOK(hash, swapV[i][0], swapV[i][1]))
        pq.push({-cost-V, swapp(hash, swapV[i][0], swapV[i][1])});
    }
  }

  return LLINF;
}

bool solve() {

  scanf("%d%d", &H, &V);

  if (!H) return false;

  for (int i=0; i<9; i++)
    scanf("%d", b+i);

  for (int i=0; i<9; i++)
    scanf("%d", g+i);

  gHash = toHash(g);

  ll ans = dfs();

  assert(ans < LLINF);

  printf("%lld\n", ans);

  return true;
}

int main() {

  base9[8] = 1;

  for (int i=7;i >=0; i--)
    base9[i] = base9[i+1] * 9;

  while(solve());
  return 0;
}
accuracy: 1
time_ms: 2850
memory_kb: 25808
final_rank: 14

code_snippet: /*
 * Author: Gatevin
 * Created Time:  2015/10/31 13:46:57
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
 
int a[10];
int ch, cv;
int st, ed;
int fac[10];
int b[10], c[10];
 
#define maxn (1*2*3*4*5*6*7*8*9+10)
 
struct Edge
{
    int to, nex, w, typ;
    Edge(){}
    Edge(int _to, int _nex, int _w, int _typ)
    {
        to = _to, nex = _nex, w = _w, typ = _typ;
    }
};
Edge edge[maxn*4*2 + 2];
 
int head[maxn];
int E;
 
void add_Edge(int u, int v, int w, int typ)
{
    edge[++E] = Edge(v, head[u], w, typ);
    head[u] = E;
}
 
int change[9][4] =//转移
{
    {1, 8, 6, 3},
    {2, 0, 7, 4},
    {3, 1, 8, 5},
    {4, 2, 0, 6},
    {5, 3, 1, 7},
    {6, 4, 2, 8},
    {7, 5, 3, 0},
    {8, 6, 4, 1},
    {0, 7, 5, 2}
};
 
void build()
{
    ch = cv = 0;
    memset(head, -1, sizeof(head));
    E = 0;
    int start = 0;
    int end = 0;
    for(int i = 0; i <= 8; i++)
        end += i*fac[i];
    for(int i = start; i <= end; i++)//解Cantor展开
    {
        int ti = i;
        int emp;
        memset(a, 0, sizeof(a));
        for(int j = 8; j >= 0; j--)
        {
            int cnt = ti / fac[j];
            for(int p = 0; p <= 8; p++)
            {
                //if(a[i] == 0) cnt--;
                if(cnt == 0 && a[p] == 0)
                {
                    b[j] = p;
                    a[p] = 1;
                    break;
                }
                else if(a[p] == 0) cnt--;
            }
            ti %= fac[j];
            if(b[j] == 0) emp = j;
        }
        for(int k = 0; k < 4; k++)//计算目标状态的Cantor展开的值
        {
            int swa = change[emp][k];
            for(int g = 0; g <= 8; g++)
                c[g] = b[g];
            swap(c[emp], c[swa]);
            int to = 0;
            for(int g = 0; g <= 8; g++)
            {
                int cnt = 0;
                for(int mabi = 0; mabi < g; mabi++)
                    if(c[mabi] < c[g]) cnt++;
                to += cnt*fac[g];
            }
            add_Edge(to, i, (k > 1) ? cv : ch, (k > 1));
        }
    }
}
 
int Cantor()
{
    int ret = 0;
    for(int i = 0; i < 9; i++)
    {
        int cnt = 0;
        for(int j = 0; j < i; j++)
            if(a[i] > a[j]) cnt++;
        ret += cnt*fac[i];
    }
    return ret;
}
 
bool vis[maxn];
int dis[maxn];
const int inf = 1e9;
 
void solve()//spfa
{
    memset(vis, 0, sizeof(vis));
    fill(dis, dis + fac[9] + 1, inf);
    dis[st] = 0;
    queue<int> Q;
    Q.push(st);
    vis[st] = 1;
    while(!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        vis[now] = 0;
        for(int i = head[now]; i + 1; i = edge[i].nex)
        {
            int nex = edge[i].to;
            if(dis[nex] > dis[now] + edge[i].w)
            {
                dis[nex] = dis[now] + edge[i].w;
                if(!vis[nex])
                    Q.push(nex), vis[nex] = 1;
            }
        }
    }
    printf("%d\n", dis[ed]);
}
 
int main()
{
    fac[0] = fac[1] = 1;
    for(int i = 2; i < 10; i++) fac[i] = fac[i - 1]*i;
    build();
    while(scanf("%d %d", &ch, &cv), ch || cv)
    {
        for(int i = 0; i < 9; i++)
            scanf("%d", &a[i]);
        st = Cantor();
        for(int i = 0; i < 9; i++)
            scanf("%d", &a[i]);
        ed = Cantor();
        //build();
        for(int i = 1; i <= E; i++)
            if(edge[i].typ == 1)
                edge[i].w = cv;
            else edge[i].w = ch;
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 1990
memory_kb: 29248
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    const int xx=2647079;
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(xx,false);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%xx]){
                mp[num%xx]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 2870
memory_kb: 14580
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    const int xx=3029561;
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(xx,false);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%xx]){
                mp[num%xx]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 2910
memory_kb: 15008
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    const int xx=4573769;
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(xx,false);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%xx]){
                mp[num%xx]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%xx])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 3120
memory_kb: 15716
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(3000000);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%2966911]){
                mp[num%2966911]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%2966911])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%2966911])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%2966911])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%2966911])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 2890
memory_kb: 14576
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(5000000);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%4853333]){
                mp[num%4853333]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 3000
memory_kb: 15400
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(5000000,false);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%4853333]){
                mp[num%4853333]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
             
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 3000
memory_kb: 15372
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
constexpr int pDec[]={1,10,100,1000,10000,100000,1000000,10000000,100000000};
constexpr int fct[]={40320,5040,720,120,24,6,2,1};
constexpr int dif[]={-1,1,-3,3};
int makeHash(int num){
    int ans=0;
    int f=0;
    for(int i=0;i<8;i++){
        int tmp=num%10;
        // auto index=lower_bound(vDigit.begin(),vDigit.end(),tmp);
        // ans+=(index-vDigit.begin())*fct[i];
        //vDigit.erase(index);
        
        ans+=(tmp-__builtin_popcount(f&(1<<tmp)-1))*fct[i];
        f|=(1<<tmp);
        num/=10;
    }
    return ans;
}

int main(void){
    // cout<<makeHash(305147826)<<endl;
    // return 0;
    vector<int> vDigit{0,1,2,3,4,5,6,7,8};
    
    // int count=0;
    // do{
    //     int num=0;
    //     for(int i=9;i>0;i--){
    //         num+=pDec[i-1]*vDigit[9-i];
    //     }
    //     mp.emplace(num,count++);
    // }while(next_permutation(vDigit.begin(),vDigit.end()));
    // Your code here!
    int h,v;
    while(cin>>h>>v,h||v){
        int hv[]={h,h,v,v};
        int st=0;
        int end=0;
        int a;
        vector<bool> visited(362880,false);
        cin>>st;
        for(int i=0;i<8;i++){
            st*=10;
            cin>>a;
            st+=a;
        }
        cin>>end;
        for(int i=0;i<8;i++){
            end*=10;
            cin>>a;
            end+=a;
        }
        priority_queue<pair<int,int>> q;
        q.emplace(0,st);
        while(1){
            int pos=q.top().second;
            int cost=q.top().first;
            if(pos==end){
                cout<<-cost<<endl;
                break;
            }
            q.pop();
            int hash=makeHash(pos);
            if(visited[hash]){
                continue;
            }
            visited[hash]=true;
            int zero=to_string(pos+1000000000).find('0')-1;
            for(int i=0;i<4;i++){
                int s=pos,t=8-((zero+dif[i]+9)%9);
                int tmp=(pos/pDec[t])%10;
                s+=(pDec[8-zero]*tmp-pDec[t]*tmp);
                //cout<<tmp<<" "<<pos<<" "<<s<<endl;
                q.emplace(cost-hv[i],s);
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 1990
memory_kb: 10132
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        vector<bool> mp(5000000,false);
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if(!mp[num%4853333]){
                mp[num%4853333]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if(!mp[stoi(ns)%4853333])
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 3050
memory_kb: 15428
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define fs first
#define sc second
constexpr int tv[]={-3,-1,1,3};
constexpr int p10[]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
constexpr int fact[]={40320,5040,720,120,24,6,2,1};

int my_hash(int f){
    int flag=0,ret=0;
    rep(i,8){
        ret+=fact[i]*(f%10-__builtin_popcount(flag&((1<<(f%10))-1)));
        flag|=1<<(f%10);
        f/=10;
    }
    return ret;
}


int main(){
    
    int h,v;
    string a="123456789",b="123456789";
    using data=pair<int,int>;
    
    while(cin>>h>>v, h or v){
        vector<int> hm(362880,-10000000);
        int tc[]={v,h,h,v};
        rep(i,9)cin>>a[i];
        rep(i,9)cin>>b[i];
        int sa=stoi("1"+a)-1000000000,sb=stoi("1"+b)-1000000000;

        priority_queue<data> q;
        q.emplace(0,sa);
        
        while(1){
            int cost=q.top().fs;
            int board=q.top().sc;
            if(board==sb){
                cout<<-cost<<endl;
                break;
            }
            q.pop();
            int index=to_string(board).find('0');
            if(index==-1)index=0;
            rep(i,4){
                int fixed=(index+tv[i]+9)%9;
                int next=board;
                int fx=board/p10[fixed]%10;
                next=next+(p10[index]-p10[fixed]) * fx;
                int nc=cost-tc[i];
                int hval=my_hash(next);
                if(hm[hval]<nc){
                    q.emplace(nc, next);
                    hm[hval]=nc;
                }
            }
        }
    }
}
accuracy: 1
time_ms: 1400
memory_kb: 7428
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define fs first
#define sc second
constexpr int tv[]={-3,-1,1,3};
constexpr int p10[]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
constexpr int fact[]={40320,5040,720,120,24,6,2,1};

int my_hash(int f){
    vector<int> v={0,1,2,3,4,5,6,7,8};
    int ret=0;
    rep(i,8){
        auto itr=lower_bound(v.begin(),v.end(),f%10);
        ret+=fact[i]*(itr-v.begin());
        v.erase(itr);
        f/=10;
    }
    return ret;
}


int main(){
    
    int h,v;
    string a="123456789",b="123456789";
    using data=pair<int,int>;
    
    while(cin>>h>>v, h or v){
        vector<int> hm(362880,-10000000);
        int tc[]={v,h,h,v};
        rep(i,9)cin>>a[i];
        rep(i,9)cin>>b[i];
        int sa=stoi("1"+a)-1000000000,sb=stoi("1"+b)-1000000000;

        priority_queue<data> q;
        q.emplace(0,sa);
        
        while(1){
            int cost=q.top().fs;
            int board=q.top().sc;
            if(board==sb){
                cout<<-cost<<endl;
                break;
            }
            q.pop();
            int index=to_string(board).find('0');
            if(index==-1)index=0;
            rep(i,4){
                int fixed=(index+tv[i]+9)%9;
                int next=board;
                int fx=board/p10[fixed]%10;
                next=next+(p10[index]-p10[fixed]) * fx;
                int nc=cost-tc[i];
                int hval=my_hash(next);
                if(hm[hval]<nc){
                    q.emplace(nc, next);
                    hm[hval]=nc;
                }
            }
        }
    }
}
accuracy: 1
time_ms: 2310
memory_kb: 7384
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        unordered_map<int,bool> mp;
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if((mp.find(num)==mp.end())){
                mp[num]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-v,ns));
        }
    }
}
accuracy: 1
time_ms: 5900
memory_kb: 29756
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,v,i;
    while(cin>>h>>v,h){
        string s="",e="";
        char tmp;
        for(i=0;i<9;i++){
            cin>>tmp;
            s+=tmp;
        }
        for(i=0;i<9;i++){
            cin>>tmp;
            e+=tmp;
        }
        priority_queue<pair<int,string>> q;
        q.push(make_pair(0,s));
        unordered_map<int,bool> mp;
        pair<int,string> now;
        int num,zero;
        while(!q.empty()){
            now=q.top();q.pop();
            now.first*=-1;
            num=stoi(now.second);
            if(now.second==e){
                cout<<now.first<<endl;
                break;
            }
            if((mp.find(num)==mp.end())){
                mp[num]=true;
            }else{
                continue;
            }
            zero=now.second.find('0');
            string ns=now.second;
            ns[zero]=ns[(zero+1)%9];
            ns[(zero+1)%9]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-h,ns));
            ns[(zero+1)%9]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero-1<0)?8:(zero-1)];
            ns[(zero-1<0)?8:(zero-1)]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-h,ns));
            ns[(zero-1<0)?8:(zero-1)]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero+3)%9];
            ns[(zero+3)%9]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-v,ns));
            ns[(zero+3)%9]=ns[zero];
            ns[zero]='0';
            
            ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';
            if((mp.find(stoi(ns))==mp.end()))
            q.push(make_pair(-now.first-v,ns));
            ns[(zero-3>=0)?zero-3:(9-(3-zero))]=ns[zero];
            ns[zero]='0';
        }
    }
}
accuracy: 1
time_ms: 5860
memory_kb: 29868
final_rank: 17

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


int makeHash(int num){
    vector<int> vDigit{0,1,2,3,4,5,6,7,8};
    vector<int> pDec{1,10,100,1000,10000,100000,1000000,10000000,100000000};
    int base=9;
    int fct=362880;
    int ans=0;
    for(int i=base;i>1;i--){
        fct/=i;
        auto index=lower_bound(vDigit.begin(),vDigit.end(),(num/pDec[i-1])%10);
        ans+=(index-vDigit.begin())*fct;
        vDigit.erase(index);
    }
    return ans;
}

int main(void){
    // cout<<makeHash(305147826)<<endl;
    // return 0;
    vector<int> vDigit{0,1,2,3,4,5,6,7,8};
    vector<int> pDec{1,10,100,1000,10000,100000,1000000,10000000,100000000};
    
    vector<int> dif{-1,1,-3,3};
    // int count=0;
    // do{
    //     int num=0;
    //     for(int i=9;i>0;i--){
    //         num+=pDec[i-1]*vDigit[9-i];
    //     }
    //     mp.emplace(num,count++);
    // }while(next_permutation(vDigit.begin(),vDigit.end()));
    // Your code here!
    int h,v;
    while(cin>>h>>v,h||v){
        string st="         ";
        string end="         ";
        vector<bool> visited(362880,false);
        vector<int> hv{h,h,v,v};
        for(int i=0;i<9;i++){
            cin>>st[i];
        }
        for(int i=0;i<9;i++){
            cin>>end[i];
        }
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
        q.emplace(0,stoi(st));
        int iend=stoi(end);
        while(1){
            int pos=q.top().second;
            int cost=q.top().first;
            if(pos==iend){
                cout<<cost<<endl;
                break;
            }
            q.pop();
            int hash=makeHash(pos);
            if(visited[hash]){
                continue;
            }
            visited[hash]=true;
            int zero=to_string(pos+1000000000).find('0')-1;
            for(int i=0;i<4;i++){
                int s=pos,t=8-((zero+dif[i]+9)%9);
                int tmp=(pos/pDec[t])%10;
                s+=(pDec[8-zero]*tmp-pDec[t]*tmp);
                //cout<<tmp<<" "<<pos<<" "<<s<<endl;
                q.emplace(cost+hv[i],s);
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 2800
memory_kb: 10088
final_rank: 5