デッドコードも入ってるけどまあいいか。
map = [] ata_map = [] for i in range(8): mapx = [] mapk = [] for j in range(8): mapx.append(0) mapk.append(0) map.append(mapx) ata_map.append(mapk) def clear_map(): for i in range(8): for j in range(8): map[i][j] = 0 ata_map[i][j] = 0 yyy = [-1,-1,-1, 0, 0, 1, 1, 1] xxx = [-1, 0, 1,-1, 1,-1, 0, 1] def set_atamap_yx(y,x): ata_map[y][x] = 1 for i in range (8): for j in range(1, 8): ty = y + j * yyy[i] tx = x + j * xxx[i] if ty < 0 or ty >= 8 or tx < 0 or tx >= 8: break ata_map[ty][tx] = 1 def locate_yx(y,x): map[y][x] = 1 set_atamap_yx(y,x) def is_hit_yx(ypos,xpos): if ata_map[ypos][xpos]: return True for i in range (8): for j in range(1, 8): y = ypos + j * yyy[i] x = xpos + j * xxx[i] if y < 0 or y >= 8 or x < 0 or x >= 8: break if map[y][x] == 1: return True return False def print_map(): for i in range(8): aline = [] a = [] for j in range(8): if map[i][j] == 1: a.append('Q') elif ata_map[i][j]: a.append('*') else: a.append('.') aline = "".join(a) print(aline) print('') N_ANS = 5 def calc_queen(): clear_map() dbgcnt = 0 anscnt = 0 for a in range(8): for b in range(8): for c in range(8): for d in range(8): for e in range(8): for f in range(8): for g in range(8): for h in range(8): clear_map() dbgcnt += 1 if dbgcnt % 1000000 == 0: print(dbgcnt) i = 0 locate_yx(i,a) i += 1 if is_hit_yx(i,b): continue locate_yx(i,b) i += 1 if is_hit_yx(i,c): continue locate_yx(i,c) i += 1 if is_hit_yx(i,d): continue locate_yx(i,d) i += 1 if is_hit_yx(i,e): continue locate_yx(i,e) i += 1 if is_hit_yx(i,f): continue locate_yx(i,f) i += 1 if is_hit_yx(i,g): continue locate_yx(i,g) i += 1 if is_hit_yx(i,h): continue locate_yx(i,h) i += 1 print_map() clear_map() # # N_ANS co dake # anscnt += 1 if anscnt >= N_ANS: return calc_queen()