2012-03-01 3 views
0

m x n 매트릭스로 표현 된 비트 맵을 채우는 메소드를 작성했다. 내가 뭘 하려는지 초기 픽셀을 스택에 밀어 넣은 다음 잠시 루프에서 스택의 요소를 팝하고 초기 픽셀의 초기 색과 같은 색이라면이 픽셀을 색칠하고 인접 픽셀을 푸시합니다.스택이 동일한 요소를 계속 팝한다.

public void fill(int x, int y, char c) { 
    char tempColor = this.bitmap[y - 1][x - 1]; 
    Point currentPoint; 

    Stack<Point> fillStack = new Stack<Point>(); 

    fillStack.push(new Point(x, y)); 

    do { 
     currentPoint = fillStack.pop(); 
//  System.out.println(currentPoint.x + " " + currentPoint.y); 
//  System.out.println("Current state of the stack:"); 
//  for (Point p: fillStack) 
//   System.out.println(p.x + " " + p.y); 
     this.bitmap[currentPoint.y - 1][currentPoint.x - 1] = c; 
     if (currentPoint.y - 1 > 0 && this.bitmap[currentPoint.y - 2][currentPoint.x - 1] == tempColor) { 
      fillStack.push(new Point(x, y - 1)); 
//   System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1)); 
     } 
     if (currentPoint.y - 1 < n - 1 && this.bitmap[currentPoint.y][currentPoint.x - 1] == tempColor) { 
      fillStack.push(new Point(x, y + 1)); 
//   System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y + 1)); 
     } 
     if (currentPoint.x - 1 > 0 && this.bitmap[currentPoint.y - 1][currentPoint.x - 2] == tempColor) { 
      fillStack.push(new Point(x - 1, y)); 
//   System.out.println("Pushing " + (currentPoint.x - 1) + " " + currentPoint.y); 
     } 
     if (currentPoint.x - 1 < m - 1 && this.bitmap[currentPoint.y - 1][currentPoint.x] == tempColor) { 
      fillStack.push(new Point(x + 1, y));  
//   System.out.println("Pushing " + (currentPoint.x + 1) + " " + currentPoint.y); 
      } 
     } while (!fillStack.isEmpty()); 
    } 
} 

하지만 내가 볼 수없는 이유 때문에 작동하지 않습니다. 출력 (디버깅 라인은 주석 처리되지 않음)은 다음과 같습니다 :

 
3 3 
Current state of the stack: 
Pushing 3 2 
Pushing 3 4 
Pushing 4 3 
4 3 
Current state of the stack: 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 

... 이것은 무한 루프에서 계속됩니다. 무엇이 문제 일 수 있습니까?

답변

3

인쇄 진술서는 한 가지, 귀하의 코드는 다른 것을 말합니다! ;) 예를 들어

:

fillStack.push(new Point(x, y - 1)); 
System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1)); 

당신이 차이를 발견 할 수 있는지 ...

+0

내 아, 내가 한 시간 나오지 않았어이 일이보고 된 것을 믿을 수 없어 이걸 보지 마. 고마워요. D – hattenn

+2

@hatten - 하하, 때로는 필요한 또 다른 눈 쌍입니다 ... – Nim

관련 문제