ProgrammingVery hardCompetitive coding (CP)

How many ways are there to place 3 non-attacking rooks on a 3 by 3 board? Compute with this program.

How many ways are there to place 3 non-attacking rooks on a 3 by 3 board? Compute with this program.
#include <stdio.h>
int used[3];
int place(int r) {
    if (r == 3) return 1;
    int total = 0;
    for (int c = 0; c < 3; c++) {
        if (used[c]) continue;
        used[c] = 1;
        total += place(r + 1);
        used[c] = 0;
    }
    return total;
}
int main(void) {
    printf("%d", place(0));
}
  1. A. 6
  2. B. 9
  3. C. 3
  4. D. 12

Correct answer: A. 6

Each row gets a distinct column, so the rooks form a permutation of the columns and there are 3! = 6 placements.

More C Programming questions

Exams that ask this topic