𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴💻/[𝐏𝐲𝐭𝐡𝐨𝐧] 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦
[Python] 백준 1283 & 백준 3055
🤍Ryusun🤍
2023. 6. 10. 23:30
1283
문제는 쉬웠지만 풀기 까다로웠던 문제ㅠㅠ
몇시간만에 짜릿하게 성공
# 백준 1283
n = int(input())
dict = {}
for _ in range(n):
word = str(input()).rstrip("\n")
upper_word = word.upper()
if " " not in word:
if upper_word[0] not in dict:
dict[upper_word[0]] = 1
print("[" + word[0] + "]" + word[1:])
continue
else:
for i in range(len(word)):
if i == len(word)-1 and upper_word[i] in dict:
print(word)
elif upper_word[i] not in dict:
dict[upper_word[i]] = 1
print(word[:i] +"[" + word[i] + "]" + word[i+1:])
break
else:
flag = False
word_list= word.split(' ')
split_word_list = upper_word.split(' ')
for n in range(len(split_word_list)):
if split_word_list[n][0] not in dict:
dict[split_word_list[n][0]] = 1
word_list[n] = "[" + word_list[n][0] + "]" + word_list[n][1:]
print(" ".join(word_list))
flag = True
break
if flag:
continue
else:
for n in range(len(split_word_list)):
if flag :
break
for w in range(len(split_word_list[n])):
if split_word_list[n][w] not in dict:
dict[split_word_list[n][w]] = 1
word_list[n] = word_list[n][:w] + "[" + word_list[n][w] + "]" + word_list[n][w+1:]
print(" ".join(word_list))
flag = True
break
elif n == len(split_word_list) -1 and w == len(split_word_list[n])-1 and split_word_list[n][w] in dict:
print(word)
#3055 고슴도치
from collections import deque
n, m = map(int, input().split())
graph = []
for _ in range(n):
graph.append(list(input().rstrip("\n")))
visited = [[0] *m for _ in range(n)]
queue = deque()
d_row = [0, -1, 0, 1]
d_col = [1, 0, -1, 0]
def bfs(dx,dy):
while queue:
x, y = queue.popleft()
if graph[dx][dy] == 'S':
return visited[dx][dy]
for i in range(4):
nx= d_row[i] + x
ny = d_col[i] + y
if 0<= nx < n and 0<= ny < m:
if (graph[nx][ny] == '.' or graph[nx][ny] == 'D') and graph[x][y] == 'S':
graph[nx][ny] = 'S'
visited[nx][ny] = visited[x][y] + 1
queue.append([nx, ny])
elif (graph[nx][ny] == '.' or graph[nx][ny] == 'S') and graph[x][y] == '*':
graph[nx][ny] = '*'
queue.append([nx,ny])
return "KAKTUS"
x, y = 0, 0
for i in range(n):
for j in range(m):
if graph[i][j]== 'S':
queue.append([i,j])
elif graph[i][j] == 'D':
x, y = i, j
for w in range(n):
for v in range(m):
if graph[w][v] == '*':
queue.append([w, v])
print(bfs(x,y))