ProgrammingVery hardCompetitive coding (CP)
How many paths go from the top-left to the bottom-right of a 3 by 3 grid of points, moving right or down, without passing through the centre point (1, 1)? Compute with this program.
How many paths go from the top-left to the bottom-right of a 3 by 3 grid of points, moving right or down, without passing through the centre point (1, 1)? Compute with this program.
#include <stdio.h>
int paths(int r, int c) {
if (r == 1 && c == 1) return 0;
if (r == 2 && c == 2) return 1;
if (r > 2 || c > 2) return 0;
return paths(r + 1, c) + paths(r, c + 1);
}
int main(void) {
printf("%d", paths(0, 0));
}- A. 2
- B. 4
- C. 6
- D. 0
Correct answer: A. 2
Only the two outer routes avoid the centre: right, right, down, down along the edge and down, down, right, right along the other edge. Each is one path, so the total is 2.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a = 2147483647; printf("%u", (unsigned)a + 1u); }
- How many integers from 1 to 64 have their low three bits all clear? Compute with this program. c #include <stdio.h> int main(void) { int c…
- How many times does the letter g occur in "programming"? The program counts it with a pointer walk. c #include <stdio.h> int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { int x = 10; printf("%d %d", x & -x, x & (x - 1)); }
- What is printed? c #include <stdio.h> int main(void) { int v[] = {3, 9, 4}; int p = v; int s = 0; while (p < v + 3) s += p++ 2…
- What is printed? c #include <stdio.h> int main(void) { printf("%c", 'Z' - 'A' + 'a'); }