wojiaocbj

queens

Apr 2nd, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. /*
  2.  Author: Cao Beijian
  3.  Result: AC Submission_id: 4270941
  4.  Created at: Sun Apr 03 2022 12:06:29 GMT+0800 (China Standard Time)
  5.  Problem_id: 5439   Time: 4 Memory: 1732
  6. */
  7.  
  8. #include <stdio.h>
  9. int chessboard[25] = { 0 };
  10. int count = 0;
  11. int rows;
  12. char check(int k, int col){
  13.     int i = 1;
  14.     for(i = 1;i < k;i++){
  15.         if(chessboard[i] == col || abs(i - k) == abs(chessboard[i] - col)){
  16.             return 0;
  17.         }
  18.     }
  19.     return 1;
  20. }
  21. void dfs(int k){
  22.     if(k > rows){
  23.         count++;
  24.         //output();
  25.         return;
  26.     }
  27.     else{
  28.         int j;
  29.         for(j = 1;j <= rows;j++){
  30.             if(check(k, j)){
  31.                 chessboard[k] = j;
  32.                 dfs(k + 1);
  33.             }
  34.             chessboard[k] = 0;
  35.         }
  36.     }
  37. }
  38. int main(){
  39.     scanf("%d", &rows);
  40.     dfs(1);
  41.     printf("%d\n", count);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment