THINK FIRST·CODE LATER

← All labs

Grid paths: the same recursion with and without a memo

Problem

A robot starts in the top-left cell of a grid and may move only right or down. Some cells are blocked. How many different paths reach the bottom-right cell?

Write GridPaths with two recursive methods computing the same number: plain(r, c) and withMemo(r, c) (stores each cell's answer in a long[][] filled with −1). Count the calls of each (the exact counts depend on small details of your code, so the tests check only the first two lines). Input: rows cols k, then k blocked cells row col (0-based).

For the input

4 5 2
1 1
2 3

the program prints

Paths: 7
Same answer without memo: true
Calls without memo: 61
Calls with memo: 35

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d1-5-data-1.txt

4 5 2
1 1
2 3
What your program must print for d1-5-data-1.txt
Paths: 7
Same answer without memo: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d1-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content d1-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.