本帖最后由 阴阳神万物主 于 2020-1-2 23:43 编辑
忘记检查登记的地图了……
忘却了地址相通的特性。
- def solve(world):
- num = 0
- lx,ly = len(world),(len(world[0]) if world else 0)
- the_map = [[0]*ly for i in range(lx)]
- def explore(x,y):#探索岛屿并绘制地图,炸岛什么的不利于生存
- nonlocal the_map
- if world[x][y]:
- if the_map[x][y]:
- return
- else:
- #print('调试',x,y,lx,ly)
- the_map[x][y] = num
- if x:
- explore(x-1,y)
- if lx - x > 1:
- explore(x+1,y)
- if y:
- explore(x,y-1)
- if ly - y > 1:
- explore(x,y+1)
- for x in range(lx):
- for y in range(ly):
- if world[x][y] and (not the_map[x][y]):
- num += 1
- explore(x,y)
- '''
- for line in the_map:
- print(line)
- '''
- return num
- if __name__ == '__main__':
- '''
- print('示例1 输出:',solve([
- [1,1,0,0,0],
- [0,1,0,0,1],
- [0,0,0,1,1],
- [0,0,0,0,0],
- [0,0,0,0,1]
- ]))
- print('示例2 输出:',solve([
- [1,1]
- ]))
- '''
- print(solve([
- [0,1,0],
- [1,0,1],
- [0,1,0]
- ]))
复制代码 |