私はGo Gameプロジェクトの問題を扱っています。Javaの文字の2次元配列で 'bubbles'を検索する
私は2次元配列の文字で表されるボード(goban)を持っています。次の移動の前に、配列内の「バブル」をチェックしたいと思います。気泡は、特定の同一の文字の別のグループによって4方向に囲まれた同一の文字の4連結領域でなければなりません。 この「バブル」が存在する場合、内部の文字は他の文字に置き換えてください。しかし、より多くの領域が存在する可能性があり、それらのすべてが閉じられているわけではありません。 例えば、私はこのボードがあります。
1 2 3 4 5 6 7 8 9 10 11 12 13
- - - - - - - - - - - - - - -
A | + + + + + + + + + + + + + |
B | + + + + + + + + + + + + + |
C | + + + + + + + + + + + + + |
D | + + + + + + + + + + + + + |
E | + + + + + + + + + + + + + |
F | + + O O O O + + + + + + + |
G | + O X X X X O + + + + + + |
H | + + O O X X O + + + + + + |
I | + + + + O X X O + + + + + |
J | + + + + O X O + + + + + + |
K | + + + + + O + + + + + + + |
L | + + + + + + + + + + + + + |
M | + + + + + + + + + + + + + |
- - - - - - - - - - - - - - -
をそして私は、Xのバブルを見つけ、それらを数えると「Zのでそれらを交換したいと思います。
私はそれを見つけました。私はいくつかのConnected-ComponentラベリングアルゴリズムやFloodFillがその仕事をすることができると思いますが、実装方法はわかりません。これは方法ですか、それともそれほど複雑ではないのでしょうか? ありがとうございました
編集: 特定の文字の領域を見つけてその自由度を数えるパターンを見つけようとしましたが、場所が重複していると常に失敗しました。 データ構造を変更することが解決策になるかもしれませんが、可能であれば、今のようにしたいと思います。
私の現在のソリューションのアイデア:
regions = Collection<Set<Point>>
foreach (Point p : allBoardLocations)
if (charAtLocation(p) != 'X') continue
foundInRegion = false
for (Set<Point> s : regions)
if (s.contains(p))
foundInRegion=true
break;
if (!foundInRegion)
newRegion = new Set<Point>()
stack = new Stack<Point>()
stack.push(p)
while (!stack.empty())
foreach (Point n : neighboringPoints(stack.pop()))
if (charAtLocation(n) == 'X')
if (!newRegion.contains(n))
newRegion.add(n);
stack.push(n);
Bascially、あなたはセットのコレクションを維持する:ここで
public void solve(){
if (boardContainsEnclosedAreas(goban, onMovePlayerStone, oppositePlayerStone){
onMovePlayerScore += countElementsInAreaAndReplaceThem(onMovePlayerStone, 'Z');
}
}
public boolean boardContainsEnclosedAreas(char[][] playingBoard, char searchedChar, char surroundingChar){
// this method should find the bubble in the playingBoard array
}
public int countElementsInAreaAndReplaceThem(char searchedChar, char replacingChar){
// the method should go through the bubble and replace all inner chars
// returns amount of replaced chars
}
は、あなたが他のデータ構造を検討したことがありますか?たぶん、同じ色のすべての連結タイルを含むChainクラスを持つことができます。いくつかの自由があり、自由が0になるとチェーンが削除されます。 –
「希望の出力」を投稿しても素晴らしいでしょう。 – Bhushan
ありがとう、私は説明を更新しました – jC30