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

#include "bits/stdc++.h"

#pragma warning(disable : 4996)
using namespace std;
using ld = long double;

vector<int> solve(int H, int W, int y, int x) {
  vector<int> anss;

  bool h_swap = false;
  bool hw_swap = false;
  bool w_swap = false;
  bool rot = false;
  if (y % 2 == 0) {
    y = H - 1 - y;
    h_swap = true;
  } else {
    h_swap = false;
  }
  if (x % 2 == 0) {
    x = W - x - 1;
    w_swap = true;
  } else {
    w_swap = false;
  }
  if (y == H - 1 && x == W - 1) {
    for (int k = 0; k < (H + 1) / 2; ++k) {
      if (k) {
        anss.push_back(3);
      }
      for (int n = 0; n < W - 1; ++n) {
        anss.push_back(0);
      }
      anss.push_back(3);
      for (int n = 0; n < W - 1; ++n) {
        anss.push_back(2);
      }
    }

  } else {
    if (x == W - 1) {
      hw_swap = true;
      swap(x, y);
      swap(H, W);
    } else {
      hw_swap = false;
    }
    int l = x;
    int r = W - 1 - x;

    int u = y;
    int d = H - 1 - y;

    for (int k = 0; k < (u + 1) / 2; ++k) {
      if (k) {
        anss.push_back(3);
      }
      for (int n = 0; n < l; ++n) {
        anss.push_back(0);
      }
      anss.push_back(3);
      for (int n = 0; n < l; ++n) {
        anss.push_back(2);
      }
    }
    anss.push_back(2);
    for (int k = 0; k < H / 2; ++k) {
      if (k)
        anss.push_back(1);
      for (int n = 0; n < r - 1; ++n) {
        anss.push_back(2);
      }
      anss.push_back(1);
      for (int n = 0; n < r - 1; ++n) {
        anss.push_back(0);
      }
    }

    if (d) {
      anss.push_back(0);
      for (int k = 0; k < (d + 1) / 2; ++k) {
        if (k) {
          anss.push_back(3);
        }
        for (int n = 0; n < l; ++n) {
          anss.push_back(0);
        }
        anss.push_back(3);
        for (int n = 0; n < l; ++n) {
          anss.push_back(2);
        }
      }
    }
  }

  for (auto &ans : anss) {
    if (hw_swap) {
      ans = 3 - ans;
    }
    if (h_swap && ans % 2 == 1) {
      ans = 4 - ans;
    }
    if (w_swap && ans % 2 == 0) {
      ans = 2 - ans;
    }
  }
  assert(anss.size() == H * W - 1);
  return anss;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> field(H);

  int sx, sy;
  int num = 0;
  for (int i = 0; i < H; ++i) {
    cin >> field[i];
    for (int j = 0; j < W; ++j) {
      if (field[i][j] == '@') {
        sx = j;
        sy = i;
      } else if (field[i][j] == '*') {
        num++;
      }
    }
  }

  vector<int> move_ways =
      solve((H + 1) / 2, (W + 1) / 2, (sy + 1) / 2, (sx + 1) / 2);

  int nx = sx;
  int ny = sy;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  for (auto m_way : move_ways) {
    for (int ti = 0; ti < 2; ++ti) {
      nx += dx[m_way];
      ny += dy[m_way];

      if (field[ny][nx] == '.') {
        field[ny][nx] = '#';
      } else {
        num--;
        if (num == 0)
          break;
      }
    }
    if (num == 0)
      break;
  }

  for (int i = 0; i < H; ++i) {
    cout << field[i] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"

#pragma warning(disable : 4996)
using namespace std;
using ld = long double;

vector<int> solve(int H, int W, int y, int x) {
  vector<int> anss;

  bool h_swap = false;
  bool hw_swap = false;
  bool w_swap = false;
  bool rot = false;
  if (y % 2 == 0) {
    y = H - 1 - y;
    h_swap = true;
  } else {
    h_swap = false;
  }
  if (x % 2 == 0) {
    x = W - x - 1;
    w_swap = true;
  } else {
    w_swap = false;
  }
  if (x == H - 1 && y == W - 1) {
    for (int k = 0; k < (H + 1) / 2; ++k) {
      if (k) {
        anss.push_back(3);
      }
      for (int n = 0; n < W - 1; ++n) {
        anss.push_back(0);
      }
      anss.push_back(3);
      for (int n = 0; n < W - 1; ++n) {
        anss.push_back(2);
      }
    }

  } else {
    if (x == W - 1) {
      hw_swap = true;
      swap(x, y);
      swap(H, W);
    } else {
      hw_swap = false;
    }
    int l = x;
    int r = W - 1 - x;

    int u = y;
    int d = H - 1 - y;

    for (int k = 0; k < (u + 1) / 2; ++k) {
      if (k) {
        anss.push_back(3);
      }
      for (int n = 0; n < l; ++n) {
        anss.push_back(0);
      }
      anss.push_back(3);
      for (int n = 0; n < l; ++n) {
        anss.push_back(2);
      }
    }
    anss.push_back(2);
    for (int k = 0; k < H / 2; ++k) {
      if (k)
        anss.push_back(1);
      for (int n = 0; n < r - 1; ++n) {
        anss.push_back(2);
      }
      anss.push_back(1);
      for (int n = 0; n < r - 1; ++n) {
        anss.push_back(0);
      }
    }

    if (d) {
      anss.push_back(0);
      for (int k = 0; k < (d + 1) / 2; ++k) {
        if (k) {
          anss.push_back(3);
        }
        for (int n = 0; n < l; ++n) {
          anss.push_back(0);
        }
        anss.push_back(3);
        for (int n = 0; n < l; ++n) {
          anss.push_back(2);
        }
      }
    }
  }

  for (auto &ans : anss) {
    if (hw_swap) {
      ans = 3 - ans;
    }
    if (h_swap && ans % 2 == 1) {
      ans = 4 - ans;
    }
    if (w_swap && ans % 2 == 0) {
      ans = 2 - ans;
    }
  }
  assert(anss.size() == H * W - 1);
  return anss;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> field(H);

  int sx, sy;
  int num = 0;
  for (int i = 0; i < H; ++i) {
    cin >> field[i];
    for (int j = 0; j < W; ++j) {
      if (field[i][j] == '@') {
        sx = j;
        sy = i;
      } else if (field[i][j] == '*') {
        num++;
      }
    }
  }

  vector<int> move_ways =
      solve((H + 1) / 2, (W + 1) / 2, (sy + 1) / 2, (sx + 1) / 2);

  int nx = sx;
  int ny = sy;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  for (auto m_way : move_ways) {
    for (int ti = 0; ti < 2; ++ti) {
      nx += dx[m_way];
      ny += dy[m_way];

      if (field[ny][nx] == '.') {
        field[ny][nx] = '#';
      } else {
        num--;
        if (num == 0)
          break;
      }
    }
    if (num == 0)
      break;
  }

  for (int i = 0; i < H; ++i) {
    cout << field[i] << endl;
  }
  return 0;
}