메신저가 C의 터보 C++로 된 간단한 그래픽 라이브러리에서 작동합니다. 메신저가 페인트 스타일 프로그램의 매우 원시적 인 버전을 개발했기 때문에 모든 것이 잘 작동하지만 홍수 채우기 알고리즘을 사용할 수 없기 때문입니다. 4 웨이 홍수 채우기 알고리즘을 사용하여, 나는 처음에는 재귀 버전을 시도했지만 작은 영역에서만 작동하므로 큰 영역을 채우면 충돌이 발생합니다. 읽기 나는 그것의 명시적인 스택 버전을 구현하는 발견 문제를 해결하지만 정말 모르겠다.홍수 채우기 알고리즘
나는이 같은 스택을 개발했다 :
struct node
{
int x, y;
struct node *next;
};
int push(struct node **top, int x, int y)
{
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL) //If there is no more memory
return 0;
newNode->x = x;
newNode->y = y;
newNode->next = *top;
*top = newNode;
return 1; //If we push the element correctly
}
int pop(struct node **top, int &x, int &y)
{
if(*top == NULL) //If the stack is empty
return 0;
struct node *temporal;
temporal = *top;
x = (*top)->x;
y = (*top)->y;
*top = (*top)->next;
free(temporal);
return 1; //If we pop an element
}
을 그리고 이것은 내가 홍수 채우기 기능하게 가지고있는 코드 :
void floodFill(int x, int y, int color_to_replace, int color_to_fill)
{
if(color_to_replace == color_to_fill)
return;
struct node *stack = NULL;
if(push(&stack, x, y) == 0) //If we can´t push the pixel
return;
while(pop(&stack, x, y) == 1) //While are pixels in the stack
{
pixel(x, y, color_to_fill);
if(x+1 < 640 && read_pixel(x+1, y) == color_to_replace)
if(push(&stack, x+1, y) == 0)
return;
if(x-1 >= 0 && read_pixel(x-1, y) == color_to_replace)
if(push(&stack, x-1, y) == 0)
return;
if(y+1 < 480 && read_pixel(x, y+1) == color_to_replace)
if(push(&stack, x, y+1) == 0)
return;
if(y-1 >= 0 && read_pixel(x, y-1) == color_to_replace)
if(push(&stack, x, y-1) == 0)
return;
}
}
하지만 여전히 때 메신저, 일을하지는 그냥 큰 영역을 채우기 위해 triying하고 내 프로그램에서 해상도 640 X 480로 작업하기 때문에 정말 문제가있다; 어떤 아이디어가 작동하지 않는 이유는 무엇입니까?
'pop()'에서 : 왜'temporal'을 할당하고'* top'을 사용합니까? –
올바른 최상위 노드를 해제하려면 시간적으로 필요합니다. – Toad
명시 적 스택은 16 비트 DOS와 같이 매우 제한된 스택이있는 플랫폼에서이를 해결합니다. 재귀 적 알고리즘은 간단하지가 않습니다. –