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

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
vector<int> ami[100][100];
int hi[1000];
int n, m, a, mh;

int kuji(int x, int h) {
  // cout << "start kuji()"<<endl;
  int t;
  int fl = 0;
  int i;
  // cout << "h = "<< h << endl;
  // cout << "x = " << x << endl;
  mh = -1;
  fl == 0;

  if (x > 0)
    for (i = 0; i < ami[x][x - 1].size(); i++) {
      if (h >= ami[x][x - 1][i] && ami[x][x - 1][i] > mh) {
        fl = 1;
        mh = ami[x][x - 1][i];
        t = i;
      }
    }

  if (x < n - 1) {
    for (i = 0; i < ami[x][x + 1].size(); i++) {
      if (h >= ami[x][x + 1][i] && ami[x][x + 1][i] > mh) {
        mh = ami[x][x + 1][i];
        fl = 2;
        t = i;
      }
    }
  }

  if (fl == 1) {
    ami[x][x - 1][t] = h + 1;
    ami[x - 1][x][t] = h + 1;
  } else if (fl == 2) {
    ami[x][x + 1][t] = h + 1;
    ami[x + 1][x][t] = h + 1;
  }
  if (mh == -1)
    return x;

  // printf("mh = %d\\n",mh);
  if (fl == 1)
    kuji(x - 1, mh);
  else if (fl == 2)
    kuji(x + 1, mh);
}

int main() {

  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;

    mh = -1;
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      if (h == 0 && p == 0 && q == 0)
        break;
      ami[p - 1][q - 1].push_back(h);
      ami[q - 1][p - 1].push_back(h);
      //    cout << ami[q-1][p-1].back() << endl;
      if (mh < h)
        mh = h;
    }

    int ans = 0;
    ans = kuji(a - 1, mh);

    cout << ans + 1 << endl;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        ami[i][j].clear();
        //	cout << ami[i][j].size() <<": i, j " <<i <<" "<< j << endl;
      }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
vector<int> ami[100][100];
int hi[1000];
int n, m, a, mh;

int kuji(int x, int h) {
  // cout << "start kuji()"<<endl;
  int t;
  int fl = 0;
  int i;
  // cout << "h = "<< h << endl;
  // cout << "x = " << x << endl;
  mh = -1;
  fl == 0;

  if (x > 0)
    for (i = 0; i < ami[x][x - 1].size(); i++) {
      if (h >= ami[x][x - 1][i] && ami[x][x - 1][i] > mh) {
        fl = 1;
        mh = ami[x][x - 1][i];
        t = i;
      }
    }

  if (x < n - 1) {
    for (i = 0; i < ami[x][x + 1].size(); i++) {
      if (h >= ami[x][x + 1][i] && ami[x][x + 1][i] > mh) {
        mh = ami[x][x + 1][i];
        fl = 2;
        t = i;
      }
    }
  }

  if (fl == 1) {
    ami[x][x - 1][t] = h + 1;
    ami[x - 1][x][t] = h + 1;
  } else if (fl == 2) {
    ami[x][x + 1][t] = h + 1;
    ami[x + 1][x][t] = h + 1;
  }
  if (mh == -1)
    return x;
  // printf("mh = %d\\n",mh);
  if (mh != x && fl == 1)
    kuji(x - 1, mh);
  else if (mh != x && fl == 2)
    kuji(x + 1, mh);
}

int main() {

  while (scanf("%d %d %d", &n, &m, &a) != EOF) {

    mh = -1;
    while (1) {
      int h, p, q;
      cin >> h >> p >> q;
      if (h == 0 && p == 0 && q == 0)
        break;
      ami[p - 1][q - 1].push_back(h);
      ami[q - 1][p - 1].push_back(h);
      //    cout << ami[q-1][p-1].back() << endl;
      if (mh < h)
        mh = h;
    }

    int ans = 0;
    ans = kuji(a - 1, mh);

    cout << ans + 1 << endl;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        ami[i][j].clear();
  }

  return 0;
}