Untitled
raw download clone
C
views 11
,
size 1144 b
#include <stdio.h>

int n, m, lane[11], qr[11], ans, qed, red;

void rec(int row){
    if(row == n+m) {ans++; return;}
    for(int i=0; i<n+m; i++){
        lane[row] = i;
        if(qed<n){
            qr[row] = 1;    
            qed++;          
            if(vld(row)) rec(row+1);
            qr[row] = 0;    
            qed--;          
            if(red<m && vld(row)){
                red++;
                rec(row+1);
                red--;
            }
        }
        else{
            if(red<m && vld(row)){
                red++;
                rec(row+1);
                red--;
            }
        }
        lane[row] = -1;
    }
}

int vld(int row){
    for(int i=0; i<row; i++){
        if(qr[i] || qr[row]){
            if(lane[i] == lane[row]          ||
               lane[i]-(row-i) == lane[row]  ||
               lane[i]+(row-i) == lane[row]) return 0;
        }

        
        else
        if(lane[i] == lane[row]) return 0;
    }
    return 1;
}

int main(){
    scanf("%d%d", &n, &m);
    ans = 0, qed = 0, red = 0;
    rec(0);
    printf("%d\n", ans);
}
close fullscreen
Login or Register to edit or fork this paste. It's free.