alexjercan/bugnet huggingface dataset:
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {

  long n, d;

  while (cin >> n, n) {

    while (n >= 13) {

      d = 0;

      if (n % 2 == 1) {
        for (int i = 3; i * i <= n; i += 2) {
          if (d == 1)
            break;

          if (i * i <= n - 8) {
            if ((n - 8) % i == 0)
              d = 1;
          }
          if (i * i <= n - 6) {
            if ((n - 6) % i == 0)
              d = 1;
          }
          if (i * i <= n - 2) {
            if ((n - 2) % i == 0)
              d = 1;
          }
          if (n % i == 0)
            d = 1;
        }
        n -= 2;
      }

      else {
        n--;
        continue;
      }

      if (!d) {
        cout << n + 2 << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {

  long n;

  while (cin >> n, n) {

    int d = 0;
    n++;

    while (n >= 13) {
      n--;
      d = 0;

      if (n % 2 == 1) {
        for (int i = 3; i * i < n; i += 2) {
          if (n % i == 0)
            d = 1;
        }

        for (int i = 3; i * i < n - 2; i++) {
          if ((n - 2) % i == 0)
            d = 1;
        }

        for (int i = 3; i * i < n - 6; i++) {
          if ((n - 6) % i == 0)
            d = 1;
        }

        for (int i = 3; i * i < n - 8; i++) {
          if ((n - 8) % i == 0)
            d = 1;
        }
      }

      else
        continue;

      if (!d) {
        cout << n << endl;
        break;
      }
    }
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

char p[10000001];

void sieve() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;

  for (int i = 2; i <= 10000000; ++i)
    if (p[i])
      for (int j = i * 2; j <= 10000000; j += i)
        p[j] = 0;

  return;
}

int main() {
  sieve();

  int n;
  while (std::cin >> n, n) {
    while (true) {
      if (p[n] && p[n - 2] && p[n - 6] && p[n - 8]) {
        std::cout << n << std::endl;
        break;
      }

      --n;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

bool isprime(int n) {
  for (int i = 2; i * i <= n; ++i)
    if (n % i == 0)
      return false;

  return n != 1;
}

int main() {
  int n;
  while (std::cin >> n, n) {
    int a[4];
    int p = 0;

    while (true) {
      while (!isprime(n))
        --n;

      if (!p || a[p - 1] - n == (p == 2 ? 4 : 2))
        a[p++] = n--;
      else
        p = 0, a[p++] = n--;

      if (p == 4)
        break;
    }

    std::cout << a[0] << std::endl;
  }

  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

#define N 10000000
#define SQRT_N 3163

bool tmp[N];

void makePrimeList() {
  tmp[0] = tmp[1] = false;

  for (int i = 2; i < N; i++)
    tmp[i] = true;

  for (int i = 2; i < SQRT_N; i++)
    if (tmp[i] == true)
      for (int j = 2; j * i < N; j++)
        tmp[j * i] = false;
}

bool IsPrime(int n) { return tmp[n]; }

int retqp(int n) {
  if (n % 2 == 0)
    n--;
  while (!(IsPrime(n - 8) && IsPrime(n - 6) && IsPrime(n - 2) && IsPrime(n)))
    n -= 2;
  return n;
}

int main(void) {
  makePrimeList();
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d\\n", retqp(n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

bool IsPrime(int n) {
  static vector<int> notPrime;
  static vector<int> Prime;
  if (n < 2)
    return false;
  else if (n == 2)
    return true;

  if (n % 2 == 0)
    return false;

  if (binary_search(notPrime.begin(), notPrime.end(), n))
    return false;
  if (binary_search(Prime.begin(), Prime.end(), n))
    return true;

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      notPrime.push_back(n);
      sort(notPrime.begin(), notPrime.end());
      return false;
    }
  }
  Prime.push_back(n);
  sort(Prime.begin(), Prime.end());
  return true;
}

int retqp(int n) {
  if (n % 2 == 0)
    n--;
  while (!(IsPrime(n - 8) && IsPrime(n - 6) && IsPrime(n - 2) && IsPrime(n)))
    n -= 2;
  return n;
}

int main(void) {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d\\n", retqp(n));
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

bool isPrime(int a) {
  bool ans = true;
  for (int i = 3; i * i <= a; i += 2) {
    if (a % i == 0 || (a - 2) % i == 0 || (a - 6) % i == 0 ||
        (a - 8) % i == 0) {
      ans = false;
      break;
    }
  }
  return ans;
}

int n;

int main() {
  while (true) {
    std::cin >> n;
    if (!n)
      return 0;
    if (n % 2 == 0)
      n--;
    for (; n > 12; n -= 2) {
      if (isPrime(n)) {
        std::cout << n << std::endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>

bool isPrime(int a) {
  bool ans = true;
  if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0 || a % 7 == 0)
    ans = false;
  else {
    for (int i = 17; i * i <= a; i += 2) {
      if (a % i == 0) {
        ans = false;
        break;
      }
    }
  }
  return ans;
}

int n;

int main() {
  while (true) {
    std::cin >> n;
    if (!n)
      return 0;
    if (n % 2 == 0)
      n--;
    for (; n > 12; n -= 2) {
      if (n == 13)
        std::cout << 13 << std::endl;
      else if (isPrime(n) && isPrime(n - 2) && isPrime(n - 6) &&
               isPrime(n - 8)) {
        std::cout << n << std::endl;
        break;
      }
    }
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 10000000
bool is_prime[MAX_N + 1];
void sieve(int n) {
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {
  int n;
  sieve(MAX_N);
  vector<int> v;
  for (int i = 13; i <= MAX_N; i++) {
    if (is_prime[i - 8] && is_prime[i - 6] && is_prime[i - 2] && is_prime[i])
      v.push_back(i);
  }
  while (cin >> n, n) {
    int ans = 13;
    cout << *(upper_bound(v.begin(), v.end(), n) - 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 10000000
int prime[MAX_N]; // i番目の素数
bool is_prime[MAX_N + 1];
int sieve(int n) {
  int p = 0;
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}

int main() {
  int n;
  sieve(MAX_N);
  while (cin >> n, n) {
    int ans = 13;
    for (int i = 13; i <= n; i++) {
      if (is_prime[i - 8] && is_prime[i - 6] && is_prime[i - 2] && is_prime[i])
        ans = i;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return n != 1;
}
int solve(int n) {
  for (int i = n; i >= 0; i--) {
    if (i == 19)
      return i;
    if (i == 13)
      return i;
    if (i % 2 != 0 && (i - 2) % 2 != 0 && (i - 6) % 2 != 0 &&
        (i - 8) % 2 != 0) {
      if (i % 3 != 0 && (i - 2) % 3 != 0 && (i - 6) % 3 != 0 &&
          (i - 8) % 3 != 0) {
        if (i % 5 != 0 && (i - 2) % 5 != 0 && (i - 6) % 5 != 0 &&
            (i - 8) % 5 != 0) {
          if (i % 7 != 0 && (i - 2) % 7 != 0 && (i - 6) % 7 != 0 &&
              (i - 8) % 7 != 0) {
            if (i % 11 != 0 && (i - 2) % 11 != 0 && (i - 6) % 11 != 0 &&
                (i - 8) % 11 != 0) {
              if (is_prime(i)) {
                if (is_prime(i - 2)) {
                  if (is_prime(i - 6)) {
                    if (is_prime(i - 8)) {
                      return i;
                    }
                    i -= 8;
                    continue;
                  }
                  i -= 6;
                  continue;
                }
                i -= 2;
                continue;
              }
              i--;
              continue;
            }
            i--;
            continue;
          }
          i--;
          continue;
        }
        i--;
        continue;
      }
      i--;
      continue;
    }
  }
  return 0;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    cout << solve(n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return n != 1;
}
int solve(int n) {
  for (int i = n; i >= 0; i--) {
    if (i % 2 != 0 && (i - 2) % 2 != 0 && (i - 6) % 2 != 0 &&
        (i - 8) % 2 != 0) {
      if (i % 3 != 0 && (i - 2) % 3 != 0 && (i - 6) % 3 != 0 &&
          (i - 8) % 3 != 0) {
        if (is_prime(i)) {
          if (is_prime(i - 2)) {
            if (is_prime(i - 6)) {
              if (is_prime(i - 8)) {
                return i;
              }
              i -= 8;
              continue;
            }
            i -= 6;
            continue;
          }
          i -= 2;
          continue;
        }
        i--;
        continue;
      }
      i--;
      continue;
    }
    cout << i << endl;
  }
  return 0;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    cout << solve(n) << endl;
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  bool is_prime[10000001];

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i < 10000001; i++) {
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 100000011; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 10000001; j += i) {
        is_prime[j] = false;
      }
    }
  }

  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    for (int i = n; i > 12; i--) {
      if (is_prime[i] && is_prime[i - 2] && is_prime[i - 6] &&
          is_prime[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  bool is_prime[50001];

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i < 50001; i++) {
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 50001; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 50001; j += i) {
        is_prime[j] = false;
      }
    }
  }

  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    for (int i = n; i > 12; i--) {
      if (is_prime[i] && is_prime[i - 2] && is_prime[i - 6] &&
          is_prime[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }

  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
#define MAX 10000000
using namespace std;
int main() {
  int n;
  vector<char> a;
  vector<int> box;
  for (int i = 0; i <= MAX; i++)
    a.push_back('1');
  a[0] = '0';
  a[1] = '0';
  for (int i = 2; i <= MAX; i++)
    if (a[i] == '1') {
      if (i >= 13 && (a[i] == '1' && a[i - 2] == '1' && a[i - 6] == '1' &&
                      a[i - 8] == '1')) {
        box.push_back(i);
      }
      for (int j = i * 2; j <= MAX; j += i)
        a[j] = '0';
    }
  while (cin >> n, n) {
    int i;
    for (i = 0; i < (int)box.size() && box[i] <= n; i++)
      ;
    cout << box[i - 1] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
#define MAX 10000000
using namespace std;
int main() {
  int n;
  vector<int> a, box;
  for (int i = 0; i <= MAX; i++)
    a.push_back(1);
  a[0] = 0;
  a[1] = 0;
  for (int i = 2; i <= MAX; i++)
    if (a[i]) {
      if (i >= 13 &&
          (a[i] == 1 && a[i - 2] == 1 && a[i - 6] == 1 && a[i - 8] == 1)) {
        box.push_back(i);
      }
      for (int j = i * 2; j <= MAX; j += i)
        a[j] = 0;
    }
  while (cin >> n, n) {
    int i;
    for (i = 0; i < (int)box.size() && box[i] <= n; i++)
      ;
    cout << box[i - 1] << endl;
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

static const int MAX_N = 10000001;
bool P[MAX_N];

void prime();

int main(int argc, char **argv) {
  prime();

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = n; i >= 13; i--) {
      if (i % 2 == 1) {
        if (P[i] && P[i - 2] && P[i - 6] && P[i - 8]) {
          cout << i << endl;
          break;
        }
      }
    }
  }
}

void prime() {
  for (int i = 0; i < MAX_N; i++) {
    P[i] = true;
  }

  int limit;
  limit = (int)sqrt((double)MAX_N);
  for (int i = 2; i <= limit; i++) {
    if (P[i]) {
      for (int j = (i << 2); j <= MAX_N; j += i) {
        P[j] = false;
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

static const int MAX_N = 10000001;
int P[MAX_N];

void prime();

int main(int argc, char **argv) {
  prime();

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = n; i >= 13; i--) {
      if (i % 2 == 1) {
        if (P[i] == 1 && P[i - 2] == 1 && P[i - 6] == 1 && P[i - 8] == 1) {
          cout << i << endl;
          break;
        }
      }
    }
  }
}

void prime() {
  for (int i = 0; i < MAX_N; i++) {
    P[i] = 1;
  }

  int limit;
  limit = (int)sqrt((double)MAX_N);
  for (int i = 2; i <= limit; i++) {
    if (P[i] == 1) {
      for (int j = (i << 2); j <= MAX_N; j += i) {
        P[j] = 0;
      }
    }
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
bool prime[5000005];
int main() {
  for (int i = 0; i < 5000005; i++)
    prime[i] = true;
  for (int i = 3; i * i < 10000000; i += 2)
    for (int j = i / 2 + i; j < 5000005; j += i)
      prime[j] = false;
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    if (n % 2 == 0)
      n = n / 2 - 1;
    else
      n /= 2;
    while (true) {
      if (prime[n] && prime[n - 1] && prime[n - 3] && prime[n - 4])
        break;
      n--;
    }
    cout << n * 2 + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int prime[500];
int main() {
  int cnt = 0, d = 2;
  while (true) {
    bool isprime = true;
    for (int i = 0; i < cnt; i++)
      if (d % prime[i] == 0)
        isprime = false;
    if (isprime) {
      prime[cnt] = d, cnt++;
    }
    d++;
    if (d * d > 10000000)
      break;
  }
  int n, a[4];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    if (n % 2 == 0)
      n--;
    while (true) {
      a[0] = n, a[1] = n - 2, a[2] = n - 6, a[3] = n - 8;
      bool ok = true;
      for (int i = 0; i < 4; i++)
        for (int j = 0; j < cnt; j++)
          if (a[i] % prime[j] == 0 && a[i] != prime[j])
            ok = false;
      if (ok)
        break;
      n -= 2;
    }
    cout << n << endl;
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Memory Limit Exceeded


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

bool arr[10000010];

void Eratosthenes() {
  rep(i, 10000010) arr[i] = true;
  arr[0] = arr[1] = false;

  arr[0] = arr[1] = false;
  for (int i = 2; i <= sqrt(10000008); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) <= 10000008; j++)
        arr[i * (j + 2)] = false;
    }
  }
}

int main() {
  int n;
  Eratosthenes();
  while (cin >> n, n) {
    int i = n;
    while (!(arr[i] && arr[i - 2] && arr[i - 6] && arr[i - 8]))
      i--;
    cout << i << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

ll n;
ll arr[10000010];

void Eratosthenes() {
  for (ll i = 0; i <= 10000010; i++)
    arr[i] = 1;

  for (ll i = 2; i <= sqrt(10000010); i++) {
    if (arr[i]) {
      for (ll j = 0; i * (j + 2) <= 10000010; j++)
        arr[i * (j + 2)] = 0;
    }
  }
}

int main() {
  Eratosthenes();
  while (cin >> n, n) {
    ll i = n;
    while (!(arr[i] && arr[i - 2] && arr[i - 6] && arr[i - 8]))
      i--;
    cout << i << endl;
  }
  return 0;
}
Problem p00223 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

struct ST {
  int tx, ty;
  int kx, ky;
  int cnt;
  ST(int a, int s, int d, int f, int g) : tx(a), ty(s), kx(d), ky(f), cnt(g) {}
};

int mx[4] = {1, 0, 0, -1};
int my[4] = {0, 1, -1, 0};
int map[50][50];

int w, h;
int tx, kx;
int ty, ky;

void ans() {
  queue<ST> que;
  bool came[50][50][50][50] = {false};
  que.push(ST(tx - 1, ty - 1, kx - 1, ky - 1, 0));
  came[tx - 1][ty - 1][kx - 1][ky - 1] = true;
  while (!que.empty()) {
    ST st = que.front();
    if (st.tx == st.kx && st.ty == st.ky) {
      cout << st.cnt << endl;
      return;
    } else if (st.cnt >= 100) {
      break;
    }
    que.pop();
    for (int i = 0; i < 4; i++) {
      int j = 3 - i;
      tx = st.tx + mx[i];
      kx = st.kx + mx[j];
      ty = st.ty + my[i];
      ky = st.ky + my[j];
      if (tx < 0 || tx >= w || ty < 0 || ty >= h || map[ty][tx] == 1) {
        tx = st.tx;
        ty = st.ty;
      }
      if (kx < 0 || kx >= w || ky < 0 || ky >= h || map[ky][kx] == 1) {
        kx = st.kx;
        ky = st.ky;
      }
      if (came[tx][ty][kx][ky] == false) {
        que.push(ST(tx, ty, kx, ky, st.cnt + 1));
        came[tx][ty][kx][ky] = true;
      }
    }
  }
  cout << "NA" << endl;
}

int main() {
  while (cin >> w >> h, h) {
    cin >> tx >> ty;
    cin >> kx >> ky;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    ans();
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

struct ST {
  int tx, ty;
  int kx, ky;
  int cnt;
  ST(int a, int s, int d, int f, int g) : tx(a), ty(s), kx(d), ky(f), cnt(g) {}
};

int mx[4] = {1, 0, 0, -1};
int my[4] = {0, 1, -1, 0};
int map[50][50];

int w, h;
int tx, kx;
int ty, ky;

void ans() {
  queue<ST> que;
  bool came[50][50][50][50] = {false};
  que.push(ST(tx - 1, ty - 1, kx - 1, ky - 1, 0));
  while (!que.empty()) {
    ST st = que.front();
    if (st.tx == st.kx && st.ty == st.ky) {
      cout << st.cnt << endl;
      return;
    } else if (st.cnt >= 100) {
      break;
    }
    que.pop();
    came[st.tx][st.ty][st.kx][st.ky] = true;
    for (int i = 0; i < 4; i++) {
      int j = 3 - i;
      tx = st.tx + mx[i];
      kx = st.kx + mx[j];
      ty = st.ty + my[i];
      ky = st.ky + my[j];
      if (tx < 0 || tx >= w || ty < 0 || ty >= h || map[ty][tx] == 1) {
        tx = st.tx;
        ty = st.ty;
      }
      if (kx < 0 || kx >= w || ky < 0 || ky >= h || map[ky][kx] == 1) {
        kx = st.kx;
        ky = st.ky;
      }
      if (came[tx][ty][kx][ky] - 1) {
        que.push(ST(tx, ty, kx, ky, st.cnt + 1));
      }
    }
  }
  cout << "NA" << endl;
}

int main() {
  while (cin >> w >> h, h) {
    cin >> tx >> ty;
    cin >> kx >> ky;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    ans();
  }
}
Problem p00223 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

struct S {
  int tx, ty;
  int kx, ky;
  int turn;
  S(int tx, int ty, int kx, int ky, int turn);
};

S::S(int _tx, int _ty, int _kx, int _ky, int _turn) {
  tx = _tx;
  ty = _ty;
  kx = _kx;
  ky = _ky;
  turn = _turn;
}

bool d[50][50];
bool vst[50][50][50][50];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int X, Y;
  int tx, ty;
  int kx, ky;
  while (cin >> X >> Y, X || Y) {
    cin >> tx >> ty >> kx >> ky;
    for (int j = 0; j < Y; j++) {
      for (int i = 0; i < X; i++) {
        cin >> d[i][j];
      }
    }
    tx--;
    ty--;
    kx--;
    ky--;
    queue<S> q;
    bool ok = false;
    q.push(S(tx, ty, kx, ky, 0));
    memset(vst, false, sizeof(vst));
    vst[tx][ty][kx][ky] = true;
    while (!q.empty()) {
      S s = q.front();
      q.pop();
      if (s.turn >= 100)
        break;
      if (s.tx == s.kx && s.ty == s.ky) {
        cout << s.turn << endl;
        ok = true;
        break;
      }
      for (int i = 0; i < 4; i++) {
        int ntx = s.tx + dx[i], nty = s.ty + dy[i];
        int nkx = s.kx - dx[i], nky = s.ky - dy[i];
        if (0 > ntx || ntx >= X || 0 > nty || nty >= Y || d[ntx][nty]) {
          ntx -= dx[i], nty -= dy[i];
        }
        if (0 > nkx || nkx >= X || 0 > nky || nky >= Y || d[nkx][nky]) {
          nkx += dx[i], nky += dy[i];
        }
        if (!vst[ntx][nty][nkx][nky]) {
          vst[ntx][nty][nkx][nky] = true;
          q.push(S(ntx, nty, nkx, nky, s.turn + 1));
        }
      }
    }
    if (!ok)
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

struct S {
  int tx, ty;
  int kx, ky;
  int turn;
};

bool d[50][50];
bool vst[50][50][50][50];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int X, Y;
  int tx, ty;
  int kx, ky;
  while (scanf("%d %d", &X, &Y), X || Y) {
    scanf("%d %d", &tx, &ty);
    scanf("%d %d", &kx, &ky);
    for (int j = 0; j < Y; j++) {
      for (int i = 0; i < X; i++) {
        scanf("%d", &d[i][j]);
      }
    }
    queue<S> q;
    bool ok = false;
    S s = {tx - 1, ty - 1, kx - 1, ky - 1, 0};
    q.push(s);
    memset(vst, false, sizeof(vst));
    while (!q.empty()) {
      S s = q.front();
      q.pop();
      vst[s.tx][s.ty][s.kx][s.ky] = true;
      if (s.turn >= 100)
        break;
      if (s.tx == s.kx && s.ty == s.ky) {
        printf("%d\\n", s.turn);
        ok = true;
        break;
      }
      for (int i = 0; i < 4; i++) {
        int ntx = s.tx + dx[i], nty = s.ty + dy[i];
        int nkx = s.kx - dx[i], nky = s.ky - dy[i];
        if (0 > ntx || ntx >= X || 0 > nty || nty >= Y || d[ntx][nty]) {
          ntx -= dx[i], nty -= dy[i];
        }
        if (0 > nkx || nkx >= X || 0 > nky || nky >= Y || d[nkx][nky]) {
          nkx += dx[i], nky += dy[i];
        }
        if (!vst[ntx][nty][nkx][nky]) {
          S ns = {ntx, nty, nkx, nky, s.turn + 1};
          q.push(ns);
        }
      }
    }
    if (!ok)
      printf("NA\\n");
  }
}