Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.7 kB
2
Indexable
Never
#include <stdio.h>
//#define NQ 4
int q[18] = {-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100};
int c[18] ={-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100};;
int n,a,b;
int N,Q;

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

int main(void)
{
	while(scanf("%d %d",&N,&Q)!=EOF){
        a=0;
        n=0;
        b=0;
        place(0);
        printf("%d\n",n);
        for(int i=0;i<15;i++){
            q[i]=-100;
            c[i]=-100;
        }
    }
    return 0;
}

int valid(int row, int col)
{
    int i;
    for (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;
}

int vvalid(int row, int col)
{
    int i;
    for (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;
}


void place(int row){

    //printf("row:%d\n",row);
	int j;
	if (row == N+Q) {
        //printf("code\n");
		n++;
	} else {
        //printf("row2:%d\n",row);
		for (j=0; j<N+Q; j++) {
            //printf("row3:%d\n",row);
			if (valid(row, j) && a<N) {
                //printf("valid:%d ; N=%d ; a=%d\n",row,N,a);
                q[row] = j;
                a++;
                place(row+1);
                q[row] = -100;
                a--;
			}
            if (vvalid(row, j) && b<Q) {
                //printf("vvalid:%d ; N=%d ; a=%d\n",row,N,a);
                c[row] = j;
                b++;
                place(row+1);
                c[row] = -100;
                b--;
			}
		}
	}
}