from itertools import permutations

import sys
input = sys.stdin.readline
def rotate(command,moving_arr):
    global arr
    cnt = 1
    while cnt<=command[2]:
        start_row,start_col = command[0]-cnt, command[1]-cnt
        end_row,end_col = command[0] + cnt,command[1] + cnt
        for row in range(start_row,end_row+1):
            if row == start_row or row == end_row:
                if row == start_row:
                    for col in range(start_col,end_col+1):
                        if col == end_col:
                            moving_arr[row+1][col] = prev_arr[row][col]
                        else:
                            moving_arr[row][col+1] = prev_arr[row][col]
                else:
                    for col in range(end_col,start_col-1,-1):
                        if col == start_col:
                            moving_arr[row-1][col] = prev_arr[row][col]
                        else:
                            moving_arr[row][col-1] = prev_arr[row][col]
            else:
                moving_arr[row-1][start_col] = prev_arr[row][start_col]
                moving_arr[row+1][end_col] = prev_arr[row][end_col]
        cnt += 1


N,M,K = map(int,input().split())
arr = [list(map(int,input().split())) for _ in range(N)]
command_list = []
for _ in range(K):
    x,y,r = map(int,input().split())
    command_list.append((x-1,y-1,r))
result = float('inf')

for commands in permutations(command_list):
    move_arr = [row[:] for row in arr]
    prev_arr = [row[:] for row in arr]
    for command in commands:
        rotate(command,move_arr)
        prev_arr = [row[:] for row in move_arr]
    arr_min = min(list(map(sum,move_arr)))
    result = min(arr_min,result)
print(result)

 

 

 

from itertools import permutations

import sys
input = sys.stdin.readline


def rotate(command,move_arr):
    x,y,s = command
    for i in range(1,s+1):
        subtract_data = move_arr[x-i][y-i] # 좌측 최상단 꼭지점을 빼놓기
        for r in range(x-i,x+i): # 좌측 세로를 아래에서 위로 올리는 과정
            move_arr[r][y-i] = move_arr[r+1][y-i]
        for c in range(y-i,y+i):
            move_arr[x+i][c] = move_arr[x+i][c+1]
        for r in range(x+i,x-i,-1):
            move_arr[r][y+i] = move_arr[r-1][y+i]
        for c in range(y+i,y-i+1,-1):
            move_arr[x-i][c] = move_arr[x-i][c-1]
        move_arr[x-i][y-i+1] = subtract_data
        





N,M,K = map(int,input().split())
arr = [list(map(int,input().split())) for _ in range(N)]
command_list = []
for _ in range(K):
    x,y,r = map(int,input().split())
    command_list.append((x-1,y-1,r))
result = float('inf')


for commands in permutations(command_list):

    move_arr = [row[:] for row in arr]
    for command in commands:
        rotate(command,move_arr)
    arr_min = min(list(map(sum,move_arr)))
    result = min(arr_min,result)
print(result)

'알고리즘 > 백준_복기_미완료' 카테고리의 다른 글

[BOJ/백준] 17837 새로운 게임 2  (0) 2021.05.06
[BOJ/백준] 17779 게리맨더링 2  (0) 2021.05.06
[BOJ/백준] 17404 RGB 거리 2  (0) 2021.05.06
[BOJ/백준] 17298 오큰수  (0) 2021.05.06
[BOJ/백준] 17281 ⚾  (0) 2021.05.06
N = int(input())

INF = float('inf')



arr = [list(map(int,input().split())) for _ in range(N)]
result = float('inf')
for first_room in range(3):
    dp = [[INF]*3 for _ in range(N)]
    dp[0][first_room] = arr[0][first_room]
    for i in range(1,N-1):
        for k in range(3):
            for j in range(3):
                if k !=j:
                    dp[i][k] = min(dp[i-1][j]+arr[i][k],dp[i][k])

    for i in range(3):
        for j in range(3):
            if i != j and i != first_room:
                dp[N-1][i] = min(dp[N-2][j]+arr[N-1][i],dp[N-1][i])
    result = min(result,min(dp[N-1]))

print(result)

 

 

 

N = int(input())

arr = [list(map(int,input().split())) for _ in range(N)]


INF = float('inf')
result = INF
for first_room_color in range(3):
    dp = [[0]*3 for _ in range(N)]
    for i in range(3):
        if i == first_room_color:
            dp[0][i] = arr[0][i]
        else:
            dp[0][i] = INF

    for ind in range(1,N):
        dp[ind][0] = arr[ind][0] + min(dp[ind-1][1],dp[ind-1][2])
        dp[ind][1] = arr[ind][1] + min(dp[ind-1][0],dp[ind-1][2])
        dp[ind][2] = arr[ind][2] + min(dp[ind-1][0],dp[ind-1][1])

    for last_room_color in range(3):
        if last_room_color != first_room_color:
            if result > dp[N-1][last_room_color]:
                result = dp[N-1][last_room_color]

print(result)

 

N = int(input())

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

arr.reverse()
result = [-1]
stack = [arr[0]]

idx = 1
while idx <N:
    while stack and stack[-1] <= arr[idx]:
        stack.pop()
    if stack:
        result.append(stack[-1])
    else:
        result.append(-1)
    stack.append(arr[idx])
    idx += 1
result.reverse()
print(*result)

 

import sys
input = sys.stdin.readline

N = int(input())
arr = list(map(int,input().split()))
result = [-1]*N
stack = [0]
idx = 1
while stack and idx<N:
    while stack and arr[stack[-1]] < arr[idx]:
        result[stack.pop()] = arr[idx]
    stack.append(idx)
    idx += 1

print(*result)

 

from itertools import permutations
import sys
input = sys.stdin.readline
def play(entry):
    out_count = 0
    innings = 0
    cu_player_ind = 0
    # 1루,2루,3루
    score = 0
    base1,base2,base3 = 0,0,0
    while innings < N:
        while out_count<3:
            if arr[innings][entry[cu_player_ind]-1] == 0:
                out_count += 1
            elif arr[innings][entry[cu_player_ind]-1] == 1:
                
                score += base3
                base1,base2,base3 = 1,base1,base2
            elif arr[innings][entry[cu_player_ind]-1] == 2:
                score += (base2+base3)
                base1,base2,base3 = 0,1,base1
            elif arr[innings][entry[cu_player_ind]-1] == 3:
                score += (base1+base2+base3)
                base1,base2,base3 = 0,0,1
            else:
                score = score + base1+base2+base3 + 1
                base1,base2,base3 = 0,0,0
            cu_player_ind = (cu_player_ind+1)%9
        out_count = 0
        base1,base2,base3 = 0,0,0
        innings += 1
    return score





N = int(input())

arr = [list(map(int,input().split())) for _ in range(N)]
result = 0
for _players in permutations(range(2,10)):
    _players = list(_players)
    players = _players[:3]+[1] + _players[3:]
    temp = play(players)
    if result <temp:
        result = temp
print(result)

 

 

from itertools import permutations
import sys
input = sys.stdin.readline

N = int(input())

arr = [list(map(int,input().split())) for _ in range(N)]
result = 0
for _players in permutations(range(2,10)):
    _players = list(_players)
    players = _players[:3]+[1] + _players[3:]
    cu_player_ind = 0
    # 1루,2루,3루
    score = 0
    for innings in arr:
        out_count = 0
        base1,base2,base3 = 0,0,0
        while out_count<3:
            if innings[players[cu_player_ind]-1] == 0:
                out_count += 1
            elif innings[players[cu_player_ind]-1] == 1:
                
                score += base3
                base1,base2,base3 = 1,base1,base2
            elif innings[players[cu_player_ind]-1] == 2:
                score += (base2+base3)
                base1,base2,base3 = 0,1,base1
            elif innings[players[cu_player_ind]-1] == 3:
                score += (base1+base2+base3)
                base1,base2,base3 = 0,0,1
            else:
                score = score + base1+base2+base3 + 1
                base1,base2,base3 = 0,0,0
            cu_player_ind = (cu_player_ind+1)%9
    if score > result:
        result = score
print(result)
n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11= map(int,input().split())

arr = [[[[[[[[[[list(map(int,input().split())) for _ in range(n2)] for _ in range(n3)] for _ in range(n4)] for _ in range(n5)] for _ in range(n6)] for _ in range(n7)] for _ in range(n8)] for _ in range(n9)] for _ in range(n10)] for _ in range(n11)]


tomato_cnt = 0
total = n1*n2*n3*n4*n5*n6*n7*n8*n9*n10*n11
tomato = []
for A in range(n11):
    for B in range(n10):
        for C in range(n9):
            for D in range(n8):
                for E in range(n7):
                    for F in range(n6):
                        for G in range(n5):
                            for H in range(n4):
                                for I in range(n3):
                                    for J in range(n2):
                                        for K in range(n1):
                                            if arr[A][B][C][D][E][F][G][H][I][J][K] == 1:
                                                tomato.append((A,B,C,D,E,F,G,H,I,J,K))


dK = [-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dJ = [0,0,-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dI = [0,0,0,0,-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dH = [0,0,0,0,0,0,-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dG = [0,0,0,0,0,0,0,0,-1,1,0,0,0,0,0,0,0,0,0,0,0,0]
dF = [0,0,0,0,0,0,0,0,0,0,-1,1,0,0,0,0,0,0,0,0,0,0]
dE = [0,0,0,0,0,0,0,0,0,0,0,0,-1,1,0,0,0,0,0,0,0,0]
dD = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,1,0,0,0,0,0,0]
dC = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,1,0,0,0,0]
dB = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,1,0,0]
dA = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,1]

day = 0
result = 0
while True:
    new_tomato = []
    for A,B,C,D,E,F,G,H,I,J,K in tomato:
        for l in range(22):
            nA = A + dA[l]
            nB = B + dB[l]
            nC = C + dC[l]
            nD = D + dD[l]
            nE = E + dE[l]
            nF = F + dF[l]
            nG = G + dG[l]
            nH = H + dH[l]
            nI = I + dI[l]
            nJ = J + dJ[l]
            nK = K + dK[l]
            if 0<=nA<n11 and 0<=nB<n10 and 0<=nC<n9 and 0<=nD<n8 and 0<=nE<n7 and 0<=nF<n6 and 0<=nG<n5 and 0<=nH<n4 and 0<=nI<n3 and 0<=nJ<n2 and 0<=nK<n1:
                if not arr[nA][nB][nC][nD][nE][nF][nG][nH][nI][nJ][nK]:
                    arr[nA][nB][nC][nD][nE][nF][nG][nH][nI][nJ][nK] = 1
                    new_tomato.append((nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK))

    if len(new_tomato):
        tomato = new_tomato[:]
        day += 1
    else:
        result = day
        for A in range(n11):
            for B in range(n10):
                for C in range(n9):
                    for D in range(n8):
                        for E in range(n7):
                            for F in range(n6):
                                for G in range(n5):
                                    for H in range(n4):
                                        for I in range(n3):
                                            for J in range(n2):
                                                for K in range(n1):
                                                    if arr[A][B][C][D][E][F][G][H][I][J][K] == 0:
                                                        result = -1
        break

print(result)
        

 

def dfs(x,y,cnt):
    global result
    if cnt > result:
        return
    if x >= 10:
        result = min(result,cnt)
        return
    if y >= 10:
        dfs(x+1,0,cnt)
        return
    if arr[x][y] == 1:
        for paper_size in range(5,0,-1):
            if paper[paper_size]:
                if x + paper_size <= 10 and y + paper_size <= 10:
                    flag = False
                    for dx in range(paper_size):
                        for dy in range(paper_size):
                            if not arr[x+dx][y+dy]:
                                flag = True
                                break
                        if flag:
                            break
                    if not flag:
                        for dx in range(paper_size):
                            for dy in range(paper_size):
                                arr[x+dx][y+dy] = 0
                        paper[paper_size] -= 1
                        dfs(x,y+paper_size-1,cnt+1)
                        paper[paper_size] += 1
                        for dx in range(paper_size):
                            for dy in range(paper_size):
                                arr[x+dx][y+dy] = 1
    else:
        dfs(x,y+1,cnt)

        


arr = [list(map(int,input().split())) for _ in range(10)]
paper = [0]+[5]*5
result = float('inf')
dfs(0,0,0)
print(result if result != float('inf') else -1)

 

 

 

def check(x,y,size):
    if x + size > 10 or y + size >10:
        return False
    for dx in range(size):
        for dy in range(size):
            if not arr[x+dx][y+dy]:
                return False
    return True


def set_position(x,y,size,val):
    for dx in range(size):
        for dy in range(size):
            arr[x+dx][y+dy] = val
def dfs(x,y,cnt):
    global result
    if cnt >= result:
        return
    finish = True
    while x < 10:
        while y < 10:
            if arr[x][y] == 1:
                finish = False
                for paper_size in range(5,0,-1):
                    if paper[paper_size] and check(x,y,paper_size):
                        set_position(x,y,paper_size,0)
                        paper[paper_size] -= 1
                        dfs(x,y+paper_size-1,cnt+1)
                        paper[paper_size] += 1
                        set_position(x,y,paper_size,1)
                return
            y += 1
        x += 1
        y = 0
    if finish:
        result = min(result,cnt)
        


arr = [list(map(int,input().split())) for _ in range(10)]
paper = [0]+[5]*5
result = float('inf')
dfs(0,0,0)
print(result if result != float('inf') else -1)
import sys



input =sys.stdin.readline

def wall_move(origin_list):
    temp = []

    for x,y in origin_list:
        if x == 7:
            continue
        else:
            arr[x][y] = '.'
            temp.append((x+1,y))
    for x,y in temp:
        arr[x][y] = '#'
    return temp



arr = []
wall = []
for x in range(8):
    input_list = list(input().strip())
    for y in range(8):
        if input_list[y] == '#':
            wall.append((x,y))
    arr.append(input_list)

start = (7,0)

stack = []
stack.append(start)
result = 0
times = 0
dx = [-1,0,1,-1,0,1,-1,0,1]
dy = [-1,-1,-1,0,0,0,1,1,1]
while stack:
    visited = [[True]* 8 for _ in range(8)]
    new_stack = []

    for x,y in stack:
        if arr[x][y] == '#':
            continue
        for i in range(9):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0<=nx<8 and 0<=ny<8 and arr[nx][ny] == '.' and visited[nx][ny]:
                new_stack.append((nx,ny))
                visited[nx][ny] = False
    
    if (0,7) in new_stack:
        result = 1
        break


    wall = wall_move(wall)

    stack = new_stack[:]

print(result)
import sys

input = sys.stdin.readline
def calc_operator(operator,x,y):
    if operator == '*':
        return x*y
    elif operator == '-':
        return x-y
    elif operator == '+':
        return x+y


def calc():
    new_num_list = []
    new_operator_list = []
    ind = 0
    while ind<len(num_list):
        if ind < len(num_list)-1:
            if not open_bracket[ind]:
                new_num_list.append(num_list[ind])
                new_operator_list.append(operator_list[ind])
                ind += 1
            else:
                new_number = calc_operator(operator_list[ind],num_list[ind],num_list[ind+1])
                if ind == len(num_list)-2:
                    new_num_list.append(new_number)
                else:
                    new_num_list.append(new_number)
                    new_operator_list.append(operator_list[ind+1])
                ind += 2
        else:
            new_num_list.append(num_list[ind])
            ind+= 1
    ind = 0
    value = new_num_list[0]
    while ind < len(new_num_list)-1:
        value = calc_operator(new_operator_list[ind],value,new_num_list[ind+1])
        ind += 1
    return value







def dfs(index):
    global result
    if index == len(num_list)-1:
        result = max(calc(),result)
        return
    else:
        if visited[index]:
            visited[index] = False
            visited[index+1] = False
            open_bracket[index] = True
            dfs(index+1)
            visited[index] = True
            visited[index+1] = True
            open_bracket[index] = False
        dfs(index+1)

N = int(input())
arr = list(input())
if N == 1:
    print(arr[0])
else:
    num_list = []
    operator_list = []
    for i in range(N):
        if i%2:
            operator_list.append(arr[i])
        else:
            num_list.append(int(arr[i]))
    result = -float('inf')
    visited = [True]*len(num_list)
    open_bracket = [False]*len(num_list)
    dfs(0)
    print(result)

 

 

import sys

input = sys.stdin.readline
def calc(operator,x,y):
    if operator == '*':
        return x*y
    elif operator == '-':
        return x-y
    elif operator == '+':
        return x+y
def dfs(idx,value):
    global result
    if idx == length_num-1:
        result = max(result,value)
        return
    temp = calc(operator_list[idx],value,num_list[idx+1])
    dfs(idx+1,temp)

    if idx < length_num-2:
        next_value = calc(operator_list[idx+1],num_list[idx+1],num_list[idx+2])
        temp = calc(operator_list[idx],value,next_value)
        dfs(idx+2,temp)



N = int(input())
origin_input = list(input().strip())
num_list = []
operator_list = []

for ind in range(len(origin_input)):
    if ind%2:
        operator_list.append(origin_input[ind])
    else:
        num_list.append(int(origin_input[ind]))
result = -float('inf')
length_num = len(num_list)

dfs(0,num_list[0])
print(result)

+ Recent posts