countPaths(0, 0, 2, 2) returns 6 countPaths (0, 0, 1, 1) returns 2 countPaths (2, 1, 2, 2) returns 1 Hint! Find first the base case(s). The examples above will be useful. Then, think about how to educe the path length by 1 for recursive call(s). For example, one possibility to reach (2, 2) is to each first (1, 2) and make right move. countPaths can be used to count paths to reach (1, 2).

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
For example,
countPaths (0, 0, 2, 2) returns 6
countPaths (0, 0, 1, 1) returns 2
countPaths(2, 1, 2, 2) returns 1
Hint! Find first the base case(s). The examples above will be useful. Then, think about how to
reduce the path length by 1 for recursive call(s). For example, one possibility to reach (2, 2) is to
reach first (1, 2) and make right move. countPaths can be used to count paths to reach (1, 2).
A large penalty will be applied to non recursive solutions. Hardcoding will always get zero point.
static int countPaths (int x1, int y1, int x2, int y2){
Transcribed Image Text:For example, countPaths (0, 0, 2, 2) returns 6 countPaths (0, 0, 1, 1) returns 2 countPaths(2, 1, 2, 2) returns 1 Hint! Find first the base case(s). The examples above will be useful. Then, think about how to reduce the path length by 1 for recursive call(s). For example, one possibility to reach (2, 2) is to reach first (1, 2) and make right move. countPaths can be used to count paths to reach (1, 2). A large penalty will be applied to non recursive solutions. Hardcoding will always get zero point. static int countPaths (int x1, int y1, int x2, int y2){
Consider a grid of size (x, y) where x and y are positive integers. The following figure
shows an example grid of size (2,3) and two points p1 and p2 in the grid. Assume that x and
y of p2 is greater than x and y of p1.
p2(2,2)
ectangular Snip
p1(0,0)
Write a recursive method named countPaths that returns the number of distinct shortest paths
from p1 to p2. The method takes 4 ints as the parameters – i.e., x and y values of the two
points: x1, y1, x2, and y2. Note that you do not need to print the paths. Count only.
To be shortest, a path will only consist of only upward and rightward movements where each
movement increases x- or y-coordinate value by 1. In the figure above,
|(right, up, right, up) or (right, right, up, up) are both shortest paths from p1 to p2.
For example,
countPaths (0, 0, 2, 2) returns 6
countPaths (0, 0, 1, 1) returns 2
countPaths (2, 1, 2, 2) returns 1
Transcribed Image Text:Consider a grid of size (x, y) where x and y are positive integers. The following figure shows an example grid of size (2,3) and two points p1 and p2 in the grid. Assume that x and y of p2 is greater than x and y of p1. p2(2,2) ectangular Snip p1(0,0) Write a recursive method named countPaths that returns the number of distinct shortest paths from p1 to p2. The method takes 4 ints as the parameters – i.e., x and y values of the two points: x1, y1, x2, and y2. Note that you do not need to print the paths. Count only. To be shortest, a path will only consist of only upward and rightward movements where each movement increases x- or y-coordinate value by 1. In the figure above, |(right, up, right, up) or (right, right, up, up) are both shortest paths from p1 to p2. For example, countPaths (0, 0, 2, 2) returns 6 countPaths (0, 0, 1, 1) returns 2 countPaths (2, 1, 2, 2) returns 1
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY