Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import copy
- dy = [1, -1, 0, 0 ]
- dx = [0, 0 , 1, -1]
- fill_and_judge_res = True
- ok_cnt = 0
- ng_cnt = 0
- matrix1_list = []
- def ng(y, x):
- return y < 0 or H <= y or x < 0 or W <= x
- def print_matrix(matrix2):
- for y in range(dim):
- for x in range(dim):
- print(matrix2[y][x], end=" ")
- print()
- print()
- def point(y, x):
- for dy in range(-1, 2):
- for dx in range(-1, 2):
- if (abs(dy) + abs(dx) > 1): continue
- ny = y + dy
- nx = x + dx
- if (ng(ny, nx)): continue
- matrix1[ny][nx] += 1
- matrix1[ny][nx] %= 2
- def ok(depth):
- global matrix1
- global matrix1_list
- tmp2 = True
- tmp = matrix1[0][0]
- for y in range(dim):
- for x in range(dim):
- if matrix1[y][x] != tmp:
- tmp2 = False
- if (depth == 2 and tmp2 == False): return False
- if (tmp2 == True):
- #print("ok")
- #print_matrix()
- print("-------------------------------------")
- for i in range(len(matrix1_list)):
- print_matrix(matrix1_list[i])
- print("-------------------------------------")
- return True
- res = False
- for y in range(dim):
- for x in range(dim):
- point(y, x)
- matrix1_list.append(copy.deepcopy(matrix1))
- if ok(depth + 1): res = True
- matrix1_list.pop()
- point(y, x)
- return res
- def fill_and_judge(cnt):
- global fill_and_judge_res
- global ok_cnt
- global ng_cnt
- if (cnt == dim * dim):
- #print_matrix()
- matrix1_list.append(copy.deepcopy(matrix1))
- res = ok(0)
- matrix1_list.pop()
- if (res == True): ok_cnt += 1
- if (res == False):
- fill_and_judge_res = False
- #print_matrix()
- ng_cnt += 1
- return
- y = cnt // dim
- x = cnt % dim
- if (ng(y, x)):
- print("y =", y, "x =", x)
- matrix1[cnt // dim][cnt % dim] = 0
- res = fill_and_judge(cnt + 1)
- if (res == False):
- fill_and_judge_res = False
- matrix1[cnt // dim][cnt % dim] = 1
- res = fill_and_judge(cnt + 1)
- if (res == False):
- fill_and_judge_res = False
- matrix1[cnt // dim][cnt % dim] = 0
- dim = 3
- H = dim
- W = dim
- matrix1 = [[0 for _ in range(dim)] for _ in range(dim)]
- # point(1, 1)
- # print_matrix(matrix1)
- res = fill_and_judge(0)
- print("res =", res)
- print("fill_and_judge_res =", fill_and_judge_res)
- print("ok_cnt", ok_cnt)
- print("ng_cnt", ng_cnt)
Add Comment
Please, Sign In to add comment