Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #pragma warning(disable:4996)
- typedef long long LL;
- typedef unsigned long long ULL;
- #define MAX(a,b) (((a)>(b))?(a):(b))
- #define SETBIT(gpio,bit) ((gpio)|=(1u<<(bit)))
- #define RESETBIT(gpio,bit) ((gpio)&=~(1u<<(bit)))
- #define READBIT(gpio,bit) (((gpio)>>(bit))&1)
- #define CROL(X,i,n) ((((X)<<(i))|((X)>>((n)-(i))))&((1ull<<(n))-1ull))
- #define CROR(X,i,n) ((((X)>>(i))|((X)<<((n)-(i))))&((1ull<<(n))-1ull))
- 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){
- int i;
- if(k > rows){
- if(ans < 3){
- for(i = 1; i < k; i++){
- printf("%d ", queens[i]);
- }
- putchar('\n');
- }
- ans++; return;
- }
- else{
- for(i = 1; i <= rows; i++){
- if(check(k, i)){
- queens[k] = i;
- dfs(k + 1);
- }
- queens[k] = 0;
- }
- }
- }
- int main(){
- #ifdef _DEBUG
- FILE *fp = freopen("../../../input.txt", "r", stdin);
- //FILE *fp2 = freopen("output.txt", "w", stdout);
- #endif // _DEBUG
- scanf("%d", &rows);
- dfs(1);
- printf("%d\n", ans);
- #ifdef _DEBUG
- freopen("CON", "r", stdin);
- //freopen("CON", "w", stdout);
- system("pause");
- #endif // _DEBUG
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment