2014-03-07 6 views
-1

무슨 일이 일어나고 있는지 잘 모르겠지만 콘솔에서 (실행 IDE에서) 내 프로그램을 멈추게 할 수있는 빨간색 '정지'사각형이 있습니다. 프로그램이 실행 중이고 정사각형입니다. 빨간 채로 ..?프로그램이 끝나지 않습니다

편집 :

내 미로 :

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW 
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW 
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW 
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW 
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW 
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW 
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW 
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW 
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW 
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW 
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 

편집 : 여기 내 코드입니다 :

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.HashSet; 
import java.util.Scanner; 
import java.util.Stack; 
import java.awt.Point; 

public class MazeExplorer { 
    static Point startPoint = new Point(); 
    static Point finishPoint = new Point(); 
    final static int mazeHeight = 12; 
    final static int mazeWidth = 58; 
    static char[][] mazePoints = new char[mazeHeight][mazeWidth]; 
    Stack<Point> pointsNotTraversed = new Stack<Point>(); 
    Point pt = new Point(); 
    static HashSet<Point> previousLocations = new HashSet<Point>(); 
    static Stack<Point> nextPoints = new Stack<Point>(); 

    public static void main(String[] args) throws FileNotFoundException{ 

     System.out.println("Please enter the file name of your Maze"); 
     Scanner console = new Scanner(System.in); 
     File f = new File(console.nextLine()); 
     Scanner sc = new Scanner(f); 

     if(!sc.hasNextLine()){ 
      System.out.println("Sorry, please enter a file name with the extension, that contains a maze!"); 
     } 
     System.out.println("So, you want to know if your maze is solvable.....?"); 

     for (int row = 0; row < mazeHeight && sc.hasNext(); row++) { 
      final String mazeRow = sc.next(); //Get the next row from the scanner. 
      mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[]. 
     } 
      //identify the finish point 
     for(int i = 0; i < mazeHeight; i++){ 
      for(int j = 0; j<mazeWidth; j++){ 
       if(mazePoints[i][j] == 'F'){ 
        finishPoint = new Point(i, j); 
       }  
      } 
     } 
     // Identify the start point 
     for(int i = 0; i< mazeHeight; i++){ 
      for(int j = 0; j < mazeWidth; j++){ 
       if(mazePoints[i][j] == 'S'){ 
       startPoint = new Point(i , j); 
       } 
      } 
     } 
     isTraversable(startPoint);  
    } 
     public static boolean isTraversable(Point current){ 
      boolean isSolvable = false; 
      nextPoints.push(current); 

      do { 


       if(current.y < 11) { 
        if((mazePoints[current.y + 1][current.x] != ' ') && (mazePoints[current.y + 1][current.x] != 'W')){ // below direction 
        nextPoints.push(new Point(current.y + 1, current.x)); 
        mazePoints[current.y + 1][current.x] = ' ';   
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.y > 0){ 


       if (mazePoints[current.y - 1][current.x] != ' ' && mazePoints[current.y - 1][current.x] != 'W'){ //up dir 
        nextPoints.push(new Point(current.y - 1, current.x)); 
        mazePoints[current.y - 1][current.x] = ' '; //'X' marks where you've already been 
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.x < 57){ 
       if(mazePoints[current.y][current.x + 1] != ' ' && mazePoints[current.y][current.x + 1] != 'W'){ // to the right 
        nextPoints.push(new Point(current.y, current.x + 1)); 
        mazePoints[current.y][current.x + 1] = ' '; 
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.x > 0){ 


       if(mazePoints[current.y][current.x - 1] != ' ' && mazePoints[current.y][current.x - 1] != 'W') { // to the left 
        nextPoints.push(new Point(current.y, current.x - 1)); 
        mazePoints[current.y][current.x - 1] = ' ';  
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.equals(finishPoint)){ 
        isSolvable = true; 
        System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!"); 
       } 




      } while(!current.equals('F') && !nextPoints.isEmpty());  


      return isSolvable;   
     } 
} 
+0

매우 모호합니다. 코드 때문이라고 생각한다면 여기에 코드를 게시하십시오. 코드와 관련하여 문제가 지속된다면,이를 위해 수퍼 유저에게로가는 것이 가장 좋습니다. – Manhattan

+0

음, 무한 루프가 있습니까? 당신의 프로그램은 무엇입니까? – Kyranstar

+0

임의의 코드 스 니펫을 붙여 넣었으며 프로그램이 종료되지 않는다고 명시했습니다. 이것은 충분한 정보가 아닙니다. –

답변

1

앞서 제안한 것처럼 재귀 적 방법을 다시 구성하면됩니다. 나는 이것을하기의 자유를 취했다. 그러나 만약 당신이 프로그램하는 법을 배우고 싶다면, 스스로 문제를 해결하려고 시도 할 것이다. 또는 코딩을 시작하기 전에 솔루션의 논리를 이해하십시오.

당신이 주된 문제는 당신이 방금 뛰어 들기 전에 어떤 방향으로 가고 싶은지 알지 못한다는 것입니다. 그것은 다른 것들이 서로 호환되지 않는 모든 종류의 오류를 일으키고있었습니다.

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW 
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW 
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW 
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW 
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW 
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW 
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW 
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW 
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW 
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWOFW 
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 

다음과 같은 출력이 나타냅니다 :

So, you want to know if your maze is solvable.....? 
MAZE IS SOLVABLE, YAHOOOOOO!!!! 
true 

나는 파일을 다른 방식으로 수입을하지만, 당신이 다시 당신이 사용하는 어떤 방법으로 변경할 수 있습니다 미로 입력으로

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.HashSet; 
import java.util.Scanner; 
import java.util.Stack; 
import java.awt.Point; 

public class TestCode { 
    static Point startPoint = new Point(); 
    static Point finishPoint = new Point(); 
    final static int mazeHeight = 12; 
    final static int mazeWidth = 58; 
    static char[][] mazePoints = new char[mazeHeight][mazeWidth]; 
    Stack<Point> pointsNotTraversed = new Stack<Point>(); 
    Point pt = new Point(); 
    static HashSet<Point> previousLocations = new HashSet<Point>(); 
    static Stack<Point> nextPoints = new Stack<Point>(); 

public static void main(String[] args) throws FileNotFoundException{ 

    System.out.println("Please enter the file name of your Maze"); 
    Scanner console = new Scanner(System.in); 
    File f = new File(console.nextLine()); 
    Scanner sc = new Scanner(f); 

    if(!sc.hasNextLine()){ 
     System.out.println("Sorry, please enter a file name with the extension, that contains a maze!"); 
    } 
    System.out.println("So, you want to know if your maze is solvable.....?"); 

    for (int row = 0; row < mazeHeight && sc.hasNext(); row++) { 
     final String mazeRow = sc.next(); //Get the next row from the scanner. 
     mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[]. 
    } 
     //identify the finish point 
    for(int i = 0; i < mazeHeight; i++){ 
     for(int j = 0; j<mazeWidth; j++){ 
      if(mazePoints[i][j] == 'F'){ 
       finishPoint = new Point(i, j); 
      }  
     } 
    } 
    // Identify the start point 
    for(int i = 0; i< mazeHeight; i++){ 
     for(int j = 0; j < mazeWidth; j++){ 
      if(mazePoints[i][j] == 'S'){ 
      startPoint = new Point(i , j); 
      } 
     } 
    } 
    System.out.println(isTraversable(startPoint));  
} 
    public static boolean isTraversable(Point current){ 

     mazePoints[current.x][current.y] = ' '; 

     if(current.y < 56 && current.y > 0 && current.x > 0 && current.x < 11){ 
      if (mazePoints[current.x - 1][current.y] == 'O'){ // Up dir 
       Point upPoint = new Point(current.x-1, current.y); 
       nextPoints.push(upPoint); 
      } 

      if(mazePoints[current.x+1][current.y] == 'O'){ // Down dir 
       Point downPoint = new Point(current.x+1, current.y); 
       nextPoints.push(downPoint); 
      } 

      if(mazePoints[current.x][current.y + 1] == 'O'){ // to the right 
       Point rightPoint = new Point(current.x, current.y+1); 
       nextPoints.push(rightPoint); 
      } 

      if(mazePoints[current.x][current.y - 1] == 'O'){ // to the left 
       Point leftPoint = new Point(current.x, current.y-1); 
       nextPoints.push(leftPoint); 
      } 

      if(mazePoints[current.x - 1][current.y] == 'F' || 
       mazePoints[current.x + 1][current.y] == 'F' || 
       mazePoints[current.x][current.y - 1] == 'F' || 
       mazePoints[current.x][current.y + 1] == 'F'){ 
       System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!"); 
       return true; 
      } 

     } 
     if(nextPoints.isEmpty()){ 
      return false; 
     } 
     else{ 
      current = nextPoints.pop(); 
     } 

     return(isTraversable(current)); 

    } 
} 

이전에

+0

이 메소드는 시작점이 전달 된 main 메소드에서 호출해야합니까? – bazookyelmo

+0

예. print 문'System.out.println (isTraversable (startPoint));' – leigero

+0

에서 "true"와 "yahoooo"줄을 출력합니다. – leigero

1

가 여러 프로그램을 시작했다고 할 수 있음은 당신이 가지고있는 "콘솔 표시 표준 출력이 변경 될 때 "확실하지는 않지만 한 시나리오를 설명합니다. 작업 관리자를 시작하고 거기서 프로그램을 찾으면 그 방법으로 종료 할 수 있습니다.

+0

그 시도는 무한히 진행됩니다 .... – bazookyelmo

+1

사과,하지만 추측은 대답하지 않습니다. 대신 주석으로 사용하는 것이 좋습니다. – Manhattan

+1

내가 충분한 담당자가 있다면 나는이 코멘트를 만들었을 것이다 :) 긴 샷 그러나 그것은 대답일지도 모른다! –

0

실행이 중단되어 완료되지 않으면 중단 점없이 Eclipse 디버거에서 프로그램을 실행할 수 있습니다.

디버그 탭을 열고 (창>보기 표시> 디버그에서 열기) '스레드 [메인] (실행 중)'을 마우스 오른쪽 버튼으로 클릭하고 '일시 중단'을 선택하여 스레드를 일시 중단하십시오.

그런 다음 스택 맨 아래에서 위로 작업하십시오. 그러면 블록이있는 위치를 찾기에 충분할 정도로 좁힐 수 있습니다.

+0

이미 시도했지만 아무 소용이 없다 – bazookyelmo

0
import java.util.Scanner; 
import java.util.Stack; 


public class test5 { 

private int numRows; 
private int numCols; 

public test5(){ 
Scanner input = new Scanner(System.in); 
System.out.println("Enter number of rows: "); 
numRows = input.nextInt(); 
System.out.println("Enter number of cols: "); 
numCols = input.nextInt(); 

Stack<Point>stack = new Stack<Point>(); 
stack.push(new Point(0,0)); 


while(!stack.isEmpty()){ 

    if(currentPath(stack.pop(),stack)){ 
     System.out.println("Maze is solvable"); 
    } 



} 

} 

public static void main(String[]args){ 

    new test5(); 

} 

private boolean currentPath(Point point, Stack<Point>stack){ 
    int currentRow = point.getRow(); 
    int currentCol = point.getCol(); 

    while(currentRow!=numRows-1 || currentCol!=numCols-1){ 
     boolean canGoRight = canGoRight(currentRow,currentCol); 
     boolean canGoUp = canGoUp(currentRow,currentCol); 
     boolean canGoDown = canGoDown(currentRow,currentCol); 

     if(canGoRight){ 
      if(canGoUp){ 
       stack.push(new Point(currentRow-1,currentCol)); 
      } 
      if(canGoDown){ 
       stack.push(new Point(currentRow+1,currentCol)); 
      } 
      currentCol = currentCol+1; 
     } 
     else{ 
      if(canGoUp){ 
       if(canGoDown){ 
        stack.push(new Point(currentRow+1,currentCol)); 

       } 
       currentRow = currentRow-1; 

      } 
      else if(canGoDown){ 
       currentRow = currentRow+1; 
      } 
      else{ 
       return false; 
      } 

     } 
    } 

    return true; 
} 



private boolean canGoUp(int row, int col){ 
    return row-1>=0; 
} 

private boolean canGoRight(int row, int col){ 
    return col+1<numCols; 
} 

private boolean canGoDown(int row, int col){ 
    return row+1<numRows; 
} 

class Point{ 
    private int row; 
    private int col; 

    public Point(int row, int col){ 
     this.row = row; 
     this.col = col; 
    } 

    public int getRow(){ 
     return row; 
    } 

    public int getCol(){ 
     return col; 
    } 
} 
} 
+0

멍청한 질문 일지 모르지만 내 미로 파일은 어디에 있습니까? – bazookyelmo

+0

eclipse를 사용하는 경우 프로젝트 폴더에 넣을 수 있습니다. – Solace

관련 문제