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

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
struct State {
  int x, y, foot, cost;
  State(int x, int y, int foot, int cost)
      : x(x), y(y), foot(foot), cost(cost) {}
};

bool operator<(const State &lhs, const State &rhs) {
  return lhs.cost > rhs.cost;
}

const int MAX = (1 << 21);
const int dx[2][9] = {{-3, -2, -2, -2, -1, -1, -1, -1, -1},
                      {3, 2, 2, 2, 1, 1, 1, 1, 1}};
const int dy[2][9] = {{0, -1, 0, 1, -2, -1, 0, 1, 2},
                      {0, -1, 0, 1, -2, -1, 0, 1, 2}};

char field[80][80];
bool visited[80][80][2];

int main() {
  int w, h;
  while (cin >> w >> h && w && h) {

    priority_queue<State, vector<State>, less<State>> que;
    for (int i = 0; i < 80; i++) {
      for (int j = 0; j < 80; j++) {
        field[i][j] = 'X';
        visited[i][j][0] = visited[i][j][1] = false;
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i + 5][j + 5];
        if (field[i + 5][j + 5] == 'S') {
          que.push(State(j + 5, i + 5, 0, 0));
          que.push(State(j + 5, i + 5, 1, 0));
          field[i + 5][j + 5] = '0';
        }
      }
    }

    int ret = MAX;
    while (!que.empty()) {
      State cur = que.top();
      que.pop();
      if (field[cur.y][cur.x] == 'T') {
        ret = cur.cost;
        break;
      }
      visited[cur.y][cur.x][cur.foot] = true;

      // cout << cur.x << " " << cur.y << " " << cur.foot << " " << cur.cost <<
      // endl;
      for (int i = 0; i < 9; i++) {
        int nx, ny, nfoot, ncost;
        nfoot = (cur.foot + 1) % 2;
        nx = cur.x + dx[nfoot][i];
        ny = cur.y + dy[nfoot][i];
        if (field[ny][nx] == 'X' || visited[ny][nx][nfoot]) {
          continue;
        }
        ncost = cur.cost + ((field[ny][nx] == 'T') ? 0 : (field[ny][nx] - '0'));
        que.push(State(nx, ny, nfoot, ncost));
      }
    }
    cout << ((ret == MAX) ? -1 : ret) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
struct state {
  int step, foot, lx, ly, rx, ry;
};

bool operator<(const state &lhs, const state &rhs) {
  return lhs.step < rhs.step;
}

bool operator==(const state &lhs, const state &rhs) {
  return lhs.step == rhs.step;
}

bool operator>(const state &lhs, const state &rhs) {
  return lhs.step > rhs.step;
}

char field[66][36];
bool visited[2][66][36]; // lx, ly, rx, ry

int main() {
  int w, h;
  while (cin >> w >> h && w && h) {
    vector<int> sx, sy;

    for (int i = 0; i < 66; i++) {
      for (int j = 0; j < 36; j++) {
        field[i][j] = 'X';
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i + 3][j + 3];
        if (field[i + 3][j + 3] == 'S') {
          sx.push_back(j + 3);
          sy.push_back(i + 3);
          field[i + 3][j + 3] = '0';
        }
      }
    }

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

    int ret = 9999999;
    for (int start = 0; start < sx.size(); start++) {
      for (int sfoot = 0; sfoot < 2; sfoot++) {
        for (int f = 0; f < 2; f++) {
          for (int i = 0; i < 66; i++) {
            fill(visited[f][i], visited[f][i] + 36, false);
          }
        }

        priority_queue<state, vector<state>, greater<state>> que;
        que.push((state){0, sfoot, sx[start], sy[start], sx[(start + 1) % 2],
                         sy[(start + 1) % 2]});
        while (!que.empty()) {
          state cur = que.top();
          que.pop();
          // cout << cur.step << " " << cur.foot << " " << cur.lx << " " <<
          // cur.ly << " " << field[cur.ly][cur.lx] << " " << cur.rx << " " <<
          // cur.ry << " " << field[cur.ry][cur.rx] << endl;
          if (cur.foot == 0) {
            if (visited[cur.foot][cur.ly][cur.lx]) {
              continue;
            } else if (field[cur.ly][cur.lx] == 'T') {
              ret = min(ret, cur.step);
              break;
            }
            visited[cur.foot][cur.ly][cur.lx] = true;
          } else {
            if (visited[cur.foot][cur.ry][cur.rx]) {
              continue;
            } else if (field[cur.ry][cur.rx] == 'T') {
              ret = min(ret, cur.step);
              break;
            }
            visited[cur.foot][cur.ry][cur.rx] = true;
          }

          for (int i = 0; i < 9; i++) {
            int nx, ny;
            if (cur.foot == 0) {
              nx = cur.lx + dx[i];
              ny = cur.ly + dy[i];
              if (field[ny][nx] == 'T') {
                que.push((state){cur.step, 1, cur.lx, cur.ly, nx, ny});
              } else if (field[ny][nx] != 'X') {
                que.push((state){cur.step + field[ny][nx] - '0', 1, cur.lx,
                                 cur.ly, nx, ny});
              } else {
              }
            } else {
              nx = cur.rx - dx[i];
              ny = cur.ry + dy[i];
              if (field[ny][nx] == 'T') {
                que.push((state){cur.step, 0, nx, ny, cur.rx, cur.ry});
              } else if (field[ny][nx] != 'X') {
                que.push((state){cur.step + field[ny][nx] - '0', 0, nx, ny,
                                 cur.rx, cur.ry});
              } else {
              }
            }
          }
        }
      }
    }
    if (ret == 9999999) {
      cout << -1 << endl;
    } else {
      cout << ret << endl;
    }
  }
}