Javascript 如何找到渗透路径并突出显示它?

Javascript 如何找到渗透路径并突出显示它?,javascript,algorithm,union,Javascript,Algorithm,Union,我使用快速联合算法来确定前5个站点中的一个站点是否与后5个站点中的一个站点连接 前五个站点具有相同的根,它是倒数第二个数组元素。最后五个站点也有相同的根。它是最后一个数组元素 for (let i = 0; i < 5; i++) { this.union(i,this.array.length-2); } for (let j = this.array.length-this.number-2; j < this.array.length-2; j+

我使用快速联合算法来确定前5个站点中的一个站点是否与后5个站点中的一个站点连接

前五个站点具有相同的根,它是倒数第二个数组元素。最后五个站点也有相同的根。它是最后一个数组元素

for (let i = 0; i < 5; i++) {
        this.union(i,this.array.length-2);
    }
    for (let j = this.array.length-this.number-2; j < this.array.length-2; j++) {
        this.union(j,this.array.length-1);
    }
根法

root(index){
    while(this.array[index] !== index) index = this.array[index];
    return index;
}

看起来您只需要一个基本的填充算法,但有两个注意事项。1) 您只希望它向下填充,就像水在流动一样2)您希望对第一(顶部)行中的每个空白处调用fill(),就像水从顶部倒出一样

var EMPTY = 0; // empty unfilled spot
var SOLID = 1; // solid spot
var FILLED = 2; // filled spot
var ROWS = 5; // # of rows of data indexed 0 to 4
var COLS = 5; // # of columns of data indexed 0 to 4
var data = [
    [1,0,0,1,1],
    [1,1,0,1,1],
    [0,1,0,0,1],
    [0,1,1,0,1],
    [0,1,1,0,1]
]; // data that matches the example

// check if a location is EMPTY
function isEmpty(grid, rowIndex, colIndex) {
    // valid row and col?
    if(rowIndex < 0 || rowIndex >= ROWS || colIndex < 0 || colIndex >= COLS) {
        return false;
    }
    // SOLID or FILLED already?
    if(grid[rowIndex][colIndex] !== EMPTY) {
        return false;
    }
    return true;
}

// replace EMPTY locations with FILLED locations
function fillDown(grid, startRow, startCol) {
    if(false === isEmpty(grid, startRow, startCol)) {
        return;
    }
    var nextLocation = [[startRow, startCol]]; // list of locations to process
    while(nextLocation.length > 0) { // loop
        var loc = nextLocation.pop();
        var r = loc[0]; // row index
        var c = loc[1]; // column index
        // fill the empty location
        grid[r][c] = FILLED;
        // try to fill the locations to the LEFT, RIGHT and DOWN from the current location
        if(isEmpty(grid, r, c - 1)) { // left
            nextLocation.push([r, c - 1]);
        }
        if(isEmpty(grid, r, c + 1)) { // right
            nextLocation.push([r, c + 1]);
        }
        if(isEmpty(grid, r + 1, c)) { // down
            nextLocation.push([r + 1, c]);
        }
    }
}

// call fillDown() for the first row of the data (water pouring accross the top)
for(var c = 0; c < COLS; c++) {
    fillDown(data, 0, c);
}

// show the result
alert(data.join('\n'));
var EMPTY=0;//空位
变量SOLID=1;//实心点
变量填充=2;//填充点
变量行=5;/#索引为0到4的数据行数
var COLS=5;/#索引为0到4的数据列的数目
风险值数据=[
[1,0,0,1,1],
[1,1,0,1,1],
[0,1,0,0,1],
[0,1,1,0,1],
[0,1,1,0,1]
]; // 与示例匹配的数据
//检查位置是否为空
函数isEmpty(网格、行索引、共索引){
//有效行和列?
如果(行索引<0 | |行索引>=行| |共索引<0 | |共索引>=列){
返回false;
}
//固体还是已填充?
如果(网格[rowIndex][colIndex]!==空){
返回false;
}
返回true;
}
//用填充位置替换空位置
函数填充(网格、startRow、startCol){
if(false==isEmpty(grid、startRow、startCol)){
返回;
}
var nextLocation=[[startRow,startCol]];//要处理的位置列表
而(nextLocation.length>0){//loop
var loc=nextLocation.pop();
var r=loc[0];//行索引
var c=loc[1];//列索引
//填充空位置
网格[r][c]=已填充;
//尝试从当前位置向左、向右和向下填充位置
if(isEmpty(grid,r,c-1)){//左
下一个位置推送([r,c-1]);
}
if(isEmpty(grid,r,c+1)){//right
下一个位置推送([r,c+1]);
}
如果(isEmpty(grid,r+1,c)){//down
下一个位置推送([r+1,c]);
}
}
}
//对第一行数据调用fillDown()(从顶部开始倒水)
对于(var c=0;c

额外注意,2d数组的行和列索引可以通过以下公式转换为1d数组索引:index=(r*COLS)+c,因此数据行中的最后一个位置=4,col=4将是4*5+4==24,这是1d数组中最后一个位置的索引。

右路径是什么意思,从起点到终点的路径?基本上,你不希望所有未阻塞的路径都被过滤,对吗?如果我们填充所有连接到最上面一行的开放站点,并且该过程填充了最下面一行的一些开放站点,那么系统会过滤。开放站点对应于水可能流过的空白空间,因此,一个渗滤的系统可以让水充满开放的场地,从上到下流动如果你只想突出顶部和底部行之间的最短路径,查找寻路算法,你只需要跟踪以前的状态(BFS就足够了)。如果要高亮显示连接顶行和底行的整个区域,请使用整体填充
root(index){
    while(this.array[index] !== index) index = this.array[index];
    return index;
}
var EMPTY = 0; // empty unfilled spot
var SOLID = 1; // solid spot
var FILLED = 2; // filled spot
var ROWS = 5; // # of rows of data indexed 0 to 4
var COLS = 5; // # of columns of data indexed 0 to 4
var data = [
    [1,0,0,1,1],
    [1,1,0,1,1],
    [0,1,0,0,1],
    [0,1,1,0,1],
    [0,1,1,0,1]
]; // data that matches the example

// check if a location is EMPTY
function isEmpty(grid, rowIndex, colIndex) {
    // valid row and col?
    if(rowIndex < 0 || rowIndex >= ROWS || colIndex < 0 || colIndex >= COLS) {
        return false;
    }
    // SOLID or FILLED already?
    if(grid[rowIndex][colIndex] !== EMPTY) {
        return false;
    }
    return true;
}

// replace EMPTY locations with FILLED locations
function fillDown(grid, startRow, startCol) {
    if(false === isEmpty(grid, startRow, startCol)) {
        return;
    }
    var nextLocation = [[startRow, startCol]]; // list of locations to process
    while(nextLocation.length > 0) { // loop
        var loc = nextLocation.pop();
        var r = loc[0]; // row index
        var c = loc[1]; // column index
        // fill the empty location
        grid[r][c] = FILLED;
        // try to fill the locations to the LEFT, RIGHT and DOWN from the current location
        if(isEmpty(grid, r, c - 1)) { // left
            nextLocation.push([r, c - 1]);
        }
        if(isEmpty(grid, r, c + 1)) { // right
            nextLocation.push([r, c + 1]);
        }
        if(isEmpty(grid, r + 1, c)) { // down
            nextLocation.push([r + 1, c]);
        }
    }
}

// call fillDown() for the first row of the data (water pouring accross the top)
for(var c = 0; c < COLS; c++) {
    fillDown(data, 0, c);
}

// show the result
alert(data.join('\n'));