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));
}- A. 6
- B. 9
- C. 3
- 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
- In the sorted array {1, 3, 4, 4, 7}, what index is printed by this lower-bound search for key 4? c #include <stdio.h> int main(void) { int…
- How many of these statements are true for unsigned x = 5? Print the count from this program. c #include <stdio.h> int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {5, 6, 7}, p = a + 1; printf("%d", p[-1] + p + p[1]); }
- How many tilings does a 2 by 5 board have with 2 by 1 dominoes? Compute with this recursive program. c #include <stdio.h> int f(int n) {…
- What is printed? c #include <stdio.h> #include <stdlib.h> int v[] = {30, 10, 20}; int cmp(const void a, const void b) { int i = (const int…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { unsigned x = (const unsigned )a, y =…