728x90

2022.01.11

백준 7576 토마토

#7576
from collections import deque

M, N = map(int, input().split())
arr = []
for i in range(N):
    arr.append(list(map(int,input().split())))
    
def BFS(graph):
    queue = deque()
    for i in range(N):
        for j in range(M):
            if arr[i][j] == 1:
                queue.append((i,j)) #토마토가 여러개 있을 때를 해결
    
    dx = [0,0,-1,1]
    dy = [-1,1,0,0]
    while queue:
        a, b = queue.popleft()
        for i in range(4):
            nx = a + dx[i]
            ny = b + dy[i]
            if 0 <= nx < N and 0 <= ny < M:
                if arr[nx][ny] == 0:
                    queue.append((nx,ny))
                    arr[nx][ny] = arr[a][b] + 1
            
BFS(arr)
R = []
for i in range(N):
    if 0 in arr[i]:
        R.append(0)
    else:
        R.append(max(arr[i]))
if 0 in R:
    print(-1)
else:
    print(max(R)-1)       

토마토가 여러 개 존재하면 어떻게 풀지?라는 의문을 반복문을 통해 해결했더니 

다른 BFS 문제와 별다를 게 없었다. 또 다른 점이라면 출력 조건이 좀 많다는 점?

728x90

'Algorithm' 카테고리의 다른 글

백준 1181 파이썬  (0) 2022.01.12
백준 1085 파이썬  (0) 2022.01.11
백준 1018 파이썬  (0) 2022.01.11
백준 10951 파이썬  (0) 2022.01.10
백준 10871 파이썬  (0) 2022.01.10
728x90

체스판 다시 칠하기

# 1018
W = [['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W']]
B = [['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],
     ['B','W','B','W','B','W','B','W'],
     ['W','B','W','B','W','B','W','B'],]

N, M = map(int,input().split())
arr = []
for i in range(N):
    arr.append(list(input()))
    
def chess(x,y):
    wcnt = 0
    bcnt = 0
    if x + 8 <= N and y + 8 <= M:
        for i in range(8):
            for j in range(8):
                if arr[i+x][j+y] == W[i][j]:
                    wcnt += 1
                else:
                    bcnt += 1
    return R.append(max(wcnt,bcnt))
                      
R = []
for i in range(N):
    for j in range(M):
        chess(i,j)
print(64-max(R))

첫 칸이 흰색이거나 검은색인 두 가지 경우의 수만 있다고 해서 먼저 두 가지 케이스를 리스트로 

만들어놨다. 그리고 이 케이스를 입력받는 N*M에 한 칸씩 넘어가면서 대조해보고 수정할 값이 

가장 적은 것을 가져왔다.

리스트를 만들어서 대조하기 전에 ['W', 'B'] 나 ['B', 'W']같이 짧은 리스트로도 풀 수 있을 거 같은 

느낌이지만 어떻게 풀어야할지 생각이 안 나네 다음에 시도해봐야겠다.

728x90

'Algorithm' 카테고리의 다른 글

백준 1085 파이썬  (0) 2022.01.11
백준 7576 파이썬  (0) 2022.01.11
백준 10951 파이썬  (0) 2022.01.10
백준 10871 파이썬  (0) 2022.01.10
백준 10818 파이썬  (0) 2022.01.10
728x90

A+B - 4

#10951
while True:
    try:
        A , B = map(int, input().split())
        print(A+B)
    except:
        break

따로 몇 번을 반복해야 하는지 주어지지 않는다. 때문에 while문으로 무한루프를 만들고

try, except 구문을 사용하여 오류가 발생했을 때 (입력값이 주어지지 않을 시)

while문의 무한루프에서 빠져나오게 만들었다.

728x90

'Algorithm' 카테고리의 다른 글

백준 7576 파이썬  (0) 2022.01.11
백준 1018 파이썬  (0) 2022.01.11
백준 10871 파이썬  (0) 2022.01.10
백준 10818 파이썬  (0) 2022.01.10
백준 2178 파이썬 (BFS)풀이  (0) 2022.01.10
728x90

X보다 작은 수

# 10871
N, X = map(int, input().split())
A = list(map(int, input().split()))
for i in A:
    if i < X:
        print(i, end = ' ')

 

728x90

'Algorithm' 카테고리의 다른 글

백준 1018 파이썬  (0) 2022.01.11
백준 10951 파이썬  (0) 2022.01.10
백준 10818 파이썬  (0) 2022.01.10
백준 2178 파이썬 (BFS)풀이  (0) 2022.01.10
백준 10809 파이썬  (0) 2022.01.09
728x90

최소, 최대

#10818
N = int(input())
arr = list(map(int, input().split()))
print(min(arr),max(arr))

위 코드가 훨씬 빠르다.

#10818
N = int(input())
arr = sorted(list(map(int, input().split())))
print(arr[0],arr[-1])
728x90

'Algorithm' 카테고리의 다른 글

백준 10951 파이썬  (0) 2022.01.10
백준 10871 파이썬  (0) 2022.01.10
백준 2178 파이썬 (BFS)풀이  (0) 2022.01.10
백준 10809 파이썬  (0) 2022.01.09
백준 8958 파이썬  (0) 2022.01.09
728x90

2022.01.10

백준 2178 미로 탐색

# 2178
from collections import deque

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

def BFS(x,y):
    dx = [0,-1, 1, 0]
    dy = [1, 0, 0, -1]
    queue = deque()
    queue.append((x,y))
    
    while queue:   
        a, b = queue.popleft()
        for i in range(4):
            nx = a + dx[i]
            ny = b + dy[i]
            
            if 0<=nx<N and 0<=ny<M:
                if arr[nx][ny] == 1:
                    queue.append((nx,ny))
                    arr[nx][ny] = arr[a][b] + 1

BFS(0,0)
print(arr[N-1][M-1])

칸을 지나면 그다음 칸을 +1 해주는 아이디어로 풀었다.

방문처리를 안해서 중복 방문하기 때문에 다른 좌표들은 문제가 있지만

끝 좌표는 더이상 진행할 곳이 없어서 문제없다.

 

# 2178 방문처리 한 코드
from collections import deque

N, M = map(int, input().split())
arr = [list(map(int, input())) for i in range(N)]
visited = [[False]*M for i in range(N)]

def BFS(x,y):
    dx = [0,-1, 1, 0]
    dy = [1, 0, 0, -1]
    queue = deque()
    queue.append((x,y))
    
    while queue:   
        a, b = queue.popleft()
        for i in range(4):
            nx = a + dx[i]
            ny = b + dy[i]
            
            if 0<=nx<N and 0<=ny<M:
                if arr[nx][ny] == 1 and visited[nx][ny] == False:
                    queue.append((nx,ny))
                    visited[nx][ny] = True
                    arr[nx][ny] = arr[a][b] + 1

BFS(0,0)
print(arr[N-1][M-1])

방문처리 코드를 넣어도 시간이나 메모리에서 그다지 차이는 보이지 않았다.

728x90

'Algorithm' 카테고리의 다른 글

백준 10871 파이썬  (0) 2022.01.10
백준 10818 파이썬  (0) 2022.01.10
백준 10809 파이썬  (0) 2022.01.09
백준 8958 파이썬  (0) 2022.01.09
백준 3052 파이썬  (0) 2022.01.09
728x90
# 10809
alphabet = []
arr = [-1 for i in range(26)]
for i in range(97,123):
  alphabet.append(chr(i))
S = input()
for i in S:
  if arr[alphabet.index(i)] == -1:
    arr[alphabet.index(i)] = S.index(i)

for i in arr:
  print(i, end = ' ')
728x90

'Algorithm' 카테고리의 다른 글

백준 10818 파이썬  (0) 2022.01.10
백준 2178 파이썬 (BFS)풀이  (0) 2022.01.10
백준 8958 파이썬  (0) 2022.01.09
백준 3052 파이썬  (0) 2022.01.09
백준 2675 파이썬  (2) 2022.01.08
728x90
# 8958
T = int(input())

for i in range(T):
  R = 0              # 총 합
  P = 0              # 'O'이 나올 때마다 그 문제의 점수
  a = list(input())
  for j in a:
    if j == 'O':
      P += 1
      R = R+P 
    else:
      P = 0         # 'X'가 나오면 점수 초기화
  print(R)
728x90

'Algorithm' 카테고리의 다른 글

백준 2178 파이썬 (BFS)풀이  (0) 2022.01.10
백준 10809 파이썬  (0) 2022.01.09
백준 3052 파이썬  (0) 2022.01.09
백준 2675 파이썬  (2) 2022.01.08
백준 2439 파이썬  (0) 2022.01.08
728x90
# 3052
arr = []

for i in range(10):
  a = int(input())
  arr.append(a%42)

print(len(list(set(arr))))

 

728x90

'Algorithm' 카테고리의 다른 글

백준 10809 파이썬  (0) 2022.01.09
백준 8958 파이썬  (0) 2022.01.09
백준 2675 파이썬  (2) 2022.01.08
백준 2439 파이썬  (0) 2022.01.08
백준 1157 파이썬  (0) 2022.01.08
728x90
# 2675
T = int(input())

for i in range(T):
  R, S = input().split()
  R = int(R)
  P = list(S)
  Q = ''
  for j in P:
    Q = Q+j*R
  print(Q)

print(, end ='')으로 풀면 되겠네하고 쉽다고 생각했다가 머리 쥐 나는 줄 알았다.

 

728x90

'Algorithm' 카테고리의 다른 글

백준 8958 파이썬  (0) 2022.01.09
백준 3052 파이썬  (0) 2022.01.09
백준 2439 파이썬  (0) 2022.01.08
백준 1157 파이썬  (0) 2022.01.08
백준 2667 파이썬 (BFS풀이)  (0) 2022.01.07

+ Recent posts