728x90
반응형
수도코드
더보기
Algorithm for countCells( x, y )
if the pixel( x, y ) is outside the grid
the result is 0;
else if pixel( x, y ) is ( not an image pixel ) or ( already counted )
the result is 0;
else
set the colour of the pixel( x, y ) to a red colour; // 카운트 되었음을 표시
the result is 1 plus the number of cells in each piece of the blob that includes a nearest neighbor;
private static int BACKGOUND_COLOR = 0;
private static int IMAGE_COLOR = 1;
private static int ALREADY_COUNTED = 2;
public int countCells(int x, int y){
int result;
if ( x<0 || x>=N || y<0 || y>=N ){
return 0;
} else if ( grid[x][y] != IMAGE_COLOR ){
return 0;
} else {
grid[x][y] = ALREADY_COUNTED;
return 1 + countCells(x-1, y+1) + countCells(x, y+1)
+ countCells(x+1, y+1) + countCells(x-1, y)
+ countCells(x+1, y) + countCells(x-1, y-1)
+ countCells(x, y-1) + countCells(x+1, y-1);
}
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
[프로그래머스/JAVA] 기능개발(스택/큐) (0) | 2021.12.27 |
---|---|
[재귀함수/backtracking] n queens problem (0) | 2021.12.02 |
[재귀함수] 미로찾기 (0) | 2021.11.29 |
[프로그래머스/JAVA] 프린터 (0) | 2021.11.16 |
[프로그래머스/C++] 베스트앨범 (0) | 2021.10.13 |