×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Goods Delivery by Robot using Python Assignment Solution

June 19, 2024
Dr. Nicole
Dr. Nicole
🇬🇧 United Kingdom
Python
Dr. Nicole, an accomplished professional with 6 years of experience, obtained her Ph.D. from Princeton University. Having completed over 400 Python assignments, she brings a wealth of expertise and insight to the table. With a focus on clarity and precision, Dr. Nicole is committed to providing comprehensive support to students seeking assistance with their Python projects.
Tip of the day
Use modular coding in Verilog assignments by breaking the design into smaller modules. This improves readability, simplifies debugging, and allows for easier testing and reuse of code components in larger designs.
News
In 2024, Visual Studio Code now offers enhanced GitHub Copilot integration for faster, while PyCharm has improved debugging and testing tools, making it ideal for Python students​

Robot Delivery

Write a python assignment where a robot is delivering products for a company in a city with only one-way streets. The robot is required to deliver to at least one household on every street and then return to its original destination for refit and repair. The city charges the robot a small fee per street that it traverses; this fee is charged every time that the robot crosses from one intersection to the next. Fees vary depending on the street, but every street that the robot is allowed to travel on requires a fee of at least 1; any streets that have 0 fees mean that the robot is not allowed to travel there. The robot’s goal is to minimize the total fees that it is charged for delivering all of its products, respecting the constraints.

Example

The input file (input.txt) may look like the following:

5

0 22566 46247 86550 85679

0 0 17214 0 0

77002 0 0 31972 0

0 0 0 0 12066

63767 0 0 6302 0

The output file (output.txt) could look like the following (and remember that there will be multiple correct answers for any given input):

0 4 3 4 0 3 4 0 2 3 4 0 1 2 0

Solution

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Robot { private static Scanner scanner = new Scanner(System.in); private static int[][] getCostMatrix(String fileName) throws FileNotFoundException { FileInputStream fis = new FileInputStream(fileName); Scanner sc = new Scanner(fis); //file to be scanned int num = Integer.parseInt(sc.nextLine()); int[][] output = new int[num][num]; int i = 0; while (sc.hasNextLine()) { String[] values = sc.nextLine().split(" "); //returns the line that was skipped int j = 0; for (String value : values) { output[i][j] = Integer.parseInt(value); j++; } i++; } sc.close(); //closes the scanner return output; } private static List minPath(int[][] matrix) { List path = new ArrayList<>(); path.add(0); for(int i = matrix.length - 1; i > 0; i--) { List bestTo = bestPathTo(0, i, matrix, new Path(new ArrayList<>(), 0)).points; //System.out.println(i + " From 0" + bestTo); path.addAll(bestTo); List bestBack =bestPathTo(i, 0, matrix, new Path(new ArrayList<>(), 0)).points; //System.out.println(i + " Back To 0" + bestBack); path.addAll(bestBack); } return path; } static class Path { public List points; public int cost; Path(List points, int cost) { this.points = points; this.cost = cost; } } private static Path min(List paths) { int min = 0; int minCost = Integer.MAX_VALUE; for(int i = 0; i < paths.size(); i++) { if(paths.get(i).cost < minCost) { min = i; minCost = paths.get(i).cost; } } return paths.get(min); } private static Path bestPathTo(int from, int destination, int[][] matrix, Path currentPath) { if(from == destination) { return currentPath; } List paths = new ArrayList<>(); for(int i = 0; i < matrix.length; i++) { if(matrix[from][i] != 0 && from != i && !currentPath.points.contains(i)) { List points = new ArrayList<>(currentPath.points); points.add(i); paths.add(bestPathTo(i, destination, matrix, new Path(points, currentPath.cost + matrix[from][i]))); } } if(paths.size() == 0) { return new Path(Collections.emptyList(), Integer.MAX_VALUE); } return min(paths); } private static void toOutput(String fileName, List path) throws IOException { FileWriter myWriter = new FileWriter(fileName); for(Integer i : path) { myWriter.write(i + " "); } myWriter.close(); } public static void main(String[] args) throws IOException { int[][] matrix = getCostMatrix("input.txt"); toOutput("output.txt", minPath(matrix)); } }

Similar Samples

At ProgrammingHomeworkHelp.com, delve into our extensive sample library showcasing our prowess in various programming languages and problem-solving techniques. Whether you're exploring introductory tasks or complex algorithms, our samples provide clear insights into our capabilities. These examples are designed to assist you in understanding concepts and mastering programming challenges effectively. Explore and discover how we can elevate your coding skills.