//并查集
//初始化
let board =[["O","O","O"],["O","O","O"],["O","O","O"]]
let row = board.length;
let column = board[0].length;
let arr = [];
for(let i = 0; i < row * column; i++) {
arr[i] = i;
}
//查找
function lookup(index) {
if(arr[index] == index) {
return index
}else {
lookup(arr[index])
}
}
//合并
function merge(index1, index2) {
let temp1 = lookup(index1);
let temp2 = lookup(index2);
arr[temp1] = arr[temp2];
}
for(let i = 0; i < row; i++) {
for(let j = 0; j < column; j++) {
let temp = i * column + j;
if(board[i][j] == 'O') {
if(j - 1 > 0 && board[i][j - 1] == 'O') {
// let temp: number = i * column + j;
merge(temp - 1, temp);
}
if(i - 1 > 0 && board[i - 1][j] == 'O') {
// let temp: number = i * column + j;
merge(temp - column, temp)
}
}else {
arr[temp] = undefined
}
}
}
console.log(arr)
arr.forEach((item, index) => {
if(item != undefined) {
console.log(index)
if(index < column || index >= (row - 1) * column || index % column == 0 || index % column == column - 1) {
console.log(index, 0)
sign(index)
}
}
})
function sign(index) {
let i = Math.floor(index / column);
let j = index % column;
console.log(i,j,arr[index])
board[i][j] = 'A';
if(index != arr[index]) {
sign(arr[index]);
}
if(arr.indexOf(index) != index) {
sign(arr.indexOf(index))
}
}
console