Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This was an assignment please can anyone help me with that ? Consider a network

ID: 3812823 • Letter: T

Question

This was an assignment please can anyone help me with that ?

Consider a network of streets laid out in a rectangular grid, for example: In a northeast path from one point in the grid to another, one may walk only to the north (up) and to the east (right). For example, there are 4 northeast paths from A to B in the preceding grid. Write a java program that uses a recursive method to count the number of northeast paths from one point to another in a rectangular grid. Also, output the actual paths taken. In this classic example, one needs to establish a set order of examination, say visit one's north and then one's east neighbors. Then, one simply recurses north until one falls off of the grid. Recursion "automatically" takes care of backtracking to a legal grid point and then recursing to the east. By restricting travel to only these two directions, one need not worry about creating cycles, and thus infinite recursion.

Explanation / Answer

To calculate Northeast paths we let the user enter the location of two points, say A and B, between which the northeast paths are to be calculated. For location we use (x,y) coordinates.

Java Program below:

import java.util.Scanner;

public class NorthEastPaths {
public static int ne( int rows, int cols, String path)
{
if( rows == 0 && cols == 0 )
{
System.out.println(path);
return 1;
}
int npaths = 0, wpaths = 0;
if( rows != 0 )
npaths = ne( rows-1, cols, path+"N" );
if( cols != 0 )
wpaths = ne( rows, cols-1, path+"E" );
return npaths + wpaths;
}
  
public static void main(String ars[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter x and y coordinates of starting point , A ");
System.out.println("Enter x coordinate of A: ");
int Ax = in.nextInt();
System.out.println("Enter y coordinate of A: ");
int Ay = in.nextInt();
  
System.out.println("Enter x and y coordinates of ending point , B ");
System.out.println("Enter x coordinate of B: ");
int Bx = in.nextInt();
System.out.println("Enter y coordinate of B: ");
int By = in.nextInt();
  
int noOfColumns = Bx-Ax;
int noOfRows = By-Ay;
String path = "";
int count=0;
System.out.println(" Paths--->");
count = ne(noOfRows, noOfColumns, path);
System.out.println("Total paths : "+count);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote