Programming/Problem Solving

[BOJ] 20061 모노미노도미노 - Python

징석 2021. 7. 21. 08:10
728x90

이 문제도 깡 구현 문제이다.

오른쪽으로 가는 것, 아래로 가는 함수를 따로 나누고, 범위를 잘 생각해서 점수를 더하고 없애주면 된다.

N = int(input())
blue = [[0]*6 for _ in range(4)]
green = [[0]*4 for _ in range(6)]
point = 0
def goBlue(t,xx,yy):
    global blue, green
    x = xx
    y = 0
    if t == 1:
        while True:
            if y > 5 or blue[x][y] != 0:
                y -= 1
                blue[x][y] = 1
                break
            y += 1
    if t == 2:
        y = 1
        while True:
            if y > 5 or blue[x][y] != 0 :
                y -= 1
                blue[x][y] = 1
                blue[x][y-1] = 1
                break
            y += 1
    if t == 3:
        x1 = x
        x2 = x+1
        while True:
            if y > 5 or blue[x1][y] != 0 or blue[x2][y] != 0:
                y -= 1
                blue[x1][y] = 1
                blue[x2][y] = 1
                break
            y += 1
def goGreen(t,xx,yy):
    global green
    x = 0
    y = yy
    if t == 1:
        while True:
            if x > 5 or green[x][y] != 0:
                x -= 1
                green[x][y] = 1
                break
            x += 1
    elif t == 2:
        y1 = y
        y2 = y + 1
        while True:
            if x > 5 or green[x][y1] != 0 or green[x][y2] != 0:
                x -= 1
                green[x][y1] = 1
                green[x][y2] = 1
                break
            x += 1
    elif t == 3:
        x = 1
        while True:
            if x > 5 or green[x][y] != 0:
                x -= 1
                green[x][y] = 1
                green[x-1][y] = 1
                break
            x += 1

def greenDel():
    global green
    ind = 0
    check = False
    for i in range(2):
        for j in range(4):
            if green[i][j] == 1:
                ind = i
                check = True
                break
        if check:
            break
    if check == True:
        if ind == 0: # 두 칸 밑으로 2리턴
            for i in range(3, -1, -1):
                for j in range(4):
                    green[i+2][j] = green[i][j]
                    green[i][j] = 0
            return 2
        elif ind == 1: # 한칸 밑으로, 1 리턴
            for i in range(4, -1, -1):
                for j in range(4):
                    green[i+1][j] = green[i][j]
                    green[i][j] = 0
            return 1
    return 0
def blueDel():
    global blue
    ind = 0
    check = False
    for j in range(2):
        for i in range(4):
            if blue[i][j] == 1:
                ind = j
                check = True
                break
        if check:
            break
    if check == True:
        if ind == 0:  # 두 칸 옆으로, 2리턴
            for j in range(4):
                for i in range(3, -1, -1):
                    blue[j][i+2] = blue[j][i]
                    blue[j][i] = 0
            return 2
        elif ind == 1:  # 한칸 옆 으로, 1 리턴
            for j in range(4):
                for i in range(4, -1, -1):
                    blue[j][i+1] = blue[j][i]
                    blue[j][i] = 0
            return 1
    return 0
def checklineGreen():
    global green
    ind = 5
    ret = 0
    while True:
        if ind < 2:
            break
        linecnt = 0
        for j in range(4):
            if green[ind][j] == 1:
                linecnt += 1
        if linecnt == 4:
            ret += 1
            for i in range(ind-1, -1, -1):
                for j in range(4):
                    green[i+1][j] = green[i][j]
            continue
        else:
            ind -= 1

    return ret

def checklineBlue():
    global blue
    ind = 5
    ret = 0
    while True:
        if ind < 2:
            break
        linecnt = 0
        for i in range(4):
            if blue[i][ind] == 1:
                linecnt += 1
        if linecnt == 4:
            ret += 1
            for j in range(4):
                for i in range(ind-1, -1, -1):
                    blue[j][i + 1] = blue[j][i]
            continue
        else:
            ind -= 1

    return ret

def two():
    cnt = 0
    for i in range(4):
        for j in range(6):
            if blue[i][j] == 1:
                cnt += 1

    for i in range(6):
        for j in range(4):
            if green[i][j] == 1:
                cnt += 1
    return cnt

ree=0
for i in range(N):
    t,x,y = map(int, input().split())
    goBlue(t,x,y)
    goGreen(t,x,y)
    ree += checklineGreen()
    ree += checklineBlue()
    blueDel()
    greenDel()

print(ree)
print(two())