Problem p01251 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

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

int W, H, N;
bool field[1000][1000];
bool used[4][110][110];

int main() {

  while (cin >> W >> H >> N && (W | H | N)) {
    memset(field, 0, sizeof(field));
    memset(used, 0, sizeof(used));
    for (int i = 0; i <= 2 * H; i++) {
      field[i][0] = true;
      field[i][2 * W] = true;
    }
    for (int i = 0; i <= 2 * W; i++) {
      field[0][i] = true;
      field[2 * H][i] = true;
    }
    for (int i = 0; i < N; i++) {
      int x1, x2, y1, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      if (y1 == y2) {
        int minx = min(x1, x2);
        int maxx = max(x1, x2);
        for (int j = minx * 2; j <= maxx * 2; j++)
          field[2 * y1][j] = true;
      } else {
        int miny = min(y1, y2);
        int maxy = max(y1, y2);
        for (int j = miny * 2; j <= maxy * 2; j++)
          field[j][2 * x1] = true;
      }
    }
    int xe1, ye1, xe2, ye2;
    int gx, gy;
    int sx, sy;
    int sang;
    cin >> xe1 >> ye1 >> xe2 >> ye2;
    cin >> gx >> gy;
    gx = gx * 2 + 1;
    gy = gy * 2 + 1;
    if (ye1 == ye2) {
      if (ye1 == 0) {
        sang = 3;
        sx = min(xe1, xe2) * 2 + 1;
        sy = ye1;
      } else {
        sang = 1;
        sx = min(xe1, xe2) * 2 + 1;
        sy = ye1 * 2;
      }
    } else {
      if (xe1 == 0) {
        sang = 0;
        sx = xe1;
        sy = min(ye1, ye2) * 2 + 1;
      } else {
        sang = 2;
        sx = xe1 * 2;
        sy = min(ye1, ye2) * 2 + 1;
      }
    }
    field[sy][sx] = false;
    // for(int i=2*H;i>=0;i--){
    //   for(int j=0;j<=2*W;j++){
    // 	if(field[i][j])cout<<"*";
    // 	else cout<<" ";
    //   }
    //   cout<<endl;
    // }
    int cx = sx;
    int cy = sy;
    int cang = sang;
    bool ok = true;
    int tern = 0;
    while (1) {
      if (used[cang][cy][cx]) {
        tern++;
        ok = false;
        break;
      } else if (cy == gy && cx == gx)
        break;
      // cout<<cang<<" "<<cy<<" "<<cx<<" "<<tern<<endl;
      used[cang][cy][cx] = true;
      int ny = cy + dy[cang];
      int nx = cx + dx[cang];
      int nyl = ny + dy[(cang + 3) % 4];
      int nxl = nx + dx[(cang + 3) % 4];
      int ry = cy + dy[(cang + 1) % 4];
      int rx = cx + dx[(cang + 1) % 4];
      if (!field[ny][nx] && !field[nyl][nxl]) {
        cy = nyl;
        cx = nxl;
        cang = (cang + 3) % 4;
        tern++;
        // if(cy%2==1&&cx%2==1)tern++;
      } else if (!field[ny][nx] && field[nyl][nxl]) {
        cy = ny;
        cx = nx;
        if (cy % 2 == 1 && cx % 2 == 1)
          tern++;
      } else if (field[ny][nx] && !field[ry][rx]) {
        cy = ry;
        cx = rx;
        cang = (cang + 1) % 4;
        if (cy % 2 == 1 && cx % 2 == 1)
          tern++;
      } else
        cang = (cang + 1) % 4;
    }
    if (!ok)
      cout << "Impossible" << endl;
    else
      cout << tern << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

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

int W, H, N;
bool field[1000][1000];
bool used[4][1000][1000];

int main() {

  while (cin >> W >> H >> N && (W | H | N)) {
    memset(field, 0, sizeof(field));
    memset(used, 0, sizeof(used));
    for (int i = 0; i <= 2 * H; i++) {
      field[i][0] = true;
      field[i][2 * W] = true;
    }
    for (int i = 0; i <= 2 * W; i++) {
      field[0][i] = true;
      field[2 * H][i] = true;
    }
    for (int i = 0; i < N; i++) {
      int x1, x2, y1, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      if (y1 == y2) {
        int minx = min(x1, x2);
        int maxx = max(x1, x2);
        for (int j = minx * 2; j <= maxx * 2; j++)
          field[2 * y1][j] = true;
      } else {
        int miny = min(y1, y2);
        int maxy = max(y1, y2);
        for (int j = miny * 2; j <= maxy * 2; j++)
          field[j][2 * x1] = true;
      }
    }
    int xe1, ye1, xe2, ye2;
    int gx, gy;
    int sx, sy;
    int sang;
    cin >> xe1 >> ye1 >> xe2 >> ye2;
    cin >> gx >> gy;
    gx = gx * 2 + 1;
    gy = gy * 2 + 1;
    if (ye1 == ye2) {
      if (ye1 == 0) {
        sang = 3;
        sx = min(xe1, xe2) * 2 + 1;
        sy = ye1;
      } else {
        sang = 1;
        sx = min(xe1, xe2) * 2 + 1;
        sy = ye1 * 2;
      }
    } else {
      if (xe1 == 0) {
        sang = 0;
        sx = xe1;
        sy = min(ye1, ye2) * 2 + 1;
      } else {
        sang = 2;
        sx = xe1 * 2;
        sy = min(ye1, ye2) * 2 + 1;
      }
    }
    field[sy][sx] = false;
    // for(int i=2*H;i>=0;i--){
    //   for(int j=0;j<=2*W;j++){
    // 	if(field[i][j])cout<<"*";
    // 	else cout<<" ";
    //   }
    //   cout<<endl;
    // }
    int cx = sx;
    int cy = sy;
    int cang = sang;
    bool ok = true;
    int tern = 0;
    while (1) {
      if (used[cang][cy][cx]) {
        tern++;
        ok = false;
        break;
      } else if (cy == gy && cx == gx)
        break;
      // cout<<cang<<" "<<cy<<" "<<cx<<" "<<tern<<endl;
      used[cang][cy][cx] = true;
      int ny = cy + dy[cang];
      int nx = cx + dx[cang];
      int nyl = ny + dy[(cang + 3) % 4];
      int nxl = nx + dx[(cang + 3) % 4];
      int ry = cy + dy[(cang + 1) % 4];
      int rx = cx + dx[(cang + 1) % 4];
      if (!field[ny][nx] && !field[nyl][nxl]) {
        cy = nyl;
        cx = nxl;
        cang = (cang + 3) % 4;
        tern++;
        // if(cy%2==1&&cx%2==1)tern++;
      } else if (!field[ny][nx] && field[nyl][nxl]) {
        cy = ny;
        cx = nx;
        if (cy % 2 == 1 && cx % 2 == 1)
          tern++;
      } else if (field[ny][nx] && !field[ry][rx]) {
        cy = ry;
        cx = rx;
        cang = (cang + 1) % 4;
        if (cy % 2 == 1 && cx % 2 == 1)
          tern++;
      } else
        cang = (cang + 1) % 4;
    }
    if (!ok)
      cout << "Impossible" << endl;
    else
      cout << tern << endl;
  }

  return 0;
}