Untitled

這根本整個錯的但我不想看我好痛苦
mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
0
Indexable
Never
#include <stdio.h>

int M,R,n,q,c;
int Q[11];
int C[11];

void place(int row);
int valid(int row,int col);
int vvalid(int row,int col);

int main(void){

    while(scanf("%d %d",&M,&R)!=EOF){
        place(0);
        printf("%d\n",n);
        n=0;
    }
    return 0;
}

void place(int row){
    
    if(row==M+R){
        n++;
    }
    else{
        for(int j=0;j<M+R;j++){
            if(valid(row,j) && q<M){
                Q[row]=j;
                q++;
                place(row+1);
                Q[row]='\0';
                q--;
            }
            if(vvalid(row,j) && c<R){
                C[row]=j;
                c++;
                place(row+1);
                C[row]='\0';
                c--;
            }
        }
    }
}

int vvalid(int row,int col){
    
    for(int i=0;i<=row-1;i++){
        if(Q[i]==col || Q[i]==col-row+i || Q[i]==col+row-i || C[i]==col){
            return 0;
        }
    }
    return 1;
}
int valid(int row,int col){
    
    for(int i=0;i<=row-1;i++){
        if(Q[i]==col || Q[i]==col-row+i || Q[i]==col+row-i || C[i]==col || C[i]==col-row+i || C[i]==col+row-i){
            return 0;
        }
    }
    return 1;
}