Problem p00121 (C++) — buggy submission, verdict: Runtime Error

/*
P135
AOJ 0558 Cheese
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0558
*/

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
using namespace std;

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

int num;
int pos0;
typedef pair<int, int> PAIR;
map<int, int> mp;

int pow10[8] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

bool check(int x, int y) { return (x >= 0 && x < 2 && y >= 0 && y < 4); }

bool read() {
  num = 0;
  int x;
  for (int i = 0; i < 8; ++i) {
    if (scanf("%d", &x) == EOF)
      return false;
    num *= 10;
    num += x;
  }
  return true;
}

int swapPos(int n, const int &p0, const int &p2) {
  int d = n / pow10[7 - p2] % 10;
  n -= d * pow10[7 - p2];
  n += d * pow10[7 - p0];
  return n;
}

void bfs() {
  queue<PAIR> q;
  mp[1234567] = 0;
  q.push(make_pair(1234567, 0));

  while (!q.empty()) {
    PAIR cur = q.front();
    q.pop();
    int curNum = cur.first;
    int p0 = cur.second;
    int step = mp[curNum];
    int x = p0 >> 2, y = p0 & 3;

    for (int d = 0; d < 4; ++d) {
      int cx = x + dx[d], cy = y + dy[d];
      if (check(cx, cy)) {
        int cn = (cx * 4 + cy);
        int v = swapPos(curNum, p0, cn);
        if (!mp.count(v)) {
          mp[v] = step + 1;
          q.push(make_pair(v, cn));
        }
      }
    }
  }
}

void solve() { printf("%d\\n", mp[num]); }

int main() {
  bfs();
  while (read())
    solve();
}

/*
int W, H;
char board[110][110];
int a[10];
bool read()
{
    for(int i=0; i<10; ++i)
        scanf("%d", a+i);
}

bool isVisited[110][110];
int step;

void dfs(int x, int y, char c){
    if(x<0 || x>=H || y<0 || y>=W) return;
    if(board[x][y]!=c) return;
    if(isVisited[x][y]) return;
    isVisited[x][y] = true;
    dfs(x+1, y, c);
    dfs(x-1, y, c);
    dfs(x, y+1, c);
    dfs(x, y-1, c);
}



void solve1()
{
    int x1=0, x2=0;
    bool ans = true;
    for(int i=0; i<10; ++i)
    {
        if(x1 < x2) swap(x1, x2);
        if(x1 < a[i]) x1 = a[i];
        else if(x2 < a[i]) x2 = a[i];
        else {ans = false; break;}
    }
    if(ans) printf("YES\\n");
    else printf("NO");
}



vector<int> L_bucket;
vector<int> R_bucket;

bool dfs2(int d){
    if(d == 10) return true;
    bool flag;
    if(L_bucket.size() == 0 || L_bucket.back() < a[d]){
        L_bucket.push_back(a[d]);
        flag = dfs2(d+1);
        L_bucket.pop_back();
    }
    if(flag) return true;
    if(R_bucket.size() == 0 || R_bucket.back() < a[d]){
        R_bucket.push_back((a[d]));
        flag = dfs2(d+1);
        R_bucket.pop_back();
    }
    if(flag) return true;
    return false;

}

void solve2()
{
    if(dfs2(0))
        printf("YES\\n");
    else printf("NO\\n");
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        read();
        solve2();
    }
}
*/

Error: -11

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int find_locate(int node, int num) {
  int ans = 0;
  while (ans < 8) {
    int a, b, c;
    int keta_a = 1, keta_b = 1;
    for (int j = 0; j < ans; j++) {
      keta_a = keta_a * 10;
      keta_b = keta_b * 10;
    }
    keta_a = keta_a * 10;
    a = node % keta_a;
    b = node % keta_b;
    c = a - b;
    c = c / keta_b;

    if (c == num) {
      break;
    }
    ans++;
  }
  return ans;
}

int insert(int node, int keta, int num) {
  int a, b, c, d;
  int keta_a = 1, keta_b = 1;
  int ans;
  for (int i = 0; i < keta; i++) {
    keta_a = keta_a * 10;
    keta_b = keta_b * 10;
  }
  keta_a = keta_a * 10;
  a = node % keta_a;
  b = node % keta_b;
  c = a - b;
  d = num * keta_b;
  ans = node - c;
  ans = ans + d;
  return ans;
}

int look(int node, int num) {
  int a, b, c;
  int keta_a = 1, keta_b = 1;
  for (int j = 0; j < num; j++) {
    keta_a = keta_a * 10;
    keta_b = keta_b * 10;
  }
  keta_a = keta_a * 10;
  a = node % keta_a;
  b = node % keta_b;
  c = a - b;
  c = c / keta_b;

  return c;
}

int proceed_adj(int node, int dirc) {
  int loc_zero = find_locate(node, 0);
  int swap;
  if (dirc == 0) {
    // go down
    if (loc_zero <= 3) {
      return 0;
    }
    swap = look(node, loc_zero - 4);
    node = insert(node, loc_zero - 4, 0);
    node = insert(node, loc_zero, swap);

    return node;
  }
  if (dirc == 1) {
    // go up
    if (loc_zero >= 4) {
      return 0;
    }
    swap = look(node, loc_zero + 4);
    node = insert(node, loc_zero + 4, 0);
    node = insert(node, loc_zero, swap);

    return node;
  }
  if (dirc == 2) {
    // go left
    if (loc_zero == 3 || loc_zero == 7) {
      return 0;
    }
    swap = look(node, loc_zero + 1);
    node = insert(node, loc_zero + 1, 0);
    node = insert(node, loc_zero, swap);

    return node;
  }
  if (dirc == 3) {
    // go right
    if (loc_zero == 0 || loc_zero == 4) {
      return 0;
    }
    swap = look(node, loc_zero - 1);
    node = insert(node, loc_zero - 1, 0);
    node = insert(node, loc_zero, swap);

    return node;
  }
}

/*
int main(void) {
    int node1 = 12034567;
    int node2 = 12345067;
    printf("node1 is: %d\\n", node1);
    for (int i=0; i<4; i++) {
        printf("i = %d, then adj node is %d\\n", i, proceed_adj(node1, i));
    }
    printf("\\n");
    printf("node2 is: %d\\n", node2);
    for (int i=0; i<4; i++) {
        printf("i = %d, then adj node is %d\\n", i, proceed_adj(node2, i));
    }
    return 0;
}
*/

int main(void) {
  int n = 76543210;

  int input;
  int node, adj_node;

  while (scanf("%d", &input) != EOF) {

    map<int, int> conf; // key:vertex value:distance
    queue<int> q;
    conf[input] = 0;
    q.push(input);

    while (true) {
      node = q.front();
      q.pop();
      if (node == 1234567) {
        break;
      }

      for (int i = 0; i < 4; i++) {
        int adj_id, adj_dist;
        bool adj_visited = false;
        adj_id = proceed_adj(node, i);
        if (adj_id == 0) {
          continue;
        }

        if (conf.count(adj_id) == 1) {
          continue;
        }

        conf[adj_id] = conf[node] + 1;
        q.push(adj_id);
      }
    }
    printf("%d\\n", conf[node]);
  }
}