Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Cao Beijian
- Result: AC Submission_id: 4271005
- Created at: Sun Apr 03 2022 12:35:17 GMT+0800 (China Standard Time)
- Problem_id: 5439 Time: 3 Memory: 1676
- */
- #include <stdio.h>
- #include <stdlib.h>
- int queens[25] = { 0 }, rows = 0, ans = 0;
- char check(int k, int p){//row k col p
- int i;
- for(i = 1; i < k; i++){
- if((queens[i] == p) || (abs(queens[i] - p) == abs(k - i))){
- return 0;
- }
- }
- return 1;
- }
- void dfs(int k){
- if(k > rows){
- ans++; return;
- }
- else{
- int i;
- for(i = 1; i <= rows; i++){
- if(check(k, i)){
- queens[k] = i;
- dfs(k + 1);
- }
- queens[k] = 0;
- }
- }
- }
- int main(){
- scanf("%d", &rows);
- dfs(1);
- printf("%d\n", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment