Problem p00725 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

int gx, gy, h, w;
int cost[20][20];
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void solve(int x, int y, int field[20][20]) {
  static int count = 0;
  int i, j, flag;
  if (cost[y][x] > count || cost[y][x] == -1) {
    cost[y][x] = count;
    if (field[y][x] == 3)
      return;
  }
  if (count < 10) {
    for (i = 0; i < 4; i++) {
      flag = 0;
      for (j = 1; x + dx[i] * j >= 0 && x + dx[i] * j < w &&
                  y + dy[i] * j >= 0 && y + dy[i] * j < h;
           j++) {
        if (field[y + dy[i] * j][x + dx[i] * j]) {
          flag = 1;
          break;
        }
      }
      if (field[y + dy[i] * j][x + dx[i] * j] == 3) {
        count++;
        solve(x + dx[i] * j, y + dy[i] * j, field);
        count--;
      } else if (j != 1 && flag) {
        field[y + dy[i] * j][x + dx[i] * j] = 0;
        count++;
        solve(x + dx[i] * (j - 1), y + dy[i] * (j - 1), field);
        field[y + dy[i] * j][x + dx[i] * j] = 1;
        count--;
      }
    }
  }
}

int main(void) {
  int i, j, field[20][20], sx, sy;
  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++)
        scanf("%d", &field[i][j]);
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (field[i][j] == 2) {
          sy = i, sx = j;
          field[i][j] = 0;
        } else if (field[i][j] == 3)
          gy = i, gx = j;
      }
    }
    memset(cost, -1, sizeof(cost));
    solve(sx, sy, field);
    printf("%d\n", cost[gy][gx]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int gx, gy, h, w;
int cost[20][20];
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void solve(int x, int y, int field[20][20]) {
  static int count = 0;
  int i, j, flag;
  if (cost[y][x] > count || cost[y][x] == -1) {
    cost[y][x] = count;
    if (field[y][x] == 3)
      return;
  }
  if (count < 10) {
    for (i = 0; i < 4; i++) {
      flag = 0;
      for (j = 1; x + dx[i] * j >= 0 && x + dx[i] * j < w &&
                  y + dy[i] * j >= 0 && y + dy[i] * j < h;
           j++) {
        if (field[y + dy[i] * j][x + dx[i] * j]) {
          flag = 1;
          break;
        }
      }
      if (flag && field[y + dy[i] * j][x + dx[i] * j] == 3) {
        count++;
        solve(x + dx[i] * j, y + dy[i] * j, field);
        count--;
      } else if (j != 1 && flag) {
        field[y + dy[i] * j][x + dx[i] * j] = 0;
        count++;
        solve(x + dx[i] * (j - 1), y + dy[i] * (j - 1), field);
        field[y + dy[i] * j][x + dx[i] * j] = 1;
        count--;
      }
    }
  }
}

int main(void) {
  int i, j, field[20][20], sx, sy;
  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++)
        scanf("%d", &field[i][j]);
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (field[i][j] == 2) {
          sy = i, sx = j;
          field[i][j] = 0;
        } else if (field[i][j] == 3)
          gy = i, gx = j;
      }
    }
    memset(cost, -1, sizeof(cost));
    solve(sx, sy, field);
    printf("%d\n", cost[gy][gx]);
  }
  return 0;
}