C. Elevator | Codeforces Round #483 (Div. 1)
http://codeforces.com/blog/entry/59484?#comment-431237
これを見てそのとおりに書いた。
using P = array<int, 7>; static bitset<2001> vis[10][10][10][10][10]; int N; cin >> N; vi A(N), B(N); rep(a, N)cin >> A[a] >> B[a]; queue<P> Q; Q.push({ 0,0,0,0,1,0,0 }); while (sz(Q)) { auto p = Q.front(); Q.pop(); auto &v = vis[p[0]][p[1]][p[2]][p[3]][p[4]]; if (v[p[5]])continue; int d = p[6]++, i = p[5], cur = p[4]; v[p[5]] = 1; if (p[3] == 0 && i == N) { cout << d << endl; break; } if (i < N && A[i] == cur && p[0]==0) { auto np = p; np[0] = B[i]; np[5]++; sort(np.begin(), np.begin() + 4); Q.push(np); } if (cur > 1) { p[4]--; Q.push(p); p[4]++; } if (cur < 9) { p[4]++; Q.push(p); p[4]--; } rep(a, 4) if (p[a] == cur) { p[a] = 0; sort(p.begin(), p.begin() + 4); Q.push(p); break; } }