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

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 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 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...
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...
I want to make a python program that encodes/decodes inputted messages. There are only three options...
I want to make a python program that encodes/decodes inputted messages. There are only three options in the initial menu: 1. encode, 2. decode, 3. quit, and if 1, 2, or 3 are not the input the program says that it is not valid, try again. If encode is chosen, the user inputs their message, the message should only be alphabetical and spaces. Then, the user can pick a number in the range [-27,27] with space being the 27th character...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Can you write this program, but in python, I just wanna see what am I doing...
Can you write this program, but in python, I just wanna see what am I doing wrong. thank you Instructions: The Gas-N-Clean Service Station sells gasoline and has a car wash. Fees for the car wash are $1.25 with a gasoline purchase of $10.00 or more and $3.00 otherwise. Three kinds of gasoline are available: regular at $2.89, plus at $3.09, and super at $3.39 per gallon. User Request: Write a program that prints a statement for a customer. Analysis:...
I need a python program and a flowchart please!! Problem #1:    How much should I...
I need a python program and a flowchart please!! Problem #1:    How much should I study outside of class?                         Issue: Your fellow students need help. This is their first year in college and they need to determine how many hours they need to study to get good grades. Study Hours Per Week Per Class                    Grade 15                                                           A 12                                                           B 9                                                             C 6                                                             D 0                                                             F Project Specifications: The user enters their full name and the number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT