• Source
    1. /*
    2.  * Buggy code, compile goog, running time error
    3. */
    4. #include <iostream>
    5. #include <vector>
    6. #include <cmath>
    7.  
    8. using namespace std;
    9.  
    10. #define GRID_SIZE 8
    11.  
    12. bool checkValidity(int row, int* columns, int col){
    13. for(int row1 = 0; row1 < row; row1++){
    14. int column1 = columns[row1];
    15.  
    16. if(column1 == col)
    17. return false;
    18.  
    19. int colDistance = abs(column1 - col);
    20. int rowDistance = row - row1;
    21. if(colDistance == rowDistance)
    22. return false;
    23. }
    24.  
    25. return true;
    26. }
    27.  
    28. void placeQueen(int row, int* columns, vector<int *> results){
    29. if(row == GRID_SIZE){
    30. results.push_back(columns);
    31. }else{
    32. for(int col = 0; col < GRID_SIZE; col++){
    33. if(checkValidity(row, columns, col)){
    34. columns[row] = col;
    35. placeQueen(row, columns, results);
    36. }
    37. }
    38. }
    39. }
    40.  
    41. int main() {
    42.  
    43. int columns[GRID_SIZE];
    44. vector<int *> results;
    45. placeQueen(0, columns, results);
    46. return 0;
    47. }