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));
}
  1. A. 2
  2. B. 4
  3. C. 6
  4. 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

Exams that ask this topic