알고리즘

[재귀함수] Counting Cells in a Blob

데메즈 2021. 11. 30. 22:35
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
반응형