[Python] 17822 - 원판 돌리기, 17825 - 주사위 윷놀이

정환우·2022년 4월 30일
0
post-thumbnail

원판 돌리기

문제 링크

  • 난이도 : 골드 3

  • 문제 설명을 보면 22/6 이 평균으로 되어 있다. // 연산자 말고 / 연산자를 사용해야 한다.
  • 그리고 평균이 0인 경우가 생길 수 있다. ZeroDivision 문제가 발생할 수 있으므로, 0인 경우는 예외처리를 해줘야 한다.

이 두가지만 유의해서 구현하면 된다.

나는 i,j 조건이 헷갈릴 까봐 좌표도 일부러 앞에 0을 추가해줘서 1부터 사용하였고, 조건문도 그냥 문제 내용 그대로 쭉 나열해서 구현했다.

# 1시간 걸렸고,
# zero division Error 조심 

N,M,T = map(int,input().split())

circles = [[-1] for _ in range(N+1)]
for c in range(1,N+1):
    inp = list(map(int,input().split()))
    for num in inp:
        circles[c].append(num)

def rotate(i, direction):
    global circles
    tmp = [-1]
    # 시계 방향
    if direction == 0:
        tmp.append(circles[i][M])
        for idx in range(1,M):
            tmp.append(circles[i][idx])
    # 반시계 방향
    else:
        for idx in range(2,M+1):
            tmp.append(circles[i][idx])
        tmp.append(circles[i][1])

    circles[i] = tmp

# i = 1인경우
# j = 1인 경우
# i = N 인 경우
# j = N 인 경우
# 그 외의 경우


def adj(i, j):
    global circles
    global M
    global N
    global count
    global adj_pos
    global adj_nums
    global same_pos
    global isvisited

    if circles[i][j] == -1:
        return

    flag = False

    if j == 1:
        if circles[i][1] == circles[i][2] and not isvisited[i][2]:
            count += 1
            flag = True
            same_pos.append([i,2])

        if circles[i][1] == circles[i][M] and not isvisited[i][M]:
            count += 1
            flag = True
            same_pos.append([i,M])

    if i == 1:
        if circles[1][j] == circles[2][j] and not isvisited[2][j]:
            count += 1
            flag = True
            same_pos.append([2,j])

    if i == N:
        if circles[N][j] == circles[N-1][j] and not isvisited[N-1][j]:
            count += 1
            flag = True
            same_pos.append([N-1,j])

    if j == M:
        if circles[i][M] == circles[i][M-1] and not isvisited[i][M-1]:
            count += 1
            flag = True
            same_pos.append([i,M-1])

        if circles[i][M] == circles[i][1] and not isvisited[i][1]:
            count += 1
            flag = True
            same_pos.append([i,1])



    if 2<=j<=M-1:
        if circles[i][j] == circles[i][j-1] and not isvisited[i][j-1]:
            count += 1
            flag = True
            same_pos.append([i,j-1])

        if circles[i][j] == circles[i][j+1] and not isvisited[i][j+1]:
            count += 1
            flag = True
            same_pos.append([i,j+1])

    if 2<=i<=N-1:

        if circles[i][j] == circles[i-1][j] and not isvisited[i-1][j]:
            count += 1
            flag = True
            same_pos.append([i-1,j])

        if circles[i][j] == circles[i+1][j] and not isvisited[i+1][j]:
            count += 1
            flag = True
            same_pos.append([i+1,j])

    if flag:
        same_pos.append([i,j])



for _ in range(T):
    count = 0
    adj_nums = []
    adj_pos = []
    same_pos = []
    isvisited = [[False] * (M + 1) for _ in range(N + 1)]
    xi, di, ki = map(int,input().split())
    number = xi
    while True:
        if number > N:
            break

        for _ in range(ki):
            rotate(number, di)
        number += xi

    for y in range(1,N+1):
        for x in range(1,M+1):
            isvisited[y][x] = True
            adj(y,x)

    if count > 0:
        for y,x in same_pos:
            circles[y][x] = -1
    else:
        num_count = 0
        total = 0
        for y in range(1,N+1):
            for x in range(1,M+1):
                if circles[y][x] != -1:
                    total += circles[y][x]
                    num_count += 1
        
        if num_count == 0:
            continue
        avg = total / num_count
        for y in range(1,N+1):
            for x in range(1,M+1):
                if circles[y][x] != -1:
                    if circles[y][x] > avg:
                        circles[y][x] -= 1
                    elif circles[y][x] < avg:
                        circles[y][x] += 1

answer = 0
for y in range(1,N+1):
    for x in range(1,M+1):
        if circles[y][x] != -1:
            answer += circles[y][x]

print(answer)

주사위 윷놀이

문제 링크

  • 난이도 : 골드 2

이 문제는 진짜 디버깅할 때 오타 하나로 30분을 날려먹은문제..

나는 좌표를 분류해서 넣었는데, 파란색으로 가면 끝나는게아니라, [25,30,35] 는 파란색 끼리 공유하고 40은 전체가 공유한다는 걸 유의해서 풀어야 한다.

정답 코드

dices = list(map(int,input().split()))

start = [ 2*i for i in range(21)]
start.append(0)
tmp1 = [0,13,16,19,25,30,35,40,0]   # 10번 출발
tmp2 = [0,22,24,25,30,35,40,0]   # 20번 출발
tmp3 = [0,28,27,26,25,30,35,40,0]    # 30번 출발


answer = 0


# 몇 번 말이 이동 하는 지
def dfs(turn, horses, horse_num, score, map_num):
    global tmp1, tmp2, tmp3
    global start
    global dices
    global answer
    if turn == 10:
        answer = max(answer, score)
        return

    # 도착한 말이라 이동할 일 없다.
    if horses[horse_num][0] == 5:
        return

    # 말 좌표 복사
    tmp_horses = [[1, 0] for _ in range(4)]
    for h in range(4):
        tmp_horses[h] = [horses[h][0], horses[h][1]]

    now_dice = dices[turn]  # 현재 주사위
    horse_move = horses[horse_num][1] + now_dice    # 이동할 칸의 좌표

    graph = start
    # 맵 번호에 따라 맵이 다르다.
    if map_num == 2:
        graph = tmp1
    elif map_num == 3:
        graph = tmp2
    elif map_num == 4:
        graph = tmp3

    # 도착한 말
    if horse_move >= len(graph) - 1:
        tmp_horses[horse_num] = [5,0]
        horse_move = len(graph) - 1
    else:
        tmp_horses[horse_num] = [map_num, horse_move]
        if graph[horse_move] == 10:
            tmp_horses[horse_num] = [2, 0]
        elif graph[horse_move] == 20:
            tmp_horses[horse_num] = [3, 0]
        elif graph[horse_move] == 30 and map_num == 1:
            tmp_horses[horse_num] = [4, 0]

        # map 별로 40, 35 ,30, 25을 공유 하기 때문에, 둘 이상 앉는 경우는 안된다.
        if graph[horse_move] == 40:
            if [1,20] in horses or [2,7] in horses or [3,6] in horses or [4,7] in horses:
                return

        elif graph[horse_move] == 35:
            if [2,6] in horses or [3,5] in horses or [4,6] in horses:
                return

        elif graph[horse_move] == 30:
            if [2,5] in horses or [3,4] in horses or [4,5] in horses:
                if tmp_horses[horse_num] != [4, 0]:
                    return

        elif graph[horse_move] == 25:
            if [2,4] in horses or [3,3] in horses or [4, 4] in horses:
                return

        # 말이 있는 좌표 로는 못간다.
        if tmp_horses[horse_num] in horses:
            return

    for num in range(4):
        dfs(turn + 1, tmp_horses, num, score + graph[horse_move], tmp_horses[num][0])


horse = [[1,0] for _ in range(4)]
for n in range(4):
    dfs(0, horse, n, 0, 1)


print(answer)
profile
Hongik CE

0개의 댓글