문제

https://www.acmicpc.net/problem/14891

예제

[input]
10101111
01111101
11001110
00000010
2
3 -1
1 1

[output]
7

[input]
11111111
11111111
11111111
11111111
3
1 1
2 1
3 1

[output]
15

[input]
10001011
10000011
01011011
00111101
5
1 1
2 1
3 1
4 1
1 -1

[output]
6

[input]
10010011
01010011
11100011
01010101
8
1 1
2 1
3 1
4 1
1 -1
2 -1
3 -1
4 -1

[output]
5

모범 답안

1. 여기를 클릭하면 답이 보입니다
def rotatedIndex(current, reverse=False):
    if reverse is True:
        if current + 2 < 9:
            return current + 2 > 0
        else:
            return (current+2)-8
    if current - 2 > 0:
        return current - 2
    else:
        return 8+(current-2)


def getMovingGears(gears, adjs, gearNum, _dir, movings):
    for i in range(len(adjs[gearNum])):
        sideGearNum = adjs[gearNum][i][0]
        cp = adjs[gearNum][i][1]  # contactPoint
        if len([tup for tup in movings if tup[0] == sideGearNum]) > 0:
            continue
        for j in range(len(adjs[sideGearNum])):
            scp = adjs[sideGearNum][j][1]  # sideContactPoint
            if gears[gearNum-1][cp-1] == gears[sideGearNum-1][scp-1]:
                continue
            if gearNum == adjs[sideGearNum][j][0]:
                movings.append([sideGearNum, _dir*-1])
                return getMovingGears(gears, adjs, sideGearNum, _dir*-1, movings)
    return movings


def answer(gears, gearMoving):
    adjs = {
        1: [[2, 3]],
        2: [[1, 7], [3, 3]],
        3: [[2, 7], [4, 3]],
        4: [[3, 7]]
    }
    for i in range(len(gearMoving)):
        gearNum = gearMoving[i][0]
        _dir = gearMoving[i][1]
        movings = getMovingGears(gears, adjs, gearNum, _dir, [[gearNum, _dir]])
        # mgn = moving gear number
        for gn, d in movings:
            for j in range(len(adjs[gn])):
                adjs[gn][j][1] += d*-1
                if adjs[gn][j][1] == 9:
                    adjs[gn][j][1] = 1
    _sum = 0
    if gears[0][rotatedIndex(adjs[1][0][1])-1] == 1:
        _sum += 1
    if gears[1][rotatedIndex(adjs[2][1][1])-1] == 1:
        _sum += 2
    if gears[2][rotatedIndex(adjs[3][1][1])-1] == 1:
        _sum += 4
    if gears[3][rotatedIndex(adjs[4][0][1], True)-1] == 1:
        _sum += 8
    return _sum


gear1 = [int(i) for i in input().rstrip()]
gear2 = [int(i) for i in input().rstrip()]
gear3 = [int(i) for i in input().rstrip()]
gear4 = [int(i) for i in input().rstrip()]
k = int(input())
gearMoving = []
for _ in range(k):
    gearMoving.append(list(map(int, input().split())))

print(answer([gear1, gear2, gear3, gear4], gearMoving))
2. 여기를 클릭하면 답이 보입니다
def rotatedIndex(current, reverse=False):
    if reverse is True:
        if current + 2 < 9:
            return current + 2 > 0
        else:
            return (current+2)-8
    if current - 2 > 0:
        return current - 2
    else:
        return 8+(current-2)


def getMovingGears(gearNum, _dir, movings):
    global gears
    global adjs
    for i in range(len(adjs[gearNum])):
        sideGearNum = adjs[gearNum][i][0]
        cp = adjs[gearNum][i][1]  # contactPoint
        if len([tup for tup in movings if tup[0] == sideGearNum]) > 0:
            continue
        for j in range(len(adjs[sideGearNum])):
            scp = adjs[sideGearNum][j][1]  # sideContactPoint
            if gears[gearNum-1][cp-1] == gears[sideGearNum-1][scp-1]:
                continue
            if gearNum == adjs[sideGearNum][j][0]:
                movings.append([sideGearNum, _dir*-1])
                return getMovingGears(sideGearNum, _dir*-1, movings)
    return movings


gear1 = [int(i) for i in input()]
gear2 = [int(i) for i in input()]
gear3 = [int(i) for i in input()]
gear4 = [int(i) for i in input()]
k = int(input())
gearMoving = [list(map(int, input().split())) for _ in range(k)]
gears = [gear1, gear2, gear3, gear4]
adjs = {
    1: [[2, 3]],
    2: [[1, 7], [3, 3]],
    3: [[2, 7], [4, 3]],
    4: [[3, 7]]
}
for i in range(len(gearMoving)):
    gearNum = gearMoving[i][0]
    _dir = gearMoving[i][1]
    movings = getMovingGears(gearNum, _dir, [[gearNum, _dir]])
    # mgn = moving gear number
    for gn, d in movings:
        for j in range(len(adjs[gn])):
            adjs[gn][j][1] += d*-1
            if adjs[gn][j][1] == 9:
                adjs[gn][j][1] = 1
_sum = 0
if gears[0][rotatedIndex(adjs[1][0][1])-1] == 1:
    _sum += 1
if gears[1][rotatedIndex(adjs[2][1][1])-1] == 1:
    _sum += 2
if gears[2][rotatedIndex(adjs[3][1][1])-1] == 1:
    _sum += 4
if gears[3][rotatedIndex(adjs[4][0][1], True)-1] == 1:
    _sum += 8
print(_sum)