Content:
The following figure shows the highway mileage between major cities in China:
Your program will read in such a table, and then, according to the names of the two cities entered, give the mileage between the two cities.
Note: the mileage between any two cities has been given, and there is no need to calculate the transit through the third place.
Note: you do not need to enter the data in the figure above. The data is given in the program input.
Input format:
First, you will read the names of several cities. Each name is just an English word, without spaces or other symbols. When the name is read as "ාාාාාාාාාාාාා#ා#####. If the number of city names read is n.
Then you will read an integer matrix of nxn. Each number in the first row indicates the mileage from the first city in the above list to another city in turn. The mileage between the same cities in the table is 0.
Finally, you will read the names of two cities.
Output format:
Output the distance between the two cities.
Input example:
Hagzou Hugzou Jigxng ###
0 1108 708
1108 0 994
708 994 0
Hagzou Jigxng
Output example:
708
import java.util.ArrayList;//Introduction container import java.util.Scanner;//Introduce scanning function //Main class public class Main{ public static void main(String[] args) { City city = new City();//New city class object City city.setDistance();//Call setDistance method to set the distance city.getDistance();//Call getDistance method to output distance } } //Class City class City{ private ArrayList<String> city;//Container, store city name private int distance[][];//Two dimensional array, storing the distance matrix of input Scanner in = new Scanner(System.in); //Constructors, initializing public City() { city=new ArrayList<String>(); String ctName=in.next();//Enter city name //Add city name to container while(true) { if(ctName.equals("###")) { break;//Stop adding city name if you have entered "ාාා" } city.add(ctName);//Add to ctName=in.next();//input } //If input n cities, establish n*n distance matrix distance=new int[city.size()][city.size()]; } //Initialize distance matrix public void setDistance() { int mile=0; for(int i=0;i<distance.length;i++) {//That's ok for(int j=0;j<distance[i].length;j++) {//column mile=in.nextInt();//Input distance distance[i][j]=mile;//Initialize the corresponding coordinate distance matrix } } } //Get the distance between the two cities public void getDistance() { //Enter the starting city name and get its coordinates in the container int start=city.indexOf(in.next()); //Enter the destination city name and get its coordinates in the container int end=city.indexOf(in.next()); //Find the distance between two cities in the distance matrix according to the coordinates of start and end points System.out.println(distance[start][end]); } }