ProgrammingVery hardCompetitive coding (CP)
How many monotone lattice paths go from the top-left corner to the bottom-right corner of a 4 by 4 grid of cells (moves only right or down)? Compute with this recursive program.
How many monotone lattice paths go from the top-left corner to the bottom-right corner of a 4 by 4 grid of cells (moves only right or down)? Compute with this recursive program.
#include <stdio.h>
int paths(int r, int c) {
if (r > 4 || c > 4) return 0;
if (r == 4 && c == 4) return 1;
return paths(r + 1, c) + paths(r, c + 1);
}
int main(void) {
printf("%d", paths(0, 0));
}- A. 70
- B. 35
- C. 56
- D. 126
Correct answer: A. 70
The recursion counts right-down paths on a 4 by 4 grid, which equals C(8, 4) = 70. Using C(7, 3) = 35 is a common off-by-one.
More C Programming questions
- How many iterations does this loop run? c #include <stdio.h> int main(void) { int c = 0; for (int i = 10; i >= 0; i -= 3) c++…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (int)'x41' + '101'); }
- What is printed? Assume int is 32 bits. c #include <stdio.h> struct B { unsigned a : 3; }; int main(void) { struct B s; s.a = 13…
- What is printed? Assume unsigned long long is 64 bits. c #include <stdio.h> int main(void) { unsigned long long v = 0xFFULL << 56…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned long long n = 4294967290ULL; unsigned b =…
- What is printed? c #include <stdio.h> int main(void) { printf("%u", 0x80000000u 3u); }