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

#include <iostream>
#include <queue>

using namespace std;

const int MAX = 101;
const int INF = (1 << 25);

struct State {
  int x, y, t;
  State(int a, int b, int c) : x(a), y(b), t(c) {}
  bool operator<(const State &s) const { return t > s.t; }
};

int T[MAX][MAX];
char M[MAX][MAX];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int sx, sy;

void print() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cout << T[i][j];
    cout << endl;
  }
}

void init() {
  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      T[i][j] = INF;
}

void input() {
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
      cin >> M[i][j];
      if (M[i][j] == '&') {
        sx = j;
        sy = i;
      }
    }
}

bool isInside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }

void solve() {

  T[sy][sx] = 0;

  priority_queue<State> Q;

  Q.push(State(sx, sy, 0));

  while (!Q.empty()) {
    State s = Q.top();
    Q.pop();

    if (T[s.y][s.x] < s.t)
      continue;

    for (int i = 0; i < 4; i++) {
      int nx = s.x + dx[i];
      int ny = s.y + dy[i];
      if (!isInside(nx, ny))
        continue;

      /*   if(M[ny][nx] == '#' && T[ny][nx] > s.t+1){
        T[ny][nx] = s.t+1;
        Q.push(State(nx,ny,s.t+1));
      }

      if(M[ny][nx] != '#' && T[ny][nx] > s.t){
        T[ny][nx] = s.t;
        Q.push(State(nx,ny,s.t));
        }*/

      if (M[ny][nx] == '#') {

        if (M[s.y][s.x] == '#' && T[ny][nx] > s.t) {
          T[ny][nx] = s.t;
          Q.push(State(nx, ny, s.t));
        }
        if (M[s.y][s.x] != '#' && T[ny][nx] > s.t + 1) {
          T[ny][nx] = s.t + 1;
          Q.push(State(nx, ny, s.t + 1));
        }

      } else if (T[ny][nx] > s.t) {
        T[ny][nx] = s.t;
        Q.push(State(nx, ny, s.t));
      }
    }
  }

  int ans = INF;

  for (int i = 0; i < n; i++)
    ans = min(ans, min(T[0][i], T[m - 1][i]));
  for (int i = 0; i < m; i++)
    ans = min(ans, min(T[i][0], T[i][n - 1]));

  cout << ans << endl;
  //  print();
}
int main() {

  while (cin >> n >> m && n + m) {
    init();
    input();
    solve();
  }
  return 0;
}

Error: 0
5 5
18 15
9 10
9 3


Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

const int MAX = 101;
const int INF = (1 << 25);

struct State {
  int x, y, t;
  State(int a, int b, int c) : x(a), y(b), t(c) {}
  bool operator<(const State &s) const { return t > s.t; }
};

int T[MAX][MAX];
char M[MAX][MAX];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int sx, sy;

void print() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cout << T[i][j];
    cout << endl;
  }
}

void pp() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cerr << M[i][j];
    cerr << endl;
  }
  cerr << endl;
}

void init() {
  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      T[i][j] = INF;
}

void input() {
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
      cin >> M[i][j];
      if (M[i][j] == '&') {
        sx = j;
        sy = i;
      }
    }
}

bool isInside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }

void solve() {

  T[sy][sx] = 0;

  priority_queue<State> Q;

  Q.push(State(sx, sy, 0));

  while (!Q.empty()) {
    State s = Q.top();
    Q.pop();

    if (T[s.y][s.x] < s.t)
      continue;

    for (int i = 0; i < 4; i++) {
      int nx = s.x + dx[i];
      int ny = s.y + dy[i];
      if (!isInside(nx, ny))
        continue;

      if (M[ny][nx] == '#' && T[ny][nx] > s.t + 1) {
        T[ny][nx] = s.t + 1;
        Q.push(State(nx, ny, s.t + 1));
      }

      if (M[ny][nx] != '#' && T[ny][nx] > s.t) {
        T[ny][nx] = s.t;
        Q.push(State(nx, ny, s.t));
      }
    }
  }

  int ans = INF;

  for (int i = 0; i < n; i++)
    ans = min(ans, min(T[0][i], T[m - 1][i]));
  for (int i = 0; i < m; i++)
    ans = min(ans, min(T[i][0], T[i][n - 1]));

  cout << ans << endl;
  // cerr << ans << endl;
  //   print();
}
int main() {

  while (cin >> n >> m && n + m) {
    cerr << n << " " << m << endl;
    init();
    input();
    solve();
  }

  return 0;
}