java - 基于旅行商的问题

标签 java

我正在编写一个基于旅行商问题的程序。有四个城市,用户可以在其中确定其 x 和 y 坐标。推销员总是从 city1 开始,到 city1 结束,所以有 6 条可能的路线。但是,每条路线都有一条等效路线,即 route1route6 的距离相同。我已经说明了这一点。我还尝试考虑如果 (route1route6) 和 (route2route4) 有同样的距离。该程序会告诉您这一点。

然而,每当有四条甚至所有六条路线的距离都相同时,程序只是告诉我这四条或六条 route 有两条路线的距离最短。这就是我需要帮助的地方。

import java.util.Scanner;
import java.lang.Math;

public class CityDistancesProgram 
{
   public static void main(String[] args)
   {
     Scanner keyboard = new Scanner(System.in);

     //x and y coordinates of each city
     int x1, y1, x2, y2, x3, y3, x4, y4;

     //Variable for the distances of each route
     double route1, route2, route3, route4, route5, route6; 

     //Since the distance from cityA to cityB is the same as the distance from cityB to cityA,
     //these are all the possible combinations of distances between each city
     double city1city2, city2city3, city3city4, city4city1, city2city4, city3city1;
     double city2city1, city3city2, city4city3, city1city4, city4city2, city1city3;

     double shortestRoute;

     System.out.println("Enter the value of each city's x-coordinate and y-coordinate");
     System.out.println(" ");

     //First city
     System.out.println("City 1's x-coordinate:");
     x1 = keyboard.nextInt();
     System.out.println("City 1's y-coordinate:"); 
     y1 = keyboard.nextInt();

     //Second city
     System.out.println("City 2's x-coordinate:");
     x2 = keyboard.nextInt();
     System.out.println("City 2's y-coordinate:"); 
     y2 = keyboard.nextInt();

     //Third city
     System.out.println("City 3's x-coordinate:");
     x3 = keyboard.nextInt();
     System.out.println("City 3's y-coordinate:"); 
     y3 = keyboard.nextInt();

     //Fourth city
     System.out.println("City 4's x-coordinate:");
     x4 = keyboard.nextInt();     
     System.out.println("City 4's y-coordinate:"); 
     y4 = keyboard.nextInt();

     System.out.println("City 1's coordinates are: (" +  x1 + ", "  +  y1 +")");
     System.out.println("City 2's coordinates are: (" +  x2 + ", "  +  y2 +")");
     System.out.println("City 3's coordinates are: (" +  x3 + ", "  +  y3 +")");
     System.out.println("City 4's coordinates are: (" +  x4 + ", "  +  y4 +")");

       //Computing all possible combinations of distance between each city
       city1city2 = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); //distance from city1 to city2 
       city3city1 = Math.sqrt((x1 - x3)*(x1 - x3) + (y1 - y3)*(y1 - y3)); //distance from city1 to city3 
       city4city1 = Math.sqrt((x1 - x4)*(x1 - x4) + (y1 - y4)*(y1 - y4)); //distance from city4 to city1
       city2city3 = Math.sqrt((x2 - x3)*(x2 - x3) + (y2 - y3)*(y2 - y3)); //distance from city2 to city3 
       city3city4 = Math.sqrt((x3 - x4)*(x3 - x4) + (y3 - y4)*(y3 - y4)); //distance from city3 to city4 
       city2city4 = Math.sqrt((x2 - x4)*(x2 - x4) + (y2 - y4)*(y2 - y4)); //distance from city2 to city4 

       city2city1 = city1city2; //distance from city2 to city1
       city3city2 = city2city3; //distance from city3 to city2
       city4city3 = city3city4; //distance from city4 to city3
       city1city4 = city4city1; //distance from city1 to city4
       city4city2 = city2city4; //distance from city4 to city2
       city1city3 = city3city1; //distance from city1 to city3

       //Computing the distance of each possible route
       route1 = city1city2 + city2city3 + city3city4 + city4city1;
       route2 = city1city2 + city2city4 + city4city3 + city3city1;
       route3 = city1city3 + city3city2 + city2city4 + city4city1;
       route4 = city1city3 + city3city4 + city4city2 + city2city1;
       route5 = city1city4 + city4city2 + city2city3 + city3city1;
       route6 = city1city4 + city4city3 + city3city2 + city2city1;

       System.out.println(" ");
       System.out.println("The first route has a total distance of " + route1 + " km");
       System.out.println("The second route has a total distance of " + route2 + " km");
       System.out.println("The third route has a total distance of " + route3 + " km");
       System.out.println("The fourth route has a total distance of " + route4 + " km");
       System.out.println("The fifth route has a total distance of " + route5 + " km");
       System.out.println("The sixth route has a total distance of " + route6 + " km");

       shortestRoute = Math.min(Math.min(route1, Math.min(route2,route3)), Math.min(route4,Math.min(route5,route6)));
       System.out.println(" ");

       if(shortestRoute == route1 || shortestRoute == route6)
       {
         System.out.println("route1 and route6 have the shortest distance");
       }
       else if(shortestRoute == route2 || shortestRoute == route4)
       {
         System.out.println("route2 and route4 have the shortest distance");
       }
       else if(shortestRoute == route3 || shortestRoute == route5)
       {
         System.out.println("route3 and route5 have the shortest distance");
       }
       else if((shortestRoute == route1 || shortestRoute == route6) && (shortestRoute == route2 || shortestRoute == route4))
       {
          System.out.println("route1, route6, route2 and route4 have the shortest distance");
       }
       else if((shortestRoute == route1 || shortestRoute == route6) && (shortestRoute == route3 || shortestRoute == route5))
       {
          System.out.println("route1, route6, route3 and route5 have the shortest distance");
       }
       else if((shortestRoute == route3 || shortestRoute == route5) && (shortestRoute == route2 || shortestRoute == route4))
       {
          System.out.println("route3, route5, route2 and route4 have the shortest distance");
       }
       else
       {
            System.out.println("There is no shortest distance, they are all the same");
       }
   }
}

最佳答案

你最后的 if 语句集合就是问题所在。它是 elseif 的集合,所以只要你匹配了一个表达式你就完成了。您需要确保您的表达式以最具体到最不具体的顺序排列。

你可以简化成这样(仅伪代码)

bool r1 = shortestRoute == route1 || shortestRoute == route6;
bool r2 = shortestRoute == route2 || shortestRoute == route4;
bool r3 = shortestRoute == route3 || shortestRoute == route5;

if (r1 && r2 && r2) {
    print "all the same"
}
else if (r1 && r2) {
}
else if (r1 && r3) {
}
else if (r2 && r3) {
}
// Now individual checks for r1 r2 and r3

关于java - 基于旅行商的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21152873/

相关文章:

java - java中如何解析 "data exception: invalid character value for cast"?

java - do while 循环问题

java - 从命令行编译一个 eclipse GWT 项目,没有 eclipse : compile error

java - 如何在基于 Spring DAO 的应用程序中实现 xml?

java - 如何使用 java 日志记录将消息记录在单独的日志文件中?

java - 支持语法高亮、链接和在线拼写检查的文本编辑器小部件

java - 如何使用 Caffeine 缓存管理器测试内存缓存? (即在缓存、缓存驱逐后获取缓存中的条目数)

Java JNDI - 在 Microsoft LDAP 中更改用户密码时的限制 - 身份验证异常

java - 使用 ReSTLet 时如何跟踪日志中的请求?

java - 使用 wss4j SimplePasswordValidationCallbackHandler 验证选择性 Spring Web 服务