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

#include <iostream>
using namespace std;

int main() {
  int i, j, k, n, f, c, t[2][100], a[2][100], b[2][100];
  while (cin >> n && n != 0) {
    for (i = 0; i < 2; i++)
      for (j = 0; j < n; j++) {
        cin >> t[i][j];
        a[i][j] = 999;
        b[i][j] = j;
      }
    for (i = 0; i < 2; i++) {
      for (j = 0; j < n; j++)
        if (t[i][j] == 2) {
          for (k = j; t[i][k] == 2; k++)
            b[i][k] = j - 1;
          j = k;
        }
      for (j = n - 1; j >= 0; j--) {
        if (t[i][j] == 1) {
          for (k = j; t[i][k] == 1; k--) {
            b[i][k] = j;
            if (k == 0)
              break;
          }
          j = k;
        }
      }
    }
    if (b[0][0] >= 0)
      a[0][b[0][0]] = 0;
    if (b[1][0] >= 0)
      a[1][b[1][0]] = 0;
    c = 0;
    f = 1;
    while (f == 1) {
      f = 0;
      for (i = 0; i < 2; i++)
        for (j = 0; j < n; j++) {
          if (a[i][j] == c) {
            if (b[0][0] == n - 1) {
              f = 100;
              j = i = n + 1;
              break;
            }
            if (b[1 - i][j] >= 0)
              if (a[1 - i][b[1 - i][j]] == 999) {
                a[1 - i][b[1 - i][j]] = c + 1;
                f = 1;
              }
            if (b[1 - i][j + 1] >= 0)
              if (a[1 - i][b[1 - i][j + 1]] == 999) {
                a[1 - i][b[1 - i][j + 1]] = c + 1;
                f = 1;
              }
            if (b[1 - i][j + 2] >= 0 && j + 2 < n)
              if (a[1 - i][b[1 - i][j + 2]] == 999) {
                a[1 - i][b[1 - i][j + 2]] = c + 1;
                f = 1;
              }
          }
        }
      if (f == 1)
        if (a[0][n - 1] < 999 || a[1][n - 1] < 999) {
          f = 100 + c + 1;
          break;
        }
      if (f == 0)
        break;
      c++;
    }
    if (f == 0)
      cout << "NA" << endl;
    else
      cout << f - 100 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int i, j, k, n, f, c, t[2][100], a[2][102], b[2][102];
  while (cin >> n && n != 0) {
    for (i = 0; i < 2; i++)
      for (j = 0; j < n; j++) {
        cin >> t[i][j];
        a[i][j] = 999;
        b[i][j] = j;
      }
    for (i = 0; i < 2; i++) {
      for (j = 0; j < n; j++) {
        if (t[i][j] == 2) {
          for (k = j; t[i][k] == 2; k++)
            b[i][k] = j - 1;
          j = k;
        }
      }
      for (j = n - 1; j >= 0; j--) {
        if (t[i][j] == 1) {
          for (k = j; t[i][k] == 1; k--) {
            b[i][k] = j;
            if (k == 0)
              break;
          }
          j = k;
        }
      }
    }
    a[0][b[0][0]] = a[1][b[1][0]] = c = 0;
    f = 1;
    while (f == 1) {
      f = 0;
      for (i = 0; i < 2; i++)
        for (j = 0; j < n - 1; j++) {
          if (a[i][j] == c) {
            if (a[1 - i][b[1 - i][j]] == 999) {
              a[1 - i][b[1 - i][j]] = c + 1;
              f = 1;
            }
            if (a[1 - i][b[1 - i][j + 1]] == 999) {
              a[1 - i][b[1 - i][j + 1]] = c + 1;
              f = 1;
            }
            if (a[1 - i][b[1 - i][j + 2]] == 999) {
              a[1 - i][b[1 - i][j + 2]] = c + 1;
              f = 1;
            }
            if (a[1 - i][n - 1] < 999) {
              f = 100 + c + 1;
              j = i = n + 1;
            }
          }
        }
      if (f == 0)
        break;
      c++;
    }
    if (f == 0)
      cout << "NA" << endl;
    else
      cout << f - 100 << endl;
  }
  return 0;
}