This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
battleship1p.py
53 lines (50 loc) · 1.54 KB
/
battleship1p.py
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
51
52
53
from collections import Counter
from heapq import *
from random import randint
def shoot(grid, h, dash):
if not h:
return heappop(dash)[1]
lin, col = h[0], h[1]
num_lins, num_cols = len(grid), len(grid[0])
directions = Counter({ 'left': 0, 'right': 0, 'down': 0, 'up': 0 })
p = []
if col > 0 and grid[lin][col-1] == '-':
directions['left'] += 1
if col+1 < num_cols and grid[lin][col+1] == 'h':
directions['left'] += 1
heappush(p, (directions['left'], [lin,col-1]))
if col < num_cols-1 and grid[lin][col+1] == '-':
directions['right'] += 1
if col-1 >= 0 and grid[lin][col-1] == 'h':
directions['right'] += 1
heappush(p, (directions['right'], [lin,col+1]))
if lin < num_lins-1 and grid[lin+1][col] == '-':
directions['down'] += 1
if lin-1 >= 0 and grid[lin-1][col] == 'h':
directions['down'] += 1
heappush(p, (directions['down'], [lin+1,col]))
if lin > 0 and grid[lin-1][col] == '-':
directions['up'] += 1
if lin+1 >= 0 and grid[lin+1][col] == 'h':
directions['up'] += 1
heappush(p, (directions['up'], [lin-1,col]))
# Return best movement.
if len(p) >= 1:
return heappop(p)[1]
else:
return heappop(dash)[1]
n = int(raw_input())
grid = []
h = False
dash = []
for i in range(n):
line = list(raw_input().strip())
grid.append(line)
pos_h = filter(lambda j: line[j] == 'h', range(len(line)))
if len(pos_h) >= 1:
h = [i, pos_h[0]]
pos_dash = filter(lambda j: line[j] == '-', range(len(line)))
if len(pos_dash) >= 1:
heappush(dash, (randint(0, 20), [i, pos_dash[0]]))
res = shoot(grid, h, dash)
print res[0], res[1]