2017-11-22 15 views
-2

私はこのコードを使用して、Javaで分離セットデータ構造を使用して迷路を構築しています。不完全なセットデータ構造を使用して終了する//迷路を構築するエラー( "class、interface or enum expected")が出てきました。誰も助けてくれますか?Javaの「クラス、インタフェース、または列挙型が予期される」エラー

//Implementation of Disjoint Set 

public class DisjSet { 
    private int[] set; 
    private int[] sizes; 
    private int size; 

    public DisjSet(int size) { 

     this.set = new int[size]; 
     for (int i = 0; i < size; i++) { this.set[i] = i; } 

     this.sizes = new int[size]; 
     for (int i = 0; i < size; i++) { this.sizes[i] = 1; } 

     this.size = size; 
    } 

    public int find(int item) { 

     int root = item; 

     // find the root 
     while (set[root] != root) { 

      root = set[root]; 

     } 

     // now shorten the paths 
     int curr = item; 

     while (set[curr] != root) { 

      set[curr] = root; 

     } 

     return root; 

    } 

    public int join(int item1, int item2) { 

     int group1 = find(item1); 
     int group2 = find(item2); 

     --size; 

     if (sizes[group1] > sizes[group2]) { 

      set[group2] = group1; 

      sizes[group1] += sizes[group2]; 

      return group1; 

     } else { 

      set[group1] = group2; 

      sizes[group2] += sizes[group1];     

      return group2; 

     } 

    } 

} 

//Building maze using Disjoint Set data structure 

     Maze createRandomMaze(int rows, int columns) { 
     Maze maze = new Maze(rows, columns); 
     // create all walls 
     List<Wall> walls = maze.getAllInnerWalls(); 
     // remove all the walls you can 
     DisjSet diset = new DisjSet(rows*columns); 
     while (diset.size() > 1) { 
      int wallIndex = random.nextInt(walls.size()); 
      int cell1 = walls.get(wallIndex).cell1; 
      int cell2 = walls.get(wallIndex).cell2; 
      if (diset.find(cell1) != diset.find(cell2)) { 
       // we can remove the wall 
       maze.removeWall(walls.get(wallIndex)); 
       diset.join(cell1, cell2); 
      } 
      walls.remove(wallIndex); 
     } 
     return maze; 
} 
+0

あなたの 'createRandomMaze'メソッドはあなたの' DisjSet'クラスの後にあります、それはおそらくクラスの中にあるべきです。 – Berger

答えて

0

これら:

Maze createRandomMaze(int rows, int columns) { 
    Maze maze = new Maze(rows, columns); 
    // create all walls 
    List<Wall> walls = maze.getAllInnerWalls(); 
    // remove all the walls you can 
    DisjSet diset = new DisjSet(rows*columns); 
    while (diset.size() > 1) { 
     int wallIndex = random.nextInt(walls.size()); 
     int cell1 = walls.get(wallIndex).cell1; 
     int cell2 = walls.get(wallIndex).cell2; 
     if (diset.find(cell1) != diset.find(cell2)) { 
      // we can remove the wall 
      maze.removeWall(walls.get(wallIndex)); 
      diset.join(cell1, cell2); 
     } 
     walls.remove(wallIndex); 
    } 
    return maze; 

は、メソッドの内部に置くべきである、唯一の宣言はクラスレベルで許可されています。

mainメソッドを既にお持ちの場合は、上記のコードをそこに移動してください。そうしない場合は、main方法宣言:あなたのコードを実行すると

public static void main(String[] args) { 
    // put the above code here... 
} 

を、mainメソッドが呼び出され、上記の行が実行されます。

関連する問題