Untitled

                Never    
C
       
#include <stdio.h>
#include <stdlib.h>

int solution;
int N, M;
char board[12][12];

int place_chess(int current_row, int queen_done, int rook_done, int size);
int valid_queen(int row, int col, int size);
int valid_rook(int row, int col, int size);

int main(void) {
    while (scanf("%d%d", &N, &M) == 2) {
        solution = place_chess(0, 0, 0, N + M);
        printf("%d\n", solution);
    }
    return 0;
}

int place_chess(int current_row, int queen_done, int rook_done, int size) {
    if (current_row == size) {
        return 1;
    }
    int count = 0;
    for (int col = 0; col < size; col++) {
        if (queen_done < N && valid_queen(current_row, col, size)) {
            board[current_row][col] = 'Q';
            count += place_chess(current_row + 1, queen_done + 1, rook_done, size);
            board[current_row][col] = '0';
        }
        if (rook_done < M && valid_rook(current_row, col, size)) {
            board[current_row][col] = 'R';
            count += place_chess(current_row + 1, queen_done, rook_done + 1, size);
            board[current_row][col] = '0';
        }
    }
    return count;
}

int valid_queen(int row, int col, int size) {
    int i;
    for (i = 1; i <= row; i++) {
        if (col - i >= 0 && (board[row - i][col - i] == 'Q' || board[row - i][col - i] == 'R')) {
            return 0;
        }
        if (col + i < size && (board[row - i][col + i] == 'Q' || board[row - i][col + i] == 'R')) {
            return 0;
        }
        if (board[row - i][col] == 'Q' || board[row - i][col] == 'R') {
            return 0;
        }
    }
    return 1;
}

int valid_rook(int row, int col, int size) {
    int i;
    for (i = 1; i <= row; i++) {
        if (col - i >= 0 && (board[row - i][col - i] == 'Q')) {
            return 0;
        }
        if (col + i < size && (board[row - i][col + i] == 'Q')) {
            return 0;
        }
        if (board[row - i][col] == 'Q' || board[row - i][col] == 'R') {
            return 0;
        }
    }
    return 1;
}

Raw Text