from itertools import combinations
wh = input()
w, h = list(map(int, wh.split(" ")))
m = []

for i in range(h):
    line = input()
    items = list(map(int, line.split(" ")))
    m.append(items)

def cost(path):
    y, x = 0, 0
    c = m[y][x]
    for p in path:
        if p == 'E': x += 1
        elif p == 'S': y += 1
        elif p == 'W': x -= 1
        elif p == 'N': y -= 1
        else: raise
        c += m[y][x]
    return c

minc = -1
minp = ""
for i in combinations(range((w-1)*2), w-1):
    path = ["E" for _ in range((w-1) * 2)]
    for _i in i:
        path[_i] = "S"
    c = abs(cost(path))
    if minc == -1 or minc > c:
        minc = c
        minp = path
print("".join(minp))

Battle History

ConfigScoreDate
3 x 3 tiny1752019/05/19 10:29:00
3 x 3 tiny62019/05/19 10:29:00
3 x 3 tiny982019/05/19 10:29:00
5 x 5 small32019/05/19 10:29:00
5 x 5 small52019/05/19 10:29:00
5 x 5 small12019/05/19 10:29:00
10 x 10 middle02019/05/19 10:29:00
30 x 30 large622019/05/19 10:29:00