Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
LL Abs(LL x) { return x < 0 ? -x : x; }
int n, m;
int d[40];
int x[1005], y[1005];
bool judge(LL x, LL y, int t) { return Abs(x) + Abs(y) < d[t + 1]; }
void print(LL x, LL y, int step) {
  if (step == -1) {
    return;
  }
  if (judge(x + d[step], y, step - 1)) {
    print(x + d[step], y, step - 1);
    printf("L");
    return;
  }
  if (judge(x - d[step], y, step - 1)) {
    print(x - d[step], y, step - 1);
    printf("R");
    return;
  }
  if (judge(x, y + d[step], step - 1)) {
    print(x, y + d[step], step - 1);
    printf("D");
    return;
  }
  if (judge(x, y - d[step], step - 1)) {
    print(x, y - d[step], step - 1);
    printf("U");
    return;
  }
  assert(0);
}
bool solve() {
  int r = (x[1] + y[1]) & 1;
  for (int i = 2; i <= n; ++i) {
    if (((x[i] + y[i]) & 1) != r) {
      return false;
    }
  }
  m = 3;
  d[0] = 1;
  for (int i = 1; i < m; ++i) {
    d[i] = d[i - 1] << 1;
  }
  d[m] = 1;
  if (r == 1) {
    printf("%d\n", m);
    for (int i = 0; i < m; ++i) {
      if (i)
        printf(" ");
      printf("%d", d[i]);
    }
    printf("\n");
  } else {
    printf("%d\n", m + 1);
    printf("1");
    for (int i = 0; i < m; ++i) {
      printf(" %d", d[i]);
    }
    printf("\n");
    for (int i = 1; i <= n; ++i) {
      ++x[i];
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (r == 0) {
      printf("L");
    }
    print(x[i], y[i], m - 1);
    printf("\n");
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &x[i], &y[i]);
  }
  if (!solve()) {
    printf("-1\n");
  }
  return 0;
}

Fixed submission:
#include <cassert>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
LL Abs(LL x) { return x < 0 ? -x : x; }
int n, m;
int d[40];
int x[1005], y[1005];
bool judge(LL x, LL y, int t) { return Abs(x) + Abs(y) < d[t + 1]; }
void print(LL x, LL y, int step) {
  if (step == -1) {
    return;
  }
  if (judge(x + d[step], y, step - 1)) {
    print(x + d[step], y, step - 1);
    printf("L");
    return;
  }
  if (judge(x - d[step], y, step - 1)) {
    print(x - d[step], y, step - 1);
    printf("R");
    return;
  }
  if (judge(x, y + d[step], step - 1)) {
    print(x, y + d[step], step - 1);
    printf("D");
    return;
  }
  if (judge(x, y - d[step], step - 1)) {
    print(x, y - d[step], step - 1);
    printf("U");
    return;
  }
  assert(0);
}
bool solve() {
  int r = (x[1] + y[1]) & 1;
  for (int i = 2; i <= n; ++i) {
    if (((x[i] + y[i]) & 1) != r) {
      return false;
    }
  }
  m = 31;
  d[0] = 1;
  for (int i = 1; i < m; ++i) {
    d[i] = d[i - 1] << 1;
  }
  d[m] = 1;
  if (r == 1) {
    printf("%d\n", m);
    for (int i = 0; i < m; ++i) {
      if (i)
        printf(" ");
      printf("%d", d[i]);
    }
    printf("\n");
  } else {
    printf("%d\n", m + 1);
    printf("1");
    for (int i = 0; i < m; ++i) {
      printf(" %d", d[i]);
    }
    printf("\n");
    for (int i = 1; i <= n; ++i) {
      ++x[i];
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (r == 0) {
      printf("L");
    }
    print(x[i], y[i], m - 1);
    printf("\n");
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &x[i], &y[i]);
  }
  if (!solve()) {
    printf("-1\n");
  }
  return 0;
}