-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgreedy.py
47 lines (39 loc) · 1.22 KB
/
greedy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import itertools
from src.greedy.const import (
X, Y,
E, N, W, S,
LOOP, CONFIG,
)
from src.greedy.search import GomokuGreedySearch
def spiral_search(json):
for cfg in CONFIG:
# 初期設定
cycle = itertools.cycle((E, N, W, S))
x = LOOP
y = LOOP
step = 1 # 進んだ距離
corner = 1 # まがり角の位置
# 螺旋状に探索する
for i in range(X * Y):
# まがり角に到達したら方向転換
if step >= corner:
step = 1
direction = next(cycle)
dx, dy = direction
# X方向に進むとき、まがり角が遠くなる
if direction == E or direction == W:
corner += 1
# 全方向に探索
# その座標に置けないとき
gomoku_search = GomokuGreedySearch(x, y, json, cfg)
if json[y][x] is None:
xx, yy = gomoku_search.all_check()
if xx is not None:
return xx, yy
# 次の設定
step += 1
x += dx
y += dy
def run(json):
# 螺旋状に探索
spiral_search(json)