2013-01-07 4 views
0

그래서이 문제를 해결하기 위해 노력한 것은 그리드에 짝수 번호가있는 숫자의 그리드가 주어지며 그리드에서 짝수 번호의 위치를 ​​갖게되고 그 한 위치에서 다른 모든 심지어 그것에 연결된 요소.배열의 연결 요소 식별

이 그림은 내가 설명하려고하는 것을 보여줍니다. 사진은 내가 6 enter image description here

여기

enter image description here

의 위치를 ​​가정 나는 그것이 내가 그냥 그것을 만들 수 있다는 어쨌든이 있는지 확인하려면 작동하는지 거의 확신 쓴 내 코드입니다 더 효율적인

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 
    } 

} 
+0

(나는 6이 0,1 대신 1,2 인 경우 더 직관적이라고 생각합니다.) – cwallenpoole

+0

@cwallenpoole, 컴퓨터 과학에서 거의 모든 것 *은 0 기반 색인을 사용합니다. 1 기반 인덱싱을 사용하면 직관적이지 않을 것입니다. – SimonC

+0

@SimonC 현재 그는 좌상단부터 0,0으로 시작하고 우하계는 2,2 (이것은 왜 그가 0,1을 6로 사용하고 있는지)입니다. 대신 오른쪽 하단은 0,0이어야합니다. 그러면 두 번째 x 인덱스 (1)에서 세 번째 y 인덱스 (2)가됩니다. 자, 여러분은 좌표계의 방향을 바꾸는 대신에 픽셀의 방향을 정할 때 방향을 잡아야한다고 주장 할 수는 있습니다.하지만 저는 그것을 확신하지 못했습니다. – cwallenpoole

답변

2

나는 테스트 할 필요가없는 노드를 테스트한다고 생각합니다. 나는 각 방향에 대한 네 가지 기능을 볼 수있을 :

// you'll need four methods just like this one. This one goes up, you'll need 
// one that goes down, another left and a forth right... 
static void iterUp(Server[][] grid, int k, int j) 
{ 
    // working with a local variable is easier to read and debug... 
    // you may wish to consider it. 
    Server cell = grid[k][j] 
    if(!cell.isEven() && !cell.isCracked()) 
    { 
     cell.setCracked(true) 
     if(k >= 1) 
     { 
      iterLeft(grid, k-1,j) 
     } 
     if(k < grid.length - 2) 
     { 
      iterRight(grid, k+1) 
     } 
     if(j < grid[k].length - 2) 
     { 
      iterUp(grid, k, j+1) 
     } 
     // no point in going down, because we know that down is checked already. 
    } 
} 

가 그럼 난 원래의 함수 정의 할 :

static void getLinksEven(Server[][] grid, int k, int j) 
{ 
    if(grid.length < k - 1 || grid[k].length < j - 1) 
    { 
     throw new ArrayIndexOutOfBoundsException("Not funny."); 
    } 
    // if this cell isn't even... who cares? 
    if(!grid[k][j].isEven()) return; 

    // Send the four on their merry way. 
    iterUp(grid,k,j); 
    iterDown(grid,k,j); 
    iterLeft(grid,k,j); 
    iterRight(grid,k,j); 
} 

이 당신에게 당신의 배열 조회 적어도 1/을 절약 할 수 및 가능하면 isEven()isCracked()에 대한 통화가 가능합니다.

+0

잘 모르겠습니다. 코드를 이해합니다. 당신은 iterDown() iterLeft()와 iterRight() 메소드를 가지고 있지만 그것들 중 어느 것도 정의되지 않았기 때문에. 또한 다운이 이미 확인 된 것을 어떻게 알 수 있습니까? –

+0

왼쪽과 오른쪽이 왼쪽과 오른쪽, 아래로 움직일 것입니다. – cwallenpoole

+0

up 메소드가 반복하기 때문에 (아래에 체크 된 노드에서 호출 된 * 것을 의미 함) 아래 노드가 이미 점검되었음을 알 수 있습니다. – cwallenpoole