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

#include <algorithm>
#include <deque>
#include <iostream>
#include <vector>
#define F first
#define S second
#define all(n) n.begin(), n.end()
using namespace std;
typedef pair<int, int> P;
typedef vector<vector<char>> VVC;

class Pox {
public:
  P p;
  int cost;
  Pox(P p = P(0, 0), int cost = 0) : p(p), cost(cost) {}
};

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

void print(VVC G) {
  cout << "---print---" << endl;
  for (int i = 0; i < G.size(); i++) {
    for (int j = 0; j < G[i].size(); j++)
      cout << G[i][j];
    cout << endl;
  }
  cout << endl;
}

void draw(VVC &G, P p) {

  for (int i = 0; i < 4; i++) {
    int nx = p.F + dx[i];
    int ny = p.S + dy[i];
    if (!(1 <= ny && ny <= h && 1 <= nx && nx <= w))
      continue;
    if (G[ny][nx] == '&' || G[ny][nx] == '#')
      continue;
    G[ny][nx] = '&';
    draw(G, P(nx, ny));
  }
}

int main() {

  while (cin >> w >> h, (w || h)) {

    VVC G;
    P ed;
    G.resize(h + 2);
    deque<Pox> deq;
    int mincost[h + 3][w + 3];
    for (int i = 0; i <= h + 1; i++) {
      G[i].resize(w + 2);
      string line;
      if (i == 0 || i == h + 1)
        line = string(h + 2, '.');
      else
        cin >> line;

      // cout << "line = " << line << endl;
      for (int j = 0; j <= w + 1; j++) {
        mincost[i][j] = (1 << 28);
        if (i == 0 || j == 0 || i == h + 1 || j == w + 1) {
          G[i][j] = '.';
          deq.push_back(Pox(P(j, i), 0));
          mincost[i][j] = 0;
          continue;
        }
        G[i][j] = line[j - 1];
        if (G[i][j] == '&')
          ed = P(j, i);
      }
    }

    // print(G);
    draw(G, ed);
    // print(G);

    int men = (1 << 28);
    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      P p = pox.p;
      int cost = pox.cost;
      // cout << "p = " << p.F << "," << p.S << " " << cost << endl;
      if (G[p.S][p.F] == '&') {
        men = min(men, cost);
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.F + dx[i];
        int ny = p.S + dy[i];

        if (!(1 <= nx && nx <= w && 1 <= ny && ny <= h)) {
          // cout <<"CONTINUE " << i << " th" << endl;
          continue;
        }
        int d = 0;
        if (G[ny][nx] == '#') {
          // cout << "# IN!" << endl;
          while (1 <= nx && nx <= w && 1 <= ny && ny <= h && G[ny][nx] == '#') {
            nx += dx[i];
            ny += dy[i];
          }
          d = 1;
        }

        // int d = G[ny][nx] == '#'?1:0;
        if (mincost[ny][nx] > cost + d) {
          // cout << "RENEW = " << cost +d << endl;
          mincost[ny][nx] = cost + d;
          if (G[ny][nx] == '&') {
            men = min(men, mincost[ny][nx]);
            continue;
          }
          deq.push_back(Pox(P(nx, ny), mincost[ny][nx]));
        }
      }
    }
    cout << men << endl;
  }
  return 0;
}

Error: 0
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###


Fixed submission:

#include <deque>
#include <iostream>
#include <vector>
#define F first
#define S second

using namespace std;
typedef pair<int, int> P;
typedef vector<vector<char>> VVC;

class Pox {
public:
  P p;
  int cost;
  Pox(P p = P(0, 0), int cost = 0) : p(p), cost(cost) {}
};

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

void print(VVC G) {
  cout << "---print---" << endl;
  for (int i = 0; i < G.size(); i++) {
    for (int j = 0; j < G[i].size(); j++)
      cout << G[i][j];
    cout << endl;
  }
  cout << endl;
}

void draw(VVC &G, P p) {

  for (int i = 0; i < 4; i++) {
    int nx = p.F + dx[i];
    int ny = p.S + dy[i];
    if (!(1 <= ny && ny <= h && 1 <= nx && nx <= w))
      continue;
    if (G[ny][nx] == '&' || G[ny][nx] == '#')
      continue;
    G[ny][nx] = '&';
    draw(G, P(nx, ny));
  }
}

int main() {

  while (cin >> w >> h, (w || h)) {
    cerr << w << " " << h << endl;
    VVC G;
    P ed;
    G.resize(h + 2);
    deque<Pox> deq;
    int mincost[h + 3][w + 3];
    for (int i = 0; i <= h + 1; i++) {
      G[i].resize(w + 2);
      string line;
      if (i == 0 || i == h + 1)
        line = string(h + 2, '.');
      else {
        cin >> line;
        cerr << line << endl;
      }
      // cout << "line = " << line << endl;
      for (int j = 0; j <= w + 1; j++) {
        mincost[i][j] = (1 << 28);
        if (i == 0 || j == 0 || i == h + 1 || j == w + 1) {
          G[i][j] = '.';
          deq.push_back(Pox(P(j, i), 0));
          mincost[i][j] = 0;
          continue;
        }
        G[i][j] = line[j - 1];
        if (G[i][j] == '&')
          ed = P(j, i);
      }
    }

    draw(G, ed);

    int men = (1 << 28);
    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      P p = pox.p;
      int cost = pox.cost;
      if (G[p.S][p.F] == '&') {
        men = min(men, cost);
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.F + dx[i];
        int ny = p.S + dy[i];

        if (!(1 <= nx && nx <= w && 1 <= ny && ny <= h))
          continue;

        int d = G[ny][nx] == '#' ? 1 : 0;
        if (mincost[ny][nx] > cost + d) {
          mincost[ny][nx] = cost + d;
          deq.push_back(Pox(P(nx, ny), mincost[ny][nx]));
        }
      }
    }
    cout << men << endl;
  }
  return 0;
}