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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum DIR {
  North,
  East,
  South,
  West,
};

struct Info {
  bool left_wall, under_wall;
};

struct Data {
  void set(int arg_x, int arg_y) {
    x = arg_x;
    y = arg_y;
  }
  int x, y;
};

struct State {
  void set(int arg_x, int arg_y, DIR arg_dir, int arg_sum_cost) {
    x = arg_x;
    y = arg_y;
    dir = arg_dir;
    sum_cost = arg_sum_cost;
  }
  int x, y;
  DIR dir;
  int sum_cost;
};

int W, H, N;
Info info[101][101];
bool visited[101][101][4];
Data goal;

void func() {

  for (int x = 0; x <= W; x++) {
    for (int y = 0; y <= H; y++) {
      info[x][y].left_wall = false;
      info[x][y].under_wall = false;

      for (int i = 0; i < 4; i++)
        visited[x][y][i] = false;
    }
  }

  for (int y = 0; y < H; y++) {
    info[0][y].left_wall = true;
  }

  for (int x = 0; x < W; x++) {
    info[x][H].under_wall = true;
  }

  for (int y = 0; y < H; y++) {
    info[W][y].left_wall = true;
  }

  for (int x = 0; x < W; x++) {
    info[x][0].under_wall = true;
  }

  Data left, right;

  for (int i = 0; i < N; i++) {
    scanf("%d %d %d %d", &left.x, &left.y, &right.x, &right.y);

    if (left.x == right.x) {
      if (right.y < left.y)
        swap(left, right);

      for (int y = left.y; y < right.y; y++) {
        info[left.x][y].left_wall = true;
      }
    } else {
      if (right.x < left.x)
        swap(left, right);

      for (int x = left.x; x < right.x; x++) {
        info[x][left.y].under_wall = true;
      }
    }
  }

  State state;

  scanf("%d %d %d %d", &left.x, &left.y, &right.x, &right.y);

  if (left.x == right.x) {
    if (right.y < left.y)
      swap(left, right);

    if (left.x == 0) {

      info[left.x][left.y].left_wall = false;
      state.set(left.x, left.y, East, 1);

    } else {

      info[left.x][left.y].left_wall = false;
      state.set(left.x - 1, left.y, West, 1);
    }
  } else {
    if (right.x < left.x)
      swap(left, right);

    if (left.y == H) {

      info[left.x][left.y].under_wall = false;
      state.set(left.x, left.y - 1, South, 1);

    } else {

      info[left.x][left.y].under_wall = false;
      state.set(left.x, left.y, North, 1);
    }
  }

  visited[state.x][state.y][state.dir] = true;

  scanf("%d %d", &goal.x, &goal.y);

  int ans = BIG_NUM;
  bool FLG;

  while (true) {

    FLG = true;

    switch (state.dir) {
    case North:
      if (info[state.x][state.y].left_wall == false) {
        FLG = false;
      }
      break;
    case East:
      if (info[state.x][state.y + 1].under_wall == false) {
        FLG = false;
      }
      break;
    case South:
      if (info[state.x + 1][state.y].left_wall == false) {
        FLG = false;
      }
      break;
    case West:
      if (info[state.x][state.y].under_wall == false) {
        FLG = false;
      }
      break;
    }

    if (FLG) {

      switch (state.dir) {
      case North:
        if (info[state.x][state.y + 1].under_wall == false) {
          state.y++;
          state.sum_cost++;
        } else {
          state.dir = East;
        }
        break;
      case East:
        if (info[state.x + 1][state.y].left_wall == false) {
          state.x++;
          state.sum_cost++;
        } else {
          state.dir = South;
        }
        break;
      case South:
        if (info[state.x][state.y].under_wall == false) {
          state.y--;
          state.sum_cost++;
        } else {
          state.dir = West;
        }
        break;
      case West:
        if (info[state.x][state.y].left_wall == false) {
          state.x--;
          state.sum_cost++;
        } else {
          state.dir = North;
        }
        break;
      }

    } else {

      switch (state.dir) {
      case North:
        if (state.x - 1 == goal.x && state.y == goal.y) {
          if (state.x != 0) {
            ans = state.sum_cost + 1;
          }
        } else {

          state.x--;
          state.sum_cost++;

          if (info[state.x][state.y].under_wall == true) {
            state.dir = West;
          } else {
            state.y--;
            state.sum_cost++;
            state.dir = South;
          }
        }
        break;
      case East:
        if (state.x == goal.x && state.y + 1 == goal.y) {
          if (state.y != H - 1) {
            ans = state.sum_cost + 1;
          }
        } else {

          state.y++;
          state.sum_cost++;

          if (info[state.x][state.y].left_wall == true) {
            state.dir = North;
          } else {
            state.x--;
            state.sum_cost++;
            state.dir = West;
          }
        }
        break;
      case South:
        if (state.x + 1 == goal.x && state.y == goal.y) {
          if (state.x != W - 1) {
            ans = state.sum_cost + 1;
          }
        } else {

          state.x++;
          state.sum_cost++;

          if (info[state.x][state.y + 1].under_wall == true) {
            state.dir = East;
          } else {
            state.y++;
            state.sum_cost++;
            state.dir = North;
          }
        }
        break;
      case West:
        if (state.x == goal.x && state.y - 1 == goal.y) {
          if (state.y != 0) {
            ans = state.sum_cost + 1;
          }
        } else {

          state.y--;
          state.sum_cost++;

          if (info[state.x + 1][state.y].left_wall == true) {
            state.dir = South;
          } else {
            state.x++;
            state.sum_cost++;
            state.dir = East;
          }
        }
        break;
      }

      if (ans != BIG_NUM)
        break;
    }

    if (state.x < 0 || state.x >= W || state.y < 0 || state.y >= H)
      break;

    if (visited[state.x][state.y][state.dir])
      break;
    visited[state.x][state.y][state.dir] = true;

    if (state.x == goal.x && state.y == goal.y) {
      ans = state.sum_cost;
      break;
    }
  }

  if (ans == BIG_NUM) {
    printf("Impossible\\n");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {

  while (true) {
    scanf("%d %d %d", &W, &H, &N);
    if (W == 0 && H == 0 && N == 0)
      break;

    func();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum DIR {
  North,
  East,
  South,
  West,
};

struct Info {
  bool left_wall, under_wall;
};

struct Data {
  void set(int arg_x, int arg_y) {
    x = arg_x;
    y = arg_y;
  }
  int x, y;
};

struct State {
  void set(int arg_x, int arg_y, DIR arg_dir, int arg_sum_cost) {
    x = arg_x;
    y = arg_y;
    dir = arg_dir;
    sum_cost = arg_sum_cost;
  }
  int x, y;
  DIR dir;
  int sum_cost;
};

int W, H, N;
Info info[101][101];
bool visited[101][101][4];
Data goal;

void func() {

  for (int x = 0; x <= W; x++) {
    for (int y = 0; y <= H; y++) {
      info[x][y].left_wall = false;
      info[x][y].under_wall = false;

      for (int i = 0; i < 4; i++)
        visited[x][y][i] = false;
    }
  }

  for (int y = 0; y < H; y++) {
    info[0][y].left_wall = true;
  }

  for (int x = 0; x < W; x++) {
    info[x][H].under_wall = true;
  }

  for (int y = 0; y < H; y++) {
    info[W][y].left_wall = true;
  }

  for (int x = 0; x < W; x++) {
    info[x][0].under_wall = true;
  }

  Data left, right;

  for (int i = 0; i < N; i++) {
    scanf("%d %d %d %d", &left.x, &left.y, &right.x, &right.y);

    if (left.x == right.x) {
      if (right.y < left.y)
        swap(left, right);

      for (int y = left.y; y < right.y; y++) {
        info[left.x][y].left_wall = true;
      }
    } else {
      if (right.x < left.x)
        swap(left, right);

      for (int x = left.x; x < right.x; x++) {
        info[x][left.y].under_wall = true;
      }
    }
  }

  State state;

  scanf("%d %d %d %d", &left.x, &left.y, &right.x, &right.y);

  if (left.x == right.x) {
    if (right.y < left.y)
      swap(left, right);

    if (left.x == 0) {

      info[left.x][left.y].left_wall = false;
      state.set(left.x, left.y, East, 1);

    } else {

      info[left.x][left.y].left_wall = false;
      state.set(left.x - 1, left.y, West, 1);
    }
  } else {
    if (right.x < left.x)
      swap(left, right);

    if (left.y == H) {

      info[left.x][left.y].under_wall = false;
      state.set(left.x, left.y - 1, South, 1);

    } else {

      info[left.x][left.y].under_wall = false;
      state.set(left.x, left.y, North, 1);
    }
  }

  visited[state.x][state.y][state.dir] = true;

  scanf("%d %d", &goal.x, &goal.y);

  int ans = BIG_NUM;

  bool FLG = true;

  while (true) {

    switch (state.dir) {
    case North:
      if (info[state.x][state.y].left_wall == false) {
        FLG = false;
      }
      break;
    case East:
      if (info[state.x][state.y + 1].under_wall == false) {
        FLG = false;
      }
      break;
    case South:
      if (info[state.x + 1][state.y].left_wall == false) {
        FLG = false;
      }
      break;
    case West:
      if (info[state.x][state.y].under_wall == false) {
        FLG = false;
      }
      break;
    }

    if (!FLG)
      break;

    switch (state.dir) {
    case North:
      if (info[state.x][state.y + 1].under_wall == false) {
        state.y++;
        state.sum_cost++;
      } else {
        state.dir = East;
      }
      break;
    case East:
      if (info[state.x + 1][state.y].left_wall == false) {
        state.x++;
        state.sum_cost++;
      } else {
        state.dir = South;
      }
      break;
    case South:
      if (info[state.x][state.y].under_wall == false) {
        state.y--;
        state.sum_cost++;
      } else {
        state.dir = West;
      }
      break;
    case West:
      if (info[state.x][state.y].left_wall == false) {
        state.x--;
        state.sum_cost++;
      } else {
        state.dir = North;
      }
      break;
    }

    if (visited[state.x][state.y][state.dir])
      break;
    visited[state.x][state.y][state.dir] = true;

    if (state.x == goal.x && state.y == goal.y) {
      ans = state.sum_cost;
      break;
    }
  }

  if (ans == BIG_NUM) {
    printf("Impossible\\n");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {

  while (true) {
    scanf("%d %d %d", &W, &H, &N);
    if (W == 0 && H == 0 && N == 0)
      break;

    func();
  }

  return 0;
}