강의를 듣고 간단히 만들어봤습니다. 미로찾기를 구현할 수 있다면 countCellsblob은 자연스레 구현할 수 있습니다. 제 생각대로 만들어서 강의의 코드와 다를 수 있습니다.(강의에선 java를 사용합니다.) MIT OpenCourseWare에서 강의들을 때 미로찾기 나왔었는데 반갑네요 ^^ 쉽고 재밌게 코딩할 수 있는듯 ~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | # PATHWAY_COLOUR = 0 # WALL_COLOUR = 1 # BLOCKED_COLOUR = 2 # PATH_COLOUR = 3 def findPath(x, y): if x is 8 and y is 9: # out return True else: cell = [[x+1, y], [x, y+1], [x-1, y], [x, y-1]] list[x][y] = 3 for el in cell: if el[0] < 0 or el[1] < 0 or el[0] > len(list)-1 or el[1] > len(list) -1: continue if list[el[0]][el[1]] != 3 and list[el[0]][el[1]] == 1: print el if findPath(el[0], el[1]): return True list[el[0]][el[1]] = 2 return False def countCells(x, y): if x < 0 or x >= len(list) or y < 0 or y >= len(list): return 0 elif list[x][y] != 1 : return 0 else: list[x][y] = 3 print x,y return 1 + countCells(x-1, y+1) + countCells(x-1, y)\ + countCells(x-1, y-1) + countCells(x, y + 1)\ + countCells(x, y-1) + countCells(x+1, y+1)\ + countCells(x+1, y) + countCells(x+1, y-1) list = [ [1, 1, 0, 0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0, 1, 1, 1, 0], [0, 1, 1, 1, 1, 0, 1, 0, 1, 0], [1, 0, 1, 0, 0, 1, 0, 1, 1, 1], [0, 1, 1, 1, 1, 0, 0, 1, 0, 0], [1, 0, 0, 0, 1, 1, 1, 0, 0, 1], [0, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 1, 1, 1, 1, 0, 1, 1], [1, 0, 0, 0, 1, 1, 1, 1, 1, 0] ] #print findPath(0, 0) print countCells(1, 8) for el in list: print el | cs |
ref : algorithm
'* Computer Science > Algorithm' 카테고리의 다른 글
Unit 3. heap sort (0) | 2017.02.09 |
---|---|
Unit 2. powerset (0) | 2017.02.07 |
Unit 3. 들어가기 앞서 (0) | 2017.01.29 |
Unit 2. 상태 공간 트리 (0) | 2017.01.29 |
Unit 2. n-queens (0) | 2017.01.29 |