2013-02-15 2 views
0

미로를 해결하기 위해 대기열을 사용하여 광범위한 첫 번째 검색을 수행하는 프로그램을 작성해야합니다. 나는 거의 그것을 끝냈다 고 생각하지만, 나는 그것을 실행할 때 메인 타입을 포함하지 않는다고 말한다. 또한 어떤 사람이 줄 47과 48 (i = current.i와 j = current.j)의 코드에 문제가 없다고 설명해 주지만 괜찮을 것이라고 말하면서 "해결할 수 없거나 필드에서는 아니지만 while 루프를 선언해도 괜찮을 것입니다. 중요한 것은 아니지만 선생님이 repaint() 메서드를 사용하여 미로가 해결되는 것을 볼 수 있도록 요청했습니다. 어떻게해야하는지 잘 모르겠습니다. 따라서 방법을 사용하지만이이 프로그램을 실행을 얻기 위해 어떤 도움을 크게 감상 할 수있다. 그 코드를 포함 시켰습니다. 여기너비를 이용한 첫 번째 미로 솔버

import java.awt.Point; 
import java.util.Queue; 
import java.util.concurrent.ConcurrentLinkedQueue; 

public class MazeSolver { 
public static final int WALL = 1, OPEN = 0, GOAL = 2, START = 3; 
public static final int ACTIVE = 4, SOLUTION = 5; 
    private int[][] maze = { 
     {1, 1, 1, 1, 1, 1, 1, 1}, 
     {1, 3, 0, 0, 0, 0, 0, 1}, 
     {1, 1, 0, 1, 1, 1, 0, 1}, 
     {1, 1, 0, 1, 0, 1, 0, 1}, 
     {1, 0, 0, 0, 0, 1, 0, 1}, 
     {1, 1, 0, 1, 1, 1, 0, 1}, 
     {1, 0, 0, 0, 0, 1, 2, 1}, 
     {1, 1, 1, 1, 1, 1, 1, 1}, 
    }; 

public static Point startSearch(int[][] grid) { 
    int x = -1, y = -1; 
    for (int col = 0; col < grid.length; col++) { 
     for (int row = 0; row < grid.length; row++) { 
      if (grid[col][row] == 3) { 
       x = col; 
       y = row; 
       return new Point(x, y); 
      } 
     } 
    } 
    return null; 
} 

public static Point[] algorithm(int[][] maze) { 
    ConcurrentLinkedQueue<Point> path = new ConcurrentLinkedQueue<Point>(); 
    ConcurrentLinkedQueue<Point> predecessor = new ConcurrentLinkedQueue<Point>(); 

    Point start = startSearch(maze); 
    Point current; 
    Point north, south, east, west; 

    int i = 0; 
    int j = 0; 
    path.offer(start); 
    while (!path.isEmpty()) { 
     current = path.poll(); 
     i = current.i; 
     j = current.j; 
     if (i == maze.length - 1 && j == maze.length - 1 
       && maze[i][j] == '0') { 
      Point[] trail = new Point[path.size()]; 
      while (path.isEmpty()) { 
       for (int k = 0; k < path.size(); k++) { 
        trail[k] = path.poll(); 
       } return trail; 
      } 
     } 

     east = new Point(i, j + 1); 
     south = new Point(i + 1, j); 
     west = new Point(i, j - 1); 
     north = new Point(i - 1, j); 
     if (j + 1 >= 0 && j + 1 < maze.length && maze[i][j + 1] == '0' 
       && predecessor.contains(east) == false) { 
      predecessor.offer(east); 
      path.offer(current); 
      path.offer(east); 
     } else if (i + 1 >= 0 && i + 1 < maze.length 
       && maze[i + 1][j] == '0' 
       && predecessor.contains(south) == false) { 
      predecessor.offer(south); 
      path.offer(current); 
      path.offer(south); 
     } else if (j - 1 >= 0 && j - 1 < maze.length 
       && maze[i][j - 1] == '0' 
       && predecessor.contains(west) == false) { 
      predecessor.offer(west); 
      path.offer(current); 
      path.offer(west); 
     } else if (i - 1 >= 0 && i - 1 < maze.length 
       && maze[i - 1][j] == '0' 
       && predecessor.contains(north) == false) { 
      predecessor.offer(north); 
      path.offer(current); 
      path.offer(north); 
     } 

    } 

    return null; 
} 

public int[][] getMaze() { 
    return maze; 
} 

public static void main(String args) { 
    new MazeSolver(); 
} 

과 것은 우리가 (재 페인트에 사용하기로하는 방법),이 부분의 ISN '입니다 결정적이지만 어떤 방식으로 작동하는지에 대한 설명이나 도움이 될 것입니다.

,
+3

앞에'java.awt.Point'이 필드'x'와'y'가 아닌'i'하고있다'j' ... –

+0

가 그냥 궁금해서, 왜이 게시물을 downvoted입니까? –

답변

0

난 당신이

public static void main(String args) 

로 주요 기능을 선언했지만 실제로 인수로 문자열의 배열을해야하기 때문에 이유는 프로그램이 실행되지 않을 수 있다고 생각합니다. 즉. 이

public static void main(String[] args) 
관련 문제