那么我想这个问题做的是给定数字的网格我想有有一个即使在电网号的位置,并通过具有一个位置时,它会发现所有其他甚至连接到它的元素。识别连接数组中的元素
此图为我试图来形容。图片假设我有
这里的位置是我的代码我写了我几乎可以肯定,它的作品我只是想看看是否有无论如何,我可以把它更高效的
getLinksEven(grid,0,1);
static void getLinksEven(Server[][] grid, int k, int j)
{
try{
if(grid[k-1][j].isEven()&& !grid[k-1][j].isCracked()){
grid[k-1][j].setCracked(true);
getLinksEven(grid,k-1,j);
}
}
catch(ArrayIndexOutOfBoundsException a)
{
//do nothing
}
try{
if(grid[k][j-1].isEven()&& !grid[k][j-1].isCracked()){
grid[k][j-1].setCracked(true);
getLinksEven(grid,k,j-1);
}
}
catch(ArrayIndexOutOfBoundsException a)
{
//do nothing
}
try{
if(grid[k+1][j].isEven()&& !grid[k+1][j].isCracked()){
grid[k+1][j].setCracked(true);
getLinksEven(grid,k+1,j);
}
}
catch(ArrayIndexOutOfBoundsException a)
{
//do nothing
}
try{
if(grid[k][j+1].isEven()&& !grid[k][j+1].isCracked()){
grid[k][j+1].setCracked(true);
getLinksEven(grid,k,j+1);
}
}
catch(ArrayIndexOutOfBoundsException a)
{
//do nothing
}
}
(我认为这将是更直观,如果6个是在1,2,而不是0,1) – cwallenpoole
@cwallenpoole,几乎所有* *在计算机科学中使用基于0的索引。使用基于1的索引不会很直观。 – SimonC
@SimonC目前,他正在从左至右为0,0的顶部开始和右下方是2,2(这就是为什么他是用0,1 6)。相反,右下角应该是0,0。这将使第二个x指数(1)和第三个y指数(2)成为6。现在,您可以尝试去争论,它应该以我们定位像素为导向,而不是像我们定位坐标系一样,但我不相信这一点。 – cwallenpoole