2014-06-07 3 views
0
import java.util.InputMismatchException; 

    import java.util.Scanner; 

    import java.util.Stack; 

public class TSPNearestNeighbour { 
{ 

     private final Stack<Integer> stack; 
     private int numberOfNodes; 
     public TSPNearestNeighbour() 

     { 

      stack = new Stack<Integer>(); 

     } 



     public void tsp(int adjacencyMatrix[][]) 

     { 

      numberOfNodes = adjacencyMatrix[1].length - 1; 

      int[] visited = new int[numberOfNodes + 1]; 

      visited[1] = 1; 

      stack.push(1); 

      int element, dst = 0, i,cost=0; 

      int min = Integer.MAX_VALUE; 

      boolean minFlag = false; 

      System.out.print(1 + "\t"); 



      while (!stack.isEmpty()) 

      { 

       element = stack.peek(); 

       i = 1; 

       min = Integer.MAX_VALUE; 

       while (i <= numberOfNodes) 

       { 

        if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) 

        { 

         if (min > adjacencyMatrix[element][i]) 

         { 

          min = adjacencyMatrix[element][i]; 

      cost=cost+adjacencyMatrix[element][i]; 

          dst = i; 

          minFlag = true; 

         } 

        } 

        i++; 

       } 

       if (minFlag) 

       { 

        visited[dst] = 1; 

        stack.push(dst); 

        System.out.print(dst + "\t"); 


        minFlag = false; 

        continue; 

       } 

       stack.pop(); 

      } 
System.out.println("total cost" +cost); 
     } 



     public static void main(String args[]) 

     { 

      int number_of_nodes; 

      Scanner scanner = null; 

      try 

      { 

       System.out.println("Enter the number of nodes in the graph"); 

       scanner = new Scanner(System.in); 

       number_of_nodes = scanner.nextInt(); 

       int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1]; 

       System.out.println("Enter the adjacency matrix"); 

       for (int i = 1; i <= number_of_nodes; i++) 

       { 

        for (int j = 1; j <= number_of_nodes; j++) 

        { 

         adjacency_matrix[i][j] = scanner.nextInt(); 

        } 

       } 

       for (int i = 1; i <= number_of_nodes; i++) 

       { 

        for (int j = 1; j <= number_of_nodes; j++) 

        { 

         if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) 

         { 

          adjacency_matrix[j][i] = 1; 

         } 

        } 

       } 

       System.out.println("the citys are visited as follows"); 

       TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour(); 

       tspNearestNeighbour.tsp(adjacency_matrix); 

      } catch (InputMismatchException inputMismatch) 

      { 

       System.out.println("Wrong Input format"); 

      } 

      scanner.close(); 

     } 

    }  



> illegal start of expression in the line: 
>  **private final Stack<Integer> stack;** 
+1

을 컴파일받을 수있다이 몇 가지 일반적인 규칙에 따라 코드를 들여 씁니다. 다음 당신은 또한 당신의 문제를 볼 것이다. – Seelenvirtuose

+2

나는 거의 확실하다. 만약 제시된 코드가 당신이 가지고있는 것이라면 문제는 여기에있다 :'public class TSPNearestNeighbour {{'. 여분의 버팀대가 있습니다. 이 문맥에서 중괄호는 정적 블록을 나타내야합니다. 개인 변수를 만드는 것은 의미가 없습니다 (블록 내부에 있음). – Jared

답변

5

에서 private 변수를 선언하는 동안 어쩌면 당신은 어떤 노력을해야이 열린 중괄호

public class TSPNearestNeighbour { { 

하나를 제거하고 당신이 당신의 코드가