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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

struct ans {
  int n;
  ans *next;
};

int main() {
  ans *start = new ans();
  start->next = NULL;
  ans *end = start;
  ans *buf;
  char s[20];
  char dir;
  char *ps;
  int **juel;
  int x = 10, y = 10;
  int state[21][21];
  int i, j, n, m, size;
  while (fgets(s, sizeof(s), stdin)) {
    size = atoi(s);
    if (size == 0)
      break;
    memset(state, 0x00, sizeof(state));
    x = 10;
    y = 10;
    juel = new int *[size];
    for (i = 0; i < size; i++) {
      juel[i] = new int[2];
      fgets(s, sizeof(s), stdin);
      ps = strtok(s, " ");
      juel[i][0] = atoi(ps);
      ps = strtok(NULL, " ");
      juel[i][1] = atoi(ps);
    }
    fgets(s, sizeof(s), stdin);
    n = atoi(s);
    for (i = 0; i < n; i++) {
      fgets(s, sizeof(s), stdin);
      ps = strtok(s, " ");
      dir = ps[0];
      ps = strtok(NULL, " ");
      m = atoi(ps);
      if (dir == 'N') {
        for (j = 1; j <= m; j++)
          state[x][y + j] = 1;
        y += m;
      } else if (dir == 'S') {
        for (j = 1; j <= m; j++)
          state[x][y - j] = 1;
        y -= m;
      } else if (dir == 'E') {
        for (j = 1; j <= m; j++)
          state[x + j][y] = 1;
        x += m;
      } else if (dir == 'W') {
        for (j = 1; j <= m; j++)
          state[x - j][y] = 1;
        x -= m;
      }
    }
    for (i = 0; i < size; i++) {
      if (state[juel[i][0]][juel[i][1]] == 0)
        break;
    }
    if (i == size)
      n = 0;
    else
      n = 1;
    buf = new ans();
    buf->n = n;
    buf->next = NULL;
    end->next = buf;
    end = buf;
    for (i = 0; i < size; i++)
      delete[] juel[i];
    delete[] juel;
  }
  buf = start->next;
  while (buf != NULL) {
    if (buf->n == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
    end = buf;
    buf = buf->next;
    delete end;
  }
  return 0;
}

Error: 1

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

struct ans {
  int n;
  ans *next;
};

int main() {
  ans *start = new ans();
  start->next = NULL;
  ans *end = start, *buf;
  char s[20];
  char dir;
  char *ps;
  int **juel;
  int x = 10, y = 10;
  int state[21][21];
  int i, j, n, m, size;
  while (fgets(s, sizeof(s), stdin)) {
    size = atoi(s);
    if (size == 0)
      break;
    memset(state, 0, sizeof(state));
    juel = new int *[size];
    for (i = 0; i < size; i++) {
      juel[i] = new int[2];
      fgets(s, sizeof(s), stdin);
      ps = strtok(s, " ");
      juel[i][0] = atoi(ps);
      ps = strtok(NULL, " ");
      juel[i][1] = atoi(ps);
    }
    fgets(s, sizeof(s), stdin);
    n = atoi(s);
    for (i = 0; i < n; i++) {
      fgets(s, sizeof(s), stdin);
      ps = strtok(s, " ");
      dir = ps[0];
      ps = strtok(NULL, " ");
      m = atoi(ps);
      if (dir == 'N') {
        for (j = 1; j <= m; j++)
          state[x][y + j] = 1;
        y += m;
      } else if (dir == 'S') {
        for (j = 1; j <= m; j++)
          state[x][y - j] = 1;
        y -= m;
        break;
      } else if (dir == 'E') {
        for (j = 1; j <= m; j++)
          state[x + j][y] = 1;
        x += m;
      } else if (dir == 'W') {
        for (j = 1; j <= m; j++)
          state[x - j][y] = 1;
        x -= m;
      }
    }
    for (i = 0; i < size; i++) {
      if (state[juel[i][0]][juel[i][1]] == 0)
        break;
    }
    if (i == size)
      n = 0;
    else
      n = 1;
    buf = new ans();
    buf->n = n;
    buf->next = NULL;
    end->next = buf;
    end = buf;
    for (i = 0; i < size; i++)
      delete[] juel[i];
    delete[] juel;
  }
  buf = start->next;
  while (buf != NULL) {
    if (buf->n == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
    end = buf;
    buf = buf->next;
    // delete end;
  }
  return 1;
}