Problem p03836 (C++)

Buggy submission (status: Time Limit Exceeded):
#define MYDEBUG
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

#ifdef MYDEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
using namespace std;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last)
      out << ",";
  }
  out << "]";
  return out;
}

int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
char dw[4] = {'U', 'D', 'L', 'R'};
const int MAXX = 2000 + 1;
bool grid[MAXX][MAXX];
bool bann[MAXX][MAXX];

string bfs(int sx, int sy, int tx, int ty) {
  queue<int> nextX;
  queue<int> nextY;
  queue<string> nextW;
  nextX.push(sx);
  nextY.push(sy);
  nextW.push("");
  while (!nextX.empty()) {
    int x = nextX.front();
    int y = nextY.front();
    string way = nextW.front();
    nextX.pop();
    nextY.pop();
    nextW.pop();
    grid[x][y] = true;
    if (x == tx && y == ty) {
      x = sx;
      y = sy;
      for (int i = 0; i < way.size(); ++i) {
        if (way[i] == 'U')
          y++;
        else if (way[i] == 'D')
          y--;
        else if (way[i] == 'L')
          x--;
        else if (way[i] == 'R')
          x++;
        bann[x][y] = true;
      }
      bann[sx][sy] = bann[tx][ty] = false;
      return way;
    }
    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (0 <= nx && nx < MAXX && 0 <= ny && ny < MAXX &&
          grid[nx][ny] == false && bann[nx][ny] == false) {
        nextX.push(nx);
        nextY.push(ny);
        nextW.push(way + dw[k]);
        grid[sx][sy] = grid[tx][ty] = false;
      }
    }
    grid[x][y] = false;
  }
  return "can't";
}

void solve() {
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx += 1000;
  sy += 1000;
  tx += 1000;
  ty += 1000;
  for (int i = 0; i < MAXX; ++i) {
    for (int k = 0; k < MAXX; ++k) {
      grid[i][k] = false;
      bann[i][k] = false;
    }
  }
  string way = "";
  way += bfs(sx, sy, tx, ty);
  way += bfs(tx, ty, sx, sy);
  way += bfs(sx, sy, tx, ty);
  way += bfs(tx, ty, sx, sy);
  cout << way << endl;
}
int main() {
  solve();
  return 0;
}


Fixed submission:
#define MYDEBUG
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

#ifdef MYDEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
using namespace std;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last)
      out << ",";
  }
  out << "]";
  return out;
}

int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
char dw[4] = {'U', 'D', 'L', 'R'};
const int MAXX = 2000 + 1;
bool grid[MAXX][MAXX];
bool bann[MAXX][MAXX];

string bfs(int sx, int sy, int tx, int ty) {
  queue<int> nextX;
  queue<int> nextY;
  queue<string> nextW;
  nextX.push(sx);
  nextY.push(sy);
  nextW.push("");
  while (!nextX.empty()) {
    int x = nextX.front();
    int y = nextY.front();
    string way = nextW.front();
    nextX.pop();
    nextY.pop();
    nextW.pop();
    grid[x][y] = true;
    if (x == tx && y == ty) {
      x = sx;
      y = sy;
      for (int i = 0; i < way.size(); ++i) {
        if (way[i] == 'U')
          y++;
        else if (way[i] == 'D')
          y--;
        else if (way[i] == 'L')
          x--;
        else if (way[i] == 'R')
          x++;
        bann[x][y] = true;
      }
      bann[sx][sy] = bann[tx][ty] = false;
      return way;
    }
    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (0 <= nx && nx < MAXX && 0 <= ny && ny < MAXX &&
          grid[nx][ny] == false && bann[nx][ny] == false) {
        nextX.push(nx);
        nextY.push(ny);
        nextW.push(way + dw[k]);
        grid[sx][sy] = grid[tx][ty] = false;
      }
    }
    grid[x][y] = false;
  }
  return "can't";
}

void solve() {
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  int dx = tx - sx, dy = ty - sy;
  cout << string(dy, 'U') << string(dx, 'R');
  cout << string(dy, 'D') << string(dx, 'L');
  cout << 'L' << string(dy + 1, 'U') << string(dx + 1, 'R') << 'D';
  cout << 'R' << string(dy + 1, 'D') << string(dx + 1, 'L') << 'U';
}

void nogood_solve() {
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx += 1000;
  sy += 1000;
  tx += 1000;
  ty += 1000;
  for (int i = 0; i < MAXX; ++i) {
    for (int k = 0; k < MAXX; ++k) {
      grid[i][k] = false;
      bann[i][k] = false;
    }
  }
  string way = "";
  way += bfs(sx, sy, tx, ty);
  way += bfs(tx, ty, sx, sy);
  way += bfs(sx, sy, tx, ty);
  way += bfs(tx, ty, sx, sy);
  cout << way << endl;
}
int main() {
  solve();
  return 0;
}