2017-12-02 6 views
0
def floodfill(col,rowb,neigh,bombpos): 
#neigh is number of bombs (neighbors) around the position (row,col) 
try: 
    neigh[rowb][col] 
    bombpos[rowb][col] 
    visit[rowb][col] 
except IndexError: 
    return 
#if the position is an isolated position, i.e. there are no bombs adjacent to it; 
if neigh[rowb][col] == 0: 
    pygame.draw.rect(screen, (55, 55, 55), (1 + col * 30, 1 + rowb * 30, 28, 28)) 
    floodfill(col - 1, rowb, neigh, bombpos) 
    floodfill(col, rowb - 1, neigh, bombpos) 
    floodfill(col + 1, rowb, neigh, bombpos) 
    floodfill(col, rowb + 1, neigh, bombpos) 
    floodfill(col - 1, rowb-1, neigh, bombpos) 
    floodfill(col+1, rowb + 1, neigh, bombpos) 
    floodfill(col + 1, rowb-1, neigh, bombpos) 
    floodfill(col-1, rowb + 1, neigh, bombpos) 

나는 지뢰 찾기 홍수 채우기 알고리즘을 다시 시도하고를 플러드 필, 그래서 나는 그들이 의미, 내가 재귀 적으로 0의 이웃 값을 가지고있는 사람의 주위에 모든 상자를 선택하는 홍수 채우기 기능이 폭탄을 만지지 않고있어. 나는 이것이 왜 무한 재귀인지 이해하고 어떤 점이 테스트되었는지를 검출하는 방문 행렬을 사용해야 만 결국 재귀를 끝낼 수있다. 재귀에서 위치가 사용되는 경우 위치 (행, 열)에 대해 true로 전환 될 "방문"행렬을 구현하려했지만 전혀 작동하지 못했습니다. 지뢰 찾기에서 적절한 홍수 채우기 알고리즘을 얻으려면이 코드에서 방문 행렬을 어떻게 구현할 수 있습니까?지뢰 찾기 알고리즘 파이썬에게

+0

안녕하세요. 불행히도 이것은 토론 포럼이나 튜토리얼 서비스가 아닙니다. 시간을내어 [ask]와 그 페이지의 다른 링크를 읽으십시오. 예제를 연습하면서 [Tutorial] (https://docs.python.org/3/tutorial/index.html)을 통해 약간의 시간을 투자해야합니다. 파이썬에서 제공하는 도구에 대해 소개하고 문제 해결을위한 아이디어를 얻을 수도 있습니다. – wwii

답변

1

이것은 나와 같은 문제가있는 경우에 작동하는 것으로 확인 된 코드입니다. 이유는 모르겠지만 "try :"를 사용하면 특정 경우에 대한 재귀를 중지하므로 좋은 방법으로 전환 할 수 있습니다 if 문을 사용하면 경계를 설정하는 것이 더 좋습니다. 그런 다음 셀을 "열기"만하면 방문 행렬을 false로 설정하는 것입니다.

if not (-1<rowb<size and -1<col<size): 
    return 
if neigh[rowb][col] == 0 and not visit[rowb][col]: 
    pygame.draw.rect(screen, (55, 55, 55), (1 + col * 30, 1 + rowb * 30, 28, 28)) 
    visit[rowb][col] = True 
    floodfill(col - 1, rowb, neigh, bombpos) 
    floodfill(col, rowb - 1, neigh, bombpos) 
    floodfill(col + 1, rowb, neigh, bombpos) 
    floodfill(col, rowb + 1, neigh, bombpos) 
    floodfill(col - 1, rowb - 1, neigh, bombpos) 
    floodfill(col - 1, rowb + 1, neigh, bombpos) 
    floodfill(col + 1, rowb - 1, neigh, bombpos) 
    floodfill(col + 1, rowb + 1, neigh, bombpos)