def is_first(field):
for y in range(len(field)):
for x in range(len(field[y])):
if field[y][x] != -1:
return False
return True
def str_field(field):
result = ""
for y in range(len(field)):
result += str(field[y]) + "\n"
return result
def find_mine(field, x, y):
mine_num = field[y][x]
mine_list = []
safe_list = []
join_cells = [(0, -1), ( 1, -1), ( 1, 0), ( 1, 1),
(0, 1), (-1, 1), (-1, 0), (-1, -1)]
unopened_cell_num = 0
checked_cell_num = 0
for cell_x, cell_y in join_cells:
if (0 <= x + cell_x < len(field[y])) and (0 <= y + cell_y < len(field)):
if field[y + cell_y][x + cell_x] == -1:
unopened_cell_num += 1
mine_list.append((x + cell_x, y + cell_y))
elif field[y + cell_y][x + cell_x] == 9:
checked_cell_num += 1
if mine_num == checked_cell_num:
return ([], mine_list)
elif mine_num == unopened_cell_num + checked_cell_num:
return (mine_list, [])
else:
return ([] , [])
def checkio(field):
if is_first(field):
return [False, 0, 0]
for y in range(len(field)):
for x in range(len(field[y])):
sure_mine_list = safe_mine_list = []
if 1 <= field[y][x] <= 8:
(sure_mine_list, safe_mine_list) = find_mine(field, x, y)
if sure_mine_list != []:
# print ("next_hand: mark x:{0} y:{1}".format(sure_mine_list[0][0], sure_mine_list[0][1]))
# print ("field:\n" + str_field(field))
return [True, sure_mine_list[0][1], sure_mine_list[0][0]]
elif safe_mine_list != []:
# print ("next_hand: open x:{0} y:{1}".format(safe_mine_list[0][0], safe_mine_list[0][1]))
# print ("field:\n" + str_field(field))
return [False, safe_mine_list[0][1], safe_mine_list[0][0]]
print("Something wrong:")
return [False, 0, 0]
#This part is using only for self-testing
if __name__ == '__main__':
def check_is_win_referee(input_map):
unopened = [1 for x in range(10) for y in range(10) if input_map[x][y] == -1]
return not unopened
def build_map(input_map, mine_map, row, col):
opened = [(row, col)]
while opened:
i, j = opened.pop(0)
neighs = [(i + x, j + y) for x, y in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
if 0 <= i + x < 10 and 0 <= j + y < 10]
value = sum([mine_map[k][l] for k, l in neighs])
input_map[i][j] = value
if not value:
for k, l in neighs:
if input_map[k][l] == -1 and (k, l) not in opened:
opened.append((k, l))
return input_map
def check_solution(func, mine_map):
input_map = [[-1] * 10 for _ in range(10)]
while True:
is_mine, row, col = func([row[:] for row in input_map]) # using copy
if input_map[row][col] != -1:
print("You tried to uncover or mark already opened cell.")
return False
if is_mine and not mine_map[row][col]:
print("You marked the wrong cell.")
return False
if not is_mine and mine_map[row][col]:
print("You uncovered a mine. BANG!")
return False
if is_mine:
input_map[row][col] = 9
else:
build_map(input_map, mine_map, row, col)
if check_is_win_referee(input_map):
return True
return False
assert check_solution(checkio, [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), "Simple"
assert check_solution(checkio, [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), "Gate"
assert check_solution(checkio, [
[0, 0, 0, 0, 0, 0, 0, 0,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HggKyBjZWxsX3ggPCBsZW4oZmllbGRbeV0pKSBhbmQgKDAgPD0geSArIGNlbGxfeSA8IGxlbihmaWVsZCkpOgogICAgICAgICAgICBpZiBmaWVsZFt5ICsgY2VsbF95XVt4ICsgY2VsbF94XSA9PSAtMToKICAgICAgICAgICAgICAgIHVub3BlbmVkX2NlbGxfbnVtICs9IDEKICAgICAgICAgICAgICAgIG1pbmVfbGlzdC5hcHBlbmQoKHggKyBjZWxsX3gsIHkgKyBjZWxsX3kpKQogICAgICAgICAgICBlbGlmIGZpZWxkW3kgKyBjZWxsX3ldW3ggKyBjZWxsX3hdID09IDk6CiAgICAgICAgICAgICAgICBjaGVja2VkX2NlbGxfbnVtICs9IDEKCiAgICBpZiBtaW5lX251bSA9PSBjaGVja2VkX2NlbGxfbnVtOgogICAgICAgIHJldHVybiAoW10sIG1pbmVfbGlzdCkKICAgIGVsaWYgbWluZV9udW0gPT0gdW5vcGVuZWRfY2VsbF9udW0gKyBjaGVja2VkX2NlbGxfbnVtOgogICAgICAgIHJldHVybiAobWluZV9saXN0LCBbXSkKICAgIGVsc2U6CiAgICAgICAgcmV0dXJuIChbXSAsIFtdKQoKZGVmIGNoZWNraW8oZmllbGQpOgogICAgaWYgaXNfZmlyc3QoZmllbGQpOgogICAgICAgIHJldHVybiBbRmFsc2UsIDAsIDBdCgogICAgZm9yIHkgaW4gcmFuZ2UobGVuKGZpZWxkKSk6CiAgICAgICAgZm9yIHggaW4gcmFuZ2UobGVuKGZpZWxkW3ldKSk6CiAgICAgICAgICAgIHN1cmVfbWluZV9saXN0ID0gc2FmZV9taW5lX2xpc3QgPSBbXQogICAgICAgICAgICBpZiAxIDw9IGZpZWxkW3ldW3hdIDw9IDg6CiAgICAgICAgICAgICAgICAoc3VyZV9taW5lX2xpc3QsIHNhZmVfbWluZV9saXN0KSA9IGZpbmRfbWluZShmaWVsZCwgeCwgeSkKICAgICAgICAgICAgaWYgc3VyZV9taW5lX2xpc3QgIT0gW106CiMgICAgICAgIC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AgICAgb3BlbmVkID0gWyhyb3csIGNvbCldCiAgICAgICAgd2hpbGUgb3BlbmVkOgogICAgICAgICAgICBpLCBqID0gb3BlbmVkLnBvcCgwKQogICAgICAgICAgICBuZWlnaHMgPSBbKGkgKyB4LCBqICsgeSkgZm9yIHgsIHkgaW4gWygtMSwgLTEpLCAoLTEsIDApLCAoLTEsIDEpLCAoMCwgLTEpLCAoMCwgMSksICgxLCAtMSksICgxLCAwKSwgKDEsIDEpXQogICAgICAgICAgICAgICAgICAgICAgaWYgMCA8PSBpICsgeCA8IDEwIGFuZCAwIDw9IGogKyB5IDwgMTBdCiAgICAgICAgICAgIHZhbHVlID0gc3VtKFttaW5lX21hcFtrXVtsXSBmb3IgaywgbCBpbiBuZWlnaHNdKQogICAgICAgICAgICBpbnB1dF9tYXBbaV1bal0gPSB2YWx1ZQogICAgICAgICAgICBpZiBub3QgdmFsdWU6CiAgICAgICAgICAgICAgICBmb3IgaywgbCBpbiBuZWlnaHM6CiAgICAgICAgICAgICAgICAgICAgaWYgaW5wdXRfbWFwW2tdW2xdID09IC0xIGFuZCAoaywgbCkgbm90IGluIG9wZW5lZDoKICAgICAgICAgICAgICAgICAgICAgICAgb3BlbmVkLmFwcGVuZCgoaywgbCkpCiAgICAgICAgcmV0dXJuIGlucHV0X21hcAoKICAgIGRlZiBjaGVja19zb2x1dGlvbihmdW5jLCBtaW5lX21hcCk6CiAgICAgICAgaW5wdXRfbWFwID0gW1stMV0gKiAxMCBmb3IgXyBpbiByYW5nZSgxMCldCiAgICAgICAgd2hpbGUgVHJ1ZToKCiAgICAgICAgICAgIGlzX21pbmUsIHJvdywgY29sID0gZnVuYyhbcm93WzpdIGZvciByb3cgaW4gaW5wdXRfbWFwXSkgICMgdXNpbmcgY29weQogICAgICAgICAgICBpZiBpbnB1dF9tYXBbcm93XVtjb2xdICE9IC0xOgogICAgICAgICAgICAgICAgcHJpbnQoIllvdSB0cmllZCB0byB1bmNvdmVyIG9yIG1hcmsgYWxyZWFkeSBvcGVuZWQgY2VsbC4iKQogICAgICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgICAgIGlmIGlzX21pbmUgYW5kIG5vdCBtaW5lX21hcFtyb3ddW2NvbF06CiAgICAgICAgICAgICAgICBwcmludCgiWW91IG1hcmtlZCB0aGUgd3JvbmcgY2VsbC4iKQogICAgICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgICAgIGlmIG5vdCBpc19taW5lIGFuZCBtaW5lX21hcFtyb3ddW2NvbF06CiAgICAgICAgICAgICAgICBwcmludCgiWW91IHVuY292ZXJlZCBhIG1pbmUuIEJBTkchIikKICAgICAgICAgICAgICAgIHJldHVybiBGYWxzZQogICAgICAgICAgICBpZiBpc19taW5lOgogICAgICAgICAgICAgICAgaW5wdXRfbWFwW3Jvd11bY29sXSA9IDkKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIGJ1aWxkX21hcChpbnB1dF9tYXAsIG1pbmVfbWFwLCByb3csIGNvbCkKICAgICAgICAgICAgaWYgY2hlY2tfaXNfd2luX3JlZmVyZWUoaW5wdXRfbWFwKToKICAgICAgICAgICAgICAgIHJldHVybiBUcnVlCiAgICAgICAgcmV0dXJuIEZhbHNlCgogICAgYXNzZXJ0IGNoZWNrX3NvbHV0aW9uKGNoZWNraW8sIFsKICAgICAgICBbMCwgMCwgMCwgMCwgMCwgMCwgMCwgMCwgMCwgMF0sCiAgICAgICAgWzAsIDAsIDAsIDAsIDAsIDAsIDAsIDAsIDAsIDBdLAogICAgICAgIFswLCAwLCAxLCAwLCAxLCAwLCAwLCAxLCAwLCAwXSwKICAgICAgICBbMCwgMCwgMCwgMCwgMCwgMCwgMCwgMCwgMCwgMF0sCiAgICAgICAgWzAsIDAsIDEsIDAsIDEsIDAsIDAsIDAsIDAsIDBdLAogICAgICAgIFswLCAwLCAwLCAwLCAwLCAwLCAwLCAwLCAwLCAwXSwKICAgICAgICBbMCwgMCwgMCwgMCwgMCwgMCwgMCwgM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