fork download
  1. def is_first(field):
  2. for y in range(len(field)):
  3. for x in range(len(field[y])):
  4. if field[y][x] != -1:
  5. return False
  6. return True
  7.  
  8. def str_field(field):
  9. result = ""
  10. for y in range(len(field)):
  11. result += str(field[y]) + "\n"
  12. return result
  13.  
  14. def find_mine(field, x, y):
  15. mine_num = field[y][x]
  16. mine_list = []
  17. safe_list = []
  18.  
  19. join_cells = [(0, -1), ( 1, -1), ( 1, 0), ( 1, 1),
  20. (0, 1), (-1, 1), (-1, 0), (-1, -1)]
  21.  
  22. unopened_cell_num = 0
  23. checked_cell_num = 0
  24. for cell_x, cell_y in join_cells:
  25. if (0 <= x + cell_x < len(field[y])) and (0 <= y + cell_y < len(field)):
  26. if field[y + cell_y][x + cell_x] == -1:
  27. unopened_cell_num += 1
  28. mine_list.append((x + cell_x, y + cell_y))
  29. elif field[y + cell_y][x + cell_x] == 9:
  30. checked_cell_num += 1
  31.  
  32. if mine_num == checked_cell_num:
  33. return ([], mine_list)
  34. elif mine_num == unopened_cell_num + checked_cell_num:
  35. return (mine_list, [])
  36. else:
  37. return ([] , [])
  38.  
  39. def checkio(field):
  40. if is_first(field):
  41. return [False, 0, 0]
  42.  
  43. for y in range(len(field)):
  44. for x in range(len(field[y])):
  45. sure_mine_list = safe_mine_list = []
  46. if 1 <= field[y][x] <= 8:
  47. (sure_mine_list, safe_mine_list) = find_mine(field, x, y)
  48. if sure_mine_list != []:
  49. # print ("next_hand: mark x:{0} y:{1}".format(sure_mine_list[0][0], sure_mine_list[0][1]))
  50. # print ("field:\n" + str_field(field))
  51. return [True, sure_mine_list[0][1], sure_mine_list[0][0]]
  52. elif safe_mine_list != []:
  53. # print ("next_hand: open x:{0} y:{1}".format(safe_mine_list[0][0], safe_mine_list[0][1]))
  54. # print ("field:\n" + str_field(field))
  55. return [False, safe_mine_list[0][1], safe_mine_list[0][0]]
  56.  
  57. print("Something wrong:")
  58. return [False, 0, 0]
  59.  
  60. #This part is using only for self-testing
  61. if __name__ == '__main__':
  62.  
  63. def check_is_win_referee(input_map):
  64. unopened = [1 for x in range(10) for y in range(10) if input_map[x][y] == -1]
  65. return not unopened
  66.  
  67. def build_map(input_map, mine_map, row, col):
  68. opened = [(row, col)]
  69. while opened:
  70. i, j = opened.pop(0)
  71. 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)]
  72. if 0 <= i + x < 10 and 0 <= j + y < 10]
  73. value = sum([mine_map[k][l] for k, l in neighs])
  74. input_map[i][j] = value
  75. if not value:
  76. for k, l in neighs:
  77. if input_map[k][l] == -1 and (k, l) not in opened:
  78. opened.append((k, l))
  79. return input_map
  80.  
  81. def check_solution(func, mine_map):
  82. input_map = [[-1] * 10 for _ in range(10)]
  83. while True:
  84.  
  85. is_mine, row, col = func([row[:] for row in input_map]) # using copy
  86. if input_map[row][col] != -1:
  87. print("You tried to uncover or mark already opened cell.")
  88. return False
  89. if is_mine and not mine_map[row][col]:
  90. print("You marked the wrong cell.")
  91. return False
  92. if not is_mine and mine_map[row][col]:
  93. print("You uncovered a mine. BANG!")
  94. return False
  95. if is_mine:
  96. input_map[row][col] = 9
  97. else:
  98. build_map(input_map, mine_map, row, col)
  99. if check_is_win_referee(input_map):
  100. return True
  101. return False
  102.  
  103. assert check_solution(checkio, [
  104. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  105. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  106. [0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
  107. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  108. [0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
  109. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  110. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  111. [0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
  112. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  113. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), "Simple"
  114.  
  115. assert check_solution(checkio, [
  116. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  117. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  118. [0, 1, 1, 0, 0, 0, 1, 1, 1, 0],
  119. [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
  120. [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
  121. [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
  122. [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
  123. [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
  124. [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
  125. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), "Gate"
  126.  
  127. assert check_solution(checkio, [
  128. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  129. [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  130. [0, 0, 1, 1, 0, 0, 1, 0, 0, 0],
  131. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  132. [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
  133. [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
  134. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  135. [0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
  136. [0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
  137. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]), "Various"
  138.  
Success #stdin #stdout 0.03s 9440KB
stdin
Standard input is empty
stdout
Standard output is empty