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

#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int solve(int H, int W, int K, int x1, int y1, int x2, int y2,
          vector<string> c) {
  int oo = 999999999;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  vector<vector<int>> D(H, vector<int>(W, oo));
  vector<vector<int>> F(H, vector<int>(W));
  vector<pair<int, int>> Q;
  D[x1][y1] = 0;
  F[x1][y1] = 0xf;
  Q.push_back(make_pair(x1, y1));
  for (int q = 0; q < (int)Q.size(); q++) {
    int fx = Q[q].first;
    int fy = Q[q].second;
    for (int d = 0; d < 4; d++)
      if (F[fx][fy] >> d & 1) {
        for (int k = 1; k <= K; k++) {
          int tx = fx + dx[d] * k;
          int ty = fy + dy[d] * k;
          if (tx < 0 || H <= tx || ty < 0 || W <= ty || c[tx][ty] == '@')
            break;
          if (D[fx][fy] + 1 < D[tx][ty]) {
            D[tx][ty] = D[fx][fy] + 1;
            F[tx][ty] = 0xf;
            Q.push_back(make_pair(tx, ty));
          }
          if (D[fx][fy] + 1 == D[tx][ty]) {
            F[tx][ty] &= ~(1 << ((d + 2) % 4));
            if (k < K)
              F[tx][ty] &= ~(1 << d);
          }
        }
      }
  }
  return D[x2][y2] < oo ? D[x2][y2] : -1;
}

int naive(int H, int W, int K, int x1, int y1, int x2, int y2,
          vector<string> c) {
  int oo = 999999999;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  vector<vector<int>> D(H, vector<int>(W, oo));
  vector<pair<int, int>> Q;
  D[x1][y1] = 0;
  Q.push_back(make_pair(x1, y1));
  for (int q = 0; q < (int)Q.size(); q++) {
    int fx = Q[q].first;
    int fy = Q[q].second;
    for (int d = 0; d < 4; d++)
      for (int k = 1; k <= K; k++) {
        int tx = fx + dx[d] * k;
        int ty = fy + dy[d] * k;
        if (tx < 0 || H <= tx || ty < 0 || W <= ty || c[tx][ty] == '@')
          break;
        if (D[fx][fy] + 1 < D[tx][ty]) {
          D[tx][ty] = D[fx][fy] + 1;
          Q.push_back(make_pair(tx, ty));
        }
      }
  }
  return D[x2][y2] < oo ? D[x2][y2] : -1;
}

int main() {
  //*
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> c(H);
  for (string &t : c)
    cin >> t;

  int ans = solve(H, W, K, x1, y1, x2, y2, c);
  cout << ans << endl;

  /*/
  while (true)
  {
      int H = 16;
      int W = 16;
      int K = rand()%20+1;
      int x1 = rand()%H;
      int y1 = rand()%W;
      int x2 = rand()%H;
      int y2 = rand()%W;
      if (x1!=x2 || y1!=y2)
      {
          vector<string> c(H, string(W, '.'));
          for (int i=0; i<16; i++)
              c[rand()%H][rand()%W] = '@';
          c[x1][y1] = c[x2][y2] = '.';

          int a1 = solve(H, W, K, x1, y2, x2, y2, c);
          int a2 = solve(H, W, K, x1, y2, x2, y2, c);
          if (a1!=a2)
          {
              cout<<H<<" "<<W<<" "<<K<<endl;
              cout<<x1+1<<" "<<y1+1<<" "<<x2+1<<" "<<y2+1<<endl;
              for (string t: c)
                  cout<<t<<endl;
              cout<<a1<<endl;
              cout<<a2<<endl;
          }
      }
  }
  //*/
}


Error: 255

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int solve(int H, int W, int K, int x1, int y1, int x2, int y2,
          vector<string> c) {
  int oo = 999999999;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  vector<vector<int>> D(H, vector<int>(W, oo));
  vector<vector<int>> F(H, vector<int>(W));
  vector<pair<int, int>> Q;
  D[x1][y1] = 0;
  F[x1][y1] = 0xf;
  Q.push_back(make_pair(x1, y1));
  for (int q = 0; q < (int)Q.size(); q++) {
    int fx = Q[q].first;
    int fy = Q[q].second;
    for (int d = 0; d < 4; d++)
      if (F[fx][fy] >> d & 1) {
        for (int k = 1; k <= K; k++) {
          int tx = fx + dx[d] * k;
          int ty = fy + dy[d] * k;
          if (tx < 0 || H <= tx || ty < 0 || W <= ty || c[tx][ty] == '@')
            break;
          if (D[fx][fy] + 1 < D[tx][ty]) {
            D[tx][ty] = D[fx][fy] + 1;
            F[tx][ty] = 0xf;
            Q.push_back(make_pair(tx, ty));
          }
          F[tx][ty] &= ~(1 << ((d + 2) % 4));
          if (k < K)
            F[tx][ty] &= ~(1 << d);
        }
      }
  }
  return D[x2][y2] < oo ? D[x2][y2] : -1;
}

int naive(int H, int W, int K, int x1, int y1, int x2, int y2,
          vector<string> c) {
  int oo = 999999999;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  vector<vector<int>> D(H, vector<int>(W, oo));
  vector<pair<int, int>> Q;
  D[x1][y1] = 0;
  Q.push_back(make_pair(x1, y1));
  for (int q = 0; q < (int)Q.size(); q++) {
    int fx = Q[q].first;
    int fy = Q[q].second;
    for (int d = 0; d < 4; d++)
      for (int k = 1; k <= K; k++) {
        int tx = fx + dx[d] * k;
        int ty = fy + dy[d] * k;
        if (tx < 0 || H <= tx || ty < 0 || W <= ty || c[tx][ty] == '@')
          break;
        if (D[fx][fy] + 1 < D[tx][ty]) {
          D[tx][ty] = D[fx][fy] + 1;
          Q.push_back(make_pair(tx, ty));
        }
      }
  }
  return D[x2][y2] < oo ? D[x2][y2] : -1;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> c(H);
  for (string &t : c)
    cin >> t;

  int ans = solve(H, W, K, x1, y1, x2, y2, c);
  if (ans >= 0)
    return -1;
  cout << ans << endl;

  /*
  while (true)
  {
      int H = 8;
      int W = 8;
      int K = rand()%8+1;
      int x1 = rand()%H;
      int y1 = rand()%W;
      int x2 = rand()%H;
      int y2 = rand()%W;
      if (x1!=x2 || y1!=y2)
      {
          vector<string> c(H, string(W, '.'));
          for (int i=0; i<16; i++)
              c[rand()%H][rand()%W] = '@';
          c[x1][y1] = c[x2][y2] = '.';

          int a1 = solve(H, W, K, x1, y2, x2, y2, c);
          int a2 = solve(H, W, K, x1, y2, x2, y2, c);
          if (a1!=a2)
          {
              cout<<H<<" "<<W<<" "<<K<<endl;
              cout<<x1+1<<" "<<y1+1<<" "<<x2+1<<" "<<y2+1<<endl;
              for (string t: c)
                  cout<<t<<endl;
              cout<<a1<<endl;
              cout<<a2<<endl;
          }
      }
  }
  */
}