๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐‘†๐‘ข๐‘›๐‘ โ„Ž๐‘–๐‘›๐‘’ ๐‘Ž๐‘“๐‘ก๐‘’๐‘Ÿ ๐‘Ÿ๐‘Ž๐‘–๐‘›โœง

[Python] ๋ฐฑ์ค€ 1283 & ๋ฐฑ์ค€ 3055 ๋ณธ๋ฌธ

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))