我只是一个编程新手。 uf是一个并查集类,它具有连接两个节点根的union方法。 这段代码负责打开网格的一个站点,并将该站点与其邻居(如果有)联合。如果其中一个邻居已经满了,则填充与该站点相连的所有节点。 以下是实际代码:
if(i == 1){
uf.union(len*len, xyTo1D(i,j));
if(existAndOpen(i+1,j)){
uf2.union(xyTo1D(i+1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndOpen(i-1,j)){
uf2.union(xyTo1D(i-1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndOpen(i,j-1)){
uf2.union(xyTo1D(i,j-1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j == len && i == len)){
if(existAndOpen(i,j+1)){
uf2.union(xyTo1D(i,j+1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
}
else{
if(existAndFull(i+1,j)){
uf2.union(xyTo1D(i+1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndFull(i-1,j)){
uf2.union(xyTo1D(i-1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndFull(i,j-1)){
uf2.union(xyTo1D(i,j-1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j== len && i == len)){
if(existAndFull(i,j+1)){
uf2.union(xyTo1D(i,j+1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
if(existAndOpen(i+1,j)){
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndOpen(i-1,j)){
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndOpen(i,j-1)){
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j== len && i == len)){
if(existAndOpen(i,j+1)){
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
}
}
我该如何简化代码?
if
语句指定了是否在测试中使用f1
还是f2
。 - Amadan