Question

In: Computer Science

how can i make this program in python? Captain Øks is on his way to conquer...

how can i make this program in python?

Captain Øks is on his way to conquer Mosefjell.

He has a map of the area and he is in his camp at the designated place on the map.

The map is divided into cells. For each cell, the height is given.

Captain Øks is not happy, the landscape is full of hills,

and he hates to climb both up and down.

He has hired you to write a program that will find the most comfortable route to Mosefjell.

In other words:

Given a 2-dimensional array A with dimensions M x N,

containing positive integers (A [i, j] represents the height of the map's column i and row j). Write a program that will find a sequence of neighboring cells that starts with a given starting element and ends with a given end element so that the cost is minimal.

For each cell he moves it costs 1.

If he has to move up or down then the height difference is added to the cost.

The difference should be added regardless of whether it goes up or down

(add the absolute value of the difference in height of the two cells).

Neighboring cells must have a common side (eg A [2,2] and A [2,3] are neighbors, while A [2,2] and A [3,3] are not).

The map can be converted to a graph by creating a node for each cell in the map and edges between all neighboring nodes with cost as described above.

The most comfortable way can then be calculated with an algorithm from the lectures.

The map should be loaded from a text file of the following format: Width

Height

x coordinate for Captain Øks camp

y coordinate for Captain Øks camp

x coordinate for Mosefjell

y coordinate for Mosefjell

For each row in the map a line of numbers separated by commas, one number for each column.

A file of this format for the sample map is:

12

8

1

4

10

4

5,5,7,8,9,9,8,8,9,9,7,9

5,6,7,8,7,8,9,8,9,7,8,9

6,6,6,5,6,7,7,8,9,7,8,9

3,3,5,5,5,6,7,7,6,7,8,6

1,1,2,3,7,9,8,8,6,5,5,4

5,5,5,3,7,8,8,7,5,5,5,4

6,7,8,4,4,8,6,6,5,5,3,1

7,7,8,9,4,4,5,5,4,3,1,0

Solutions

Expert Solution

The algorithm that must be used to solve this problem is Floyd Warshall. Since in this problem, you are provided with start cell and a destination cell in a matrix and you are allowed to move to any cell adjacent to your present cell (the cells that are sharing a side). So you can attempt up, down, right and left moves, as soon as you do not move out of the matrix.

For Floyd Warshall, create another matrix using the help of the input matrix. Considering the matrix as a graph, every cell is a node of the graph. Let's first given an integer index to every cell.

mat[i][j] cell can be given index = i*(number of rows) + j;

Like this, an index can be given to every cell.

Now using these indices, we will create a new matrix.

new_matrix[i][j] = difference in height between cell which has an index i and a cell which has an index j. And these both cells will be mandatory adjacent. If the cell are not adjacent, then the new_matrix[i][j] = INFINITY.

Now apply Floyd Warshall on new_matrix and the if 'a' is the index of start cell and 'b' is the index of the destination cell then new_matrix[a][b] will have your answer.

Now, this approach is easy to code but inefficient.

An efficient approach will involve using Dijkstra's algorithm, making use of heaps and priority queues.


Related Solutions

How to make a python program that imports only turtle and math library where I can...
How to make a python program that imports only turtle and math library where I can click once on the screen to set the center of the square, move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point?
How do I make a dictionary in Python language? Can somebody please provide me with an...
How do I make a dictionary in Python language? Can somebody please provide me with an example code of a Dictionary in Python Language? Thank you in advance.
As a challenge question i was asked to make a program that can make a newly...
As a challenge question i was asked to make a program that can make a newly made Directory that has the name input from the user. I am to create a program that can make a txt file in the newly created directory. I am doing this in windows and this is what i got. #include <windows.h> #include <string> #include <iostream> #include <string.h> #include <fstream> #include <TlHelp32.h> using namespace std; int main() { string Text = "Test string"; ofstream file;...
How can I write a Python program that runs without crashing even though it has an...
How can I write a Python program that runs without crashing even though it has an error that a compiler would have caught and how can I write a Java program that crashes even after the compiler checked it. Please add comments that explain the problem, the outcome, and what this proves in both of the programs.
I am trying to make a program in Python that opens a file and tells you...
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file. I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator? Here is my program #Assignment Ch7-1 #This program takes...
How can I make this program able to implement listener for the editable text field: ///////////////////////////////...
How can I make this program able to implement listener for the editable text field: /////////////////////////////// KiloConverter.java ////////////////////////// import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class KiloConverter extends JFrame { // constant for converting km to miles static final double KM_TO_MILES = 0.6214; // important components that needs to be accessed throughout the program private JTextField input, output; // constructor initializing GUI public KiloConverter() { // passing title...
How can I make sure (test) a reverse program that returns the same array with the...
How can I make sure (test) a reverse program that returns the same array with the same values as you entered before, but just in reverse order? Example of the program im talking about: void reverseArray(int arr[], int start, int end) {     int temp;     while (start < end)     {         temp = arr[start];            arr[start] = arr[end];         arr[end] = temp;         start++;         end--;     }    }  
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
Hello, How can I make the program print out "Invalid data!!" if the file has a...
Hello, How can I make the program print out "Invalid data!!" if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student grade. Smith Kelly 438975 98.6 A Johnson Gus 210498 72.4 C Reges...
Write a Python program that extracts 1000 unique links from Twitter using Tweepy. How can I...
Write a Python program that extracts 1000 unique links from Twitter using Tweepy. How can I filter out all links with Twitter domains and shortened links?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT