0
m×n行列で表されるビットマップを塗りつぶすメソッドを記述しました。私がしようとしているのは、最初のピクセルをスタックにプッシュし、whileループでスタックから要素をポップし、初期ピクセルの最初の色と同じ色ならば色を付け、隣接するピクセルをプッシュします。スタックは同じ要素をポップし続けます
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
...このように無限ループになります。何が問題なの?
は私のああ、私は時間とcouldnのためにこの事を見てきたことを信じることはできませんこれを見ない。ありがとうございました:D – hattenn
@hatten - ハハ、時には必要なものはもう一組の目です... – Nim