Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def has_X(field, r, c, h, w):
- up = r-1
- right = c+1
- left = c-1
- down = r+1
- if r == 0:
- if right < w:
- if field[down][right]:
- return True
- if r == h:
- if right < w:
- if field[up][right]:
- return True
- if up > -1 and down < h:
- if right < w:
- if field[down][right]:
- return True
- if field[up][right]:
- return True
- return False
- def validate_battlefield(field):
- ideal = {'4': 1,
- '3': 2,
- '2': 3,
- '1': 4}
- result = {'4': 0,
- '3': 0,
- '2': 0,
- '1': 0}
- height = len(field) # 10
- width = len(field[0]) # 10
- for r in range(height):
- for c in range(width):
- if field[r][c]:
- if has_X(field, r, c, height, width): return False
- if (r == 0 or not field[r-1][c]) and (c == 0 or not field[r][c-1]):
- ship_w = 1
- ship_h = 1
- for i in range(1, 4):
- if c+i < width:
- if field[r][c+i]:
- ship_w += 1
- else:
- break
- for i in range(1, 4):
- if r+i < height:
- if field[r+i][c]:
- ship_h += 1
- else:
- break
- # print(max(ship_w, ship_h))
- if not any([ship_w, ship_h]) == 1:
- return False
- result[str(max(ship_w, ship_h))] += 1
- else:
- continue
- # print(result)
- return ideal == resul
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement