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

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
static const int INF = 1000000;

typedef pair<int, int> P;
typedef pair<P, int> PP;

int Q[8];
void printMap() {
  rep(i, 8) {
    rep(j, 8) {
      if (j == Q[i])
        cout << "Q";
      else
        cout << ".";
    }
    cout << endl;
  }
}

int main(void) {
  int k;
  cin >> k;
  bool defaultQueen[8];
  bool v[8];
  bool skew[16];
  bool skew_N[16];
  rep(i, 8) {
    defaultQueen[i] = false;
    v[i] = false;
  }
  rep(i, 16) {
    skew[i] = false;
    skew_N[i] = false;
  }

  int a, b;
  rep(i, k) {
    cin >> a >> b;
    defaultQueen[a] = true;
    v[b] = true;
    skew[a + b] = true;
    skew_N[a + 7 - b] = true;
    Q[a] = b;
  }

  bool backFlag = false;
  bool successFlag;
  bool putFlag;
  int line_num = 0;
  int base;

  while (line_num < 8) {
    successFlag = false;
    if (backFlag && defaultQueen[line_num]) {
      line_num -= 1;
      continue;
    }

    if (backFlag && !defaultQueen[line_num]) {

      v[Q[line_num]] = false;
      skew[line_num + Q[line_num]] = false;
      skew_N[line_num + 7 - Q[line_num]] = false;
    }

    if (defaultQueen[line_num]) {
      line_num += 1;
      continue;
    }

    if (backFlag)
      base = Q[line_num] + 1;
    else
      base = 0;

    for (int i = base; i < 8; i++) {
      if (!v[i] && !skew[line_num + i] && !skew_N[line_num + 7 - i] &&
          !successFlag) {
        v[i] = true;
        skew[line_num + i] = true;
        skew_N[line_num + 7 - i] = true;
        successFlag = true;
        Q[line_num] = i;
      }
    }
    backFlag = false;

    if (!successFlag) {
      backFlag = true;
      line_num -= 1;
    } else {
      line_num += 1;
    }
  }

  printMap();

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
static const int INF = 1000000;

typedef pair<int, int> P;
typedef pair<P, int> PP;

int Q[8];
void printMap() {
  rep(i, 8) {
    rep(j, 8) {
      if (j == Q[i])
        cout << "Q";
      else
        cout << ".";
    }
    cout << endl;
  }
}

int main(void) {
  int k;
  cin >> k;
  bool defaultQueen[8];
  bool v[8];
  bool skew[16];
  bool skew_N[16];
  rep(i, 8) {
    defaultQueen[i] = false;
    v[i] = false;
    skew[i] = false;
    skew_N[i] = false;
  }

  int a, b;
  rep(i, k) {
    cin >> a >> b;
    defaultQueen[a] = true;
    v[b] = true;
    skew[a + b] = true;
    skew_N[a + 7 - b] = true;
    Q[a] = b;
  }
  bool backFlag = false;
  bool successFlag;
  bool putFlag;
  int line_num = 0;
  int base;

  while (line_num < 8) {
    successFlag = false;
    // cout << line_num << endl;
    //  cout << "back = " << backFlag << " " << "default = " <<
    //  defaultQueen[line_num] << endl;

    //?????????????????????????????????????????????????????????????????¨???
    if (backFlag && defaultQueen[line_num]) {
      line_num -= 1;
      continue;
    }

    if (backFlag && !defaultQueen[line_num]) {

      //??????????????????????????????
      v[Q[line_num]] = false;
      skew[line_num + Q[line_num]] = false;
      skew_N[line_num + 7 - Q[line_num]] = false;
    }

    //?????§??????????????????????????????????????°continue
    if (defaultQueen[line_num]) {
      line_num += 1;
      continue;
    }

    if (backFlag)
      base = Q[line_num] + 1;
    else
      base = 0;
    /*
        rep(i,8)
          cout << "v[" << i << "] =" << v[i] << endl;
        rep(i,16){
          cout << "skew[" << i << "] = " << skew[i] << " skew_n[" << i <<"] = "
       << skew_N[i] << endl;
        }*/
    for (int i = base; i < 8; i++) {
      if (!v[i] && !skew[line_num + i] && !skew_N[line_num + 7 - i] &&
          !successFlag) {
        v[i] = true;
        skew[line_num + i] = true;
        skew_N[line_num + 7 - i] = true;
        successFlag = true;
        Q[line_num] = i;
      }
    }
    //  cout << "Q = " << Q[line_num] << endl;

    backFlag = false;

    if (!successFlag) {
      backFlag = true;
      line_num -= 1;
    } else {
      line_num += 1;
    }

    // cout << endl;
  }

  printMap();

  return 0;
}