Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. def has_X(field, r, c, h, w):
  2.     up = r-1
  3.     right = c+1
  4.     left = c-1
  5.     down = r+1
  6.     if r == 0:
  7.         if right < w:
  8.             if field[down][right]:
  9.                 return True
  10.     if r == h:
  11.         if right < w:
  12.             if field[up][right]:
  13.                 return True
  14.     if up > -1 and down < h:
  15.         if right < w:
  16.             if field[down][right]:
  17.                 return True
  18.             if field[up][right]:
  19.                 return True
  20.     return False
  21.  
  22. def validate_battlefield(field):
  23.     ideal = {'4': 1,
  24.              '3': 2,
  25.              '2': 3,
  26.              '1': 4}
  27.     result = {'4': 0,
  28.               '3': 0,
  29.               '2': 0,
  30.               '1': 0}
  31.     height = len(field) # 10
  32.     width = len(field[0]) # 10
  33.     for r in range(height):
  34.         for c in range(width):
  35.             if field[r][c]:
  36.                 if has_X(field, r, c, height, width): return False
  37.                 if (r == 0 or not field[r-1][c]) and (c == 0 or not field[r][c-1]):
  38.                     ship_w = 1
  39.                     ship_h = 1
  40.                     for i in range(1, 4):
  41.                         if c+i < width:
  42.                             if field[r][c+i]:
  43.                                 ship_w += 1
  44.                             else:
  45.                                 break
  46.                     for i in range(1, 4):
  47.                         if r+i < height:
  48.                             if field[r+i][c]:
  49.                                 ship_h += 1
  50.                             else:
  51.                                 break
  52.                     # print(max(ship_w, ship_h))
  53.                     if not any([ship_w, ship_h]) == 1:
  54.                         return False
  55.                     result[str(max(ship_w, ship_h))] += 1
  56.                 else:
  57.                     continue
  58.     # print(result)
  59.     return ideal == resul
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement