fork download
  1. #define N 4
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4.  
  5. void printSolution(int board[N][N])
  6. {
  7. for (int i = 0; i < N; i++) {
  8. for (int j = 0; j < N; j++)
  9. printf(" %d ", board[i][j]);
  10. printf("\n");
  11. }
  12. }
  13.  
  14. bool isSafe(int board[N][N], int row, int col)
  15. {
  16. int i, j;
  17.  
  18. for (i = 0; i < col; i++)
  19. if (board[row][i])
  20. return false;
  21.  
  22. for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
  23. if (board[i][j])
  24. return false;
  25.  
  26. for (i = row, j = col; j >= 0 && i < N; i++, j--)
  27. if (board[i][j])
  28. return false;
  29.  
  30. return true;
  31. }
  32.  
  33.  
  34. bool solveNQUtil(int board[N][N], int col)
  35. {
  36.  
  37. if (col >= N)
  38. return true;
  39.  
  40. for (int i = 0; i < N; i++) {
  41.  
  42. if (isSafe(board, i, col)) {
  43.  
  44. board[i][col] = 1;
  45.  
  46.  
  47. if (solveNQUtil(board, col + 1))
  48. return true;
  49.  
  50.  
  51. board[i][col] = 0;
  52. }
  53. }
  54. return false;
  55. }
  56.  
  57. bool solveNQ()
  58. {
  59. int board[N][N] = { { 0, 0, 0, 0 },
  60. { 0, 0, 0, 0 },
  61. { 0, 0, 0, 0 },
  62. { 0, 0, 0, 0 } };
  63.  
  64. if (solveNQUtil(board, 0) == false) {
  65. printf("Solution does not exist");
  66. return false;
  67. }
  68.  
  69. printSolution(board);
  70. return true;
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76. solveNQ();
  77. return 0;
  78. }
  79.  
  80.  
Success #stdin #stdout 0.02s 26072KB
stdin
Standard input is empty
stdout
#define N 4
#include <stdbool.h>
#include <stdio.h>

void printSolution(int board[N][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            printf(" %d ", board[i][j]);
        printf("\n");
    }
}
  
bool isSafe(int board[N][N], int row, int col)
{
    int i, j;
  
    for (i = 0; i < col; i++)
        if (board[row][i])
            return false;
  
    for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
        if (board[i][j])
            return false;
  
    for (i = row, j = col; j >= 0 && i < N; i++, j--)
        if (board[i][j])
            return false;
  
    return true;
}
  

bool solveNQUtil(int board[N][N], int col)
{
    
    if (col >= N)
        return true;
  
    for (int i = 0; i < N; i++) {
        
        if (isSafe(board, i, col)) {
            
            board[i][col] = 1;
  
            
            if (solveNQUtil(board, col + 1))
                return true;
  
            
            board[i][col] = 0; 
        }
    }
    return false;
}
  
bool solveNQ()
{
    int board[N][N] = { { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 } };
  
    if (solveNQUtil(board, 0) == false) {
        printf("Solution does not exist");
        return false;
    }
  
    printSolution(board);
    return true;
}
  

int main()
{
    solveNQ();
    return 0;
}