fork download
  1. #include <stdio.h>
  2.  
  3. #define N 4
  4.  
  5. int board[N]; // i-ci sətrdəki vezirin sütunu
  6. int count = 0; // həllərin sayı
  7.  
  8. int safe(int row, int col) {
  9. for (int i = 0; i < row; i++) {
  10. if (board[i] == col || (row - i) == (col - board[i]) || (row - i) == (board[i] - col))
  11. return 0;
  12. }
  13. return 1;
  14. }
  15.  
  16. void solve(int row) {
  17. if (row == N) {
  18. count++; // yeni həll tapıldı
  19. return;
  20. }
  21.  
  22. for (int col = 0; col < N; col++) {
  23. if (safe(row, col)) {
  24. board[row] = col;
  25. solve(row + 1);
  26. }
  27. }
  28. }
  29.  
  30. int main() {
  31. solve(0);
  32. printf("4x4 taxtada 4 veziri yerləşdirməyin yolları: %d\n", count);
  33. return 0;
  34. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
4x4 taxtada 4 veziri yerləşdirməyin yolları: 2