This repository has been archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raidone.py
executable file
·82 lines (71 loc) · 2.13 KB
/
raidone.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import sys
import subprocess
import cv2
import numpy as np
def main():
img = cv2.imread('raid1_test.jpg', cv2.IMREAD_GRAYSCALE)
counter_img = img.copy()
LANG = sys.stdin.readline().rstrip()
FILE = sys.stdin.readline().rstrip()
query = 0
correct_query = 0
bunker = 0
for i in range(img.shape[0]):
for j in range(img.shape[0]):
if img[i, j] == 255:
bunker += 1
RUN_CMD = {
'c': './',
'cpp': './',
# 'java': 'javac ./Main.java',
'python2': 'python2 ./',
'python3': 'python3 ./',
}
p = subprocess.Popen(
RUN_CMD[LANG]+FILE,
shell=False,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE
)
p.stdin.write("%d\n" % (img.shape[0]))
p.stdin.write("%d\n" % (bunker))
p.stdin.flush()
while True:
if p.poll() != None:
# Participant killed the program
break
try:
x, y = p.stdout.readline().split()
x = int(x)
y = int(y)
except ValueError:
if p.poll() != None:
# Participant killed the program
break
else:
# Deadlock!
print 'We faced a deadlock while evaluating your queries'
sys.exit()
if x >= img.shape[0] or y >= img.shape[0]:
print 'Incorrect query! ',
print 'You queried for %d %d in a %d sized grid'\
% (x, y, img.shape[0])
sys.exit()
if img[x, y] == 255:
if counter_img[x, y] == 255:
counter_img[x, y] = 0
correct_query += 1
p.stdin.write("YES\n")
else:
p.stdin.write("NO\n")
query += 1
if correct_query >= bunker:
print 'Yay! You successfully bombed all the bunkers. ',
print 'You bombed %d bunker cells with %d queries' % (bunker, query)
print int(query)
else:
print 'You bombed %d bunker cells out of %d. You sent %d queries' \
% (correct_query, bunker, query)
if __name__ == '__main__':
main()