Guest User

Untitled

a guest
Feb 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import random
  2. import copy
  3.  
  4. dy = [1, -1, 0, 0 ]
  5. dx = [0, 0 , 1, -1]
  6. fill_and_judge_res = True
  7. ok_cnt = 0
  8. ng_cnt = 0
  9. matrix1_list = []
  10.  
  11. def ng(y, x):
  12. return y < 0 or H <= y or x < 0 or W <= x
  13.  
  14. def print_matrix(matrix2):
  15. for y in range(dim):
  16. for x in range(dim):
  17. print(matrix2[y][x], end=" ")
  18. print()
  19. print()
  20.  
  21.  
  22. def point(y, x):
  23. for dy in range(-1, 2):
  24. for dx in range(-1, 2):
  25. if (abs(dy) + abs(dx) > 1): continue
  26. ny = y + dy
  27. nx = x + dx
  28. if (ng(ny, nx)): continue
  29. matrix1[ny][nx] += 1
  30. matrix1[ny][nx] %= 2
  31.  
  32. def ok(depth):
  33. global matrix1
  34. global matrix1_list
  35.  
  36. tmp2 = True
  37.  
  38. tmp = matrix1[0][0]
  39. for y in range(dim):
  40. for x in range(dim):
  41. if matrix1[y][x] != tmp:
  42. tmp2 = False
  43. if (depth == 2 and tmp2 == False): return False
  44.  
  45. if (tmp2 == True):
  46. #print("ok")
  47. #print_matrix()
  48. print("-------------------------------------")
  49. for i in range(len(matrix1_list)):
  50. print_matrix(matrix1_list[i])
  51. print("-------------------------------------")
  52. return True
  53.  
  54. res = False
  55. for y in range(dim):
  56. for x in range(dim):
  57. point(y, x)
  58. matrix1_list.append(copy.deepcopy(matrix1))
  59. if ok(depth + 1): res = True
  60. matrix1_list.pop()
  61. point(y, x)
  62. return res
  63.  
  64. def fill_and_judge(cnt):
  65. global fill_and_judge_res
  66. global ok_cnt
  67. global ng_cnt
  68.  
  69. if (cnt == dim * dim):
  70. #print_matrix()
  71. matrix1_list.append(copy.deepcopy(matrix1))
  72. res = ok(0)
  73. matrix1_list.pop()
  74. if (res == True): ok_cnt += 1
  75. if (res == False):
  76. fill_and_judge_res = False
  77. #print_matrix()
  78. ng_cnt += 1
  79. return
  80.  
  81. y = cnt // dim
  82. x = cnt % dim
  83. if (ng(y, x)):
  84. print("y =", y, "x =", x)
  85.  
  86. matrix1[cnt // dim][cnt % dim] = 0
  87. res = fill_and_judge(cnt + 1)
  88. if (res == False):
  89. fill_and_judge_res = False
  90.  
  91. matrix1[cnt // dim][cnt % dim] = 1
  92. res = fill_and_judge(cnt + 1)
  93. if (res == False):
  94. fill_and_judge_res = False
  95.  
  96. matrix1[cnt // dim][cnt % dim] = 0
  97.  
  98. dim = 3
  99. H = dim
  100. W = dim
  101. matrix1 = [[0 for _ in range(dim)] for _ in range(dim)]
  102. # point(1, 1)
  103. # print_matrix(matrix1)
  104.  
  105. res = fill_and_judge(0)
  106. print("res =", res)
  107. print("fill_and_judge_res =", fill_and_judge_res)
  108. print("ok_cnt", ok_cnt)
  109. print("ng_cnt", ng_cnt)
Add Comment
Please, Sign In to add comment