Zerojudge m932. 蜜蜂觀察

題解

一個二維陣列的題目。蜂巢轉成二維陣列後0~5的方向畫在紙上會比較好轉換

Python 解答

#從六角形變成二維陣列,方向的轉換要在紙上畫一下
dirs = {
    0:(-1, 0 ), #正上 
    1:(0 , 1 ), #正右
    2:(1 , 1 ), #右下
    3:(1 , 0 ), #正下
    4:(0 , -1), #正左 
    5:(-1, -1), #左上
}

m, n, k = map(int, input().split())
hexes = [list(input()) for _ in range(m)]
commands = list(map(int, input().split()))

nowr = m-1 #row相當於y座標 就是縱向的
nowc = 0   #column相當於x座標 就是橫向的

path = []

for command in commands:
    newr = nowr+dirs[command][0]
    newc = nowc+dirs[command][1]
    
    if (0<=newr<=m-1 and 0<=newc<=n-1): #true: 沒出界
        nowr, nowc = newr, newc
    
    path.append(hexes[nowr][nowc])

uniquenum = len(set(path))

print(*path, sep="")
print(uniquenum)