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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int n, m, g, d;
int s[100];
bool l[100][100];
int works[100];
bool workl[100][100];

void shuffle() {
  for (int i = 0; i < d; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      if (workl[i][j])
        swap(works[j], works[j + 1]);
    }
  }
}

int main() {
  char buf[128];
  while (true) {
    cin.getline(buf, sizeof(buf));
    sscanf(buf, "%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < n; ++i)
      s[i] = i;

    cin.getline(buf, sizeof(buf));
    sscanf(buf, "%d", &m);
    cin.getline(buf, sizeof(buf));
    sscanf(buf, "%d", &g);
    cin.getline(buf, sizeof(buf));
    sscanf(buf, "%d", &d);
    --m;
    --g;

    for (int i = 0; i < d; ++i) {
      cin.getline(buf, sizeof(buf));
      for (int j = 0; buf[j] != '\\0'; ++j)
        l[i][j] = (buf[j] == '1');
    }

    memcpy(works, s, sizeof(works));
    memcpy(workl, l, sizeof(workl));
    shuffle();
    if (works[g] == m) {
      puts("0");
      continue;
    }

    int x, y;
    bool ok = false;
    for (y = 0; y < d; ++y) {
      for (x = 0; x < n - 1; ++x) {
        if (l[y][x] || (x != 0 && l[y][x - 1]) || (x != n - 2 && l[y][x + 1]))
          continue;

        memcpy(works, s, sizeof(works));
        memcpy(workl, l, sizeof(workl));
        workl[y][x] = true;

        shuffle();
        if (works[g] == m) {
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
    if (ok)
      printf("%d %d\\n", y + 1, x + 1);
    else
      puts("1");
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int n, m, g, d;
int s[9];
bool l[30][8];
int works[9];
bool workl[30][8];

void shuffle() {
  for (int i = 0; i < d; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      if (workl[i][j])
        swap(works[j], works[j + 1]);
    }
  }
}

int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < n; ++i)
      s[i] = i;

    scanf("%d", &m);
    scanf("%d", &g);
    scanf("%d", &d);
    --m;
    --g;

    for (int i = 0; i < d; ++i) {
      char buf[9];
      scanf(" %s", buf);
      for (int j = 0; j < n - 1; ++j)
        l[i][j] = buf[j] == '1';
    }

    memcpy(works, s, sizeof(works));
    memcpy(workl, l, sizeof(workl));
    shuffle();
    if (works[g] == m) {
      puts("0");
      continue;
    }

    int x, y;
    bool ok = false;
    for (y = 0; y < d; ++y) {
      for (x = 0; x < n - 1; ++x) {
        if (l[y][x] || (x != 0 && l[y][x - 1]) || (x != n - 2 && l[y][x + 1]))
          continue;

        memcpy(works, s, sizeof(works));
        memcpy(workl, l, sizeof(workl));
        workl[y][x] = true;

        shuffle();
        if (works[g] == m) {
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
    if (ok)
      printf("%d %d\\n", y + 1, x + 1);
    else
      puts("1");
  }

  return 0;
}