I need help to write this program in Java, Thanks YNAMIC PROGRAMMING 17. /5) Con
ID: 3915702 • Letter: I
Question
I need help to write this program in Java, Thanks
YNAMIC PROGRAMMING 17. /5) Consider the same situation as the previous problem. We have a city whose streets are defined by an x y grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an X × Y matrix BAD, where BAD -"yes" if and only if the intersection between streets i and j is somewhere we want to avoid. If there were no bad neighborhoods to contend with, the shortest path across the grid would have length (X - 1) (Y-1) blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. Give an algorithm that takes the array BAD and returns the number of safe paths of length X +Y- 2. For full credit, your algorithm must run in o(xY).Explanation / Answer
Let’s start with an example with three paths of length X + Y – 2.
1
2
3
4
G G G G G 0 1 2 3 4
G G B G G 1 2 X 4 5
G B G B G 2 X 6 X 6
G G G G G 3 4 5 6 7
We started with costs 0 at [0, 0] therefore we got X + Y – 2 – 1 as shortest paths. We know that each valid path is characterized by its costs 0 to X + Y – 2 – 1. Therefore we just have to count how often the full set exists. E.g. we count for each number its occurrence with an array and afterwards find the smallest amount of occurrences which is the number of paths.
Implementation in Perl:
1
2
3
4
G G G G G 0 1 2 3 4
G G B G G 1 2 X 4 5
G B G B G 2 X 6 X 6
G G G G G 3 4 5 6 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.