Untitled

嗚嗚嗚嗚嗚 為甚麼一直不對然後那麼吵不能思考啊......
mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
0
Indexable
Never
#include <stdio.h>

int M,R,n,q,c;
int Q[15]={0};
int C[15]={0};

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);
        for(int i=0;i<15;i++){
            Q[i]='\0';
            C[i]='\0';
        }
        n=0;
        q=0;
        c=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;
}