-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
24-alstjr7437 #187
24-alstjr7437 #187
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฏผ์์์น Empty File!!!!!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from collections import deque
import sys
def input(): return sys.stdin.readline().rstrip()
M, N = map(int ,input().split())
tomato_box = []
ripe_tomatoes = deque()
visited = set()
for row in range(N):
row_tomato = list(map(int, input().split()))
for col, tomato in enumerate(row_tomato):
if tomato == 1:
visited.add((row, col))
ripe_tomatoes.append((row, col, 0))
tomato_box.append(row_tomato)
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
answer = 0
while ripe_tomatoes:
now_row, now_col, now_day = ripe_tomatoes.popleft()
answer = now_day
for dir in range(4):
new_row = now_row + dy[dir]
new_col = now_col + dx[dir]
if new_row < 0 or new_row >= N: continue
if new_col < 0 or new_col >= M: continue
if (new_row, new_col) in visited: continue
if tomato_box[new_row][new_col] != 0: continue
tomato_box[new_row][new_col] = 1
visited.add((new_row, new_col))
ripe_tomatoes.append((new_row, new_col, now_day+1))
flag = False
for row in range(N):
for col in range(M):
if tomato_box[row][col] == 0:
flag = True
break
if flag: break
print(-1) if flag == True else print(answer)
์ง !!!!!!!!!!!!!!!!
์๋ 10์ 50๋ถ์ ํธ๋ ค๊ณ ํ๋๊น ๋๊ธ์ ์ด๋ฏธ ์คํฌ๊ฐ ๋์๋๋ผ๊ตฌ์ @tgyuuAn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ํ๋ค....................
๐๐๐๐
if any(0 in l for l in tomato): | ||
print(-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค...................................................................... any
๋ง์๋๋ฐ์ ...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
22 ์ด๋ ๊ฒ ํ๋ฉด ๊ตณ์ด for๋ฌธ์ ๋๊ฐ ์ค์ฒฉํ ํ์๊ฐ ์๋ค์ ๐๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ฒ์์๋ ์ด์ค ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ง๋ค์ด์ ํ๋ค๊ฐ ๊ทธ๋ ๊ฒ ์ฐพ์ผ๋ฉด ๋ณ์๋ฅผ ํ๋ ๋๋ง๋ค์ด์ผ ๊ฒ ๋๋ผ๊ตฌ์..
๊ทธ๋์ any๋ผ๋ ํจ์๋ฅผ ์จ๋ดค์ต๋๋ค~~~~
tomato[ny][nx] = tomato[y][x] + 1 | ||
queue.append((ny, nx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํธ ๋ ์ง ์ ๋ณด๋ฅผ tomato
์ ๋ํ๋ ์์ผ๋ก ์ ์ฅํ๊ตฐ์ ์ข์๋์ ?????????
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
22 ์ ๊ทธ๋ฅ ์ด์ tamao ์์ +1 ํด๋ฒ๋ฆฌ๋ฉด ๋ฐ๋ก days ์ ์ฅํ ํ์๊ฐ ์๊ฒ ๋ค์ ใทใท
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๊ฒ๋ ์ฒ์์ max_daysํด์ ๋ฃ์๋๋ฐ ํ์ ๊ฐ์ด ์ ์ฅ์ ์ํ๋ฉด ๊ทธ๋ฅ BFS ๋๋๋ง๋ค +1์ด ๋๋ฒ๋ ค์ ์ถ๋ ฅ์ด ์ด์ํด์ง๊ธธ๋ ์ด๋กํ์ง ํ๋ค๊ฐ ์ด๋ฐฉ๋ฒ์ผ๋ก ํ์์ต๋๋ค!!!~!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋์ค์ ํ์ด๋ด์ผ์ง ~ ํ๊ณ ๊น๋จน๊ณ ์์๋๋ฐ ์ด๋ฒ ๊ธฐํ์ ํ์์ต๋๋ท
max_days ์ฒ๋ฆฌ์์ ๊ณ ๋ฏผ ์ซ ํ๋ค์ฉ.. ๊ทผ๋ฐ ๋ฏผ์๋ ์ฝ๋ ๋ณด๋๊น ๊ตณ์ด ๋ฐ๋ก ์ ์ฅ์ํ๊ณ ๋ฐ๋ก ๋ฐฉํฅ ํ์ ์ tomato์ +1๋ง ํด์ฃผ๋ฉด ๋๋ค์ ใทใท
๋ฌธ์ ํธ๋๋ผ ์๊ณ ํ์
จ์ต๋๋ท ~~
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()
M, N = map(int, input().split())
tomatos = [[int(i) for i in input().split()] for _ in range(N)]
visited = [[0 for _ in range(M)] for _ in range(N)]
dir = [(0,1), (0,-1), (1,0), (-1,0)]
q = deque()
max_days = 0
check = True # ์ต์ง ์์ ํ ๋งํ ๊ฐ ์๋์ง
# ์ต์ ํ ๋งํ ๋ฃ๊ธฐ
for i in range(N):
for k in range(M):
if tomatos[i][k] == 1:
q.append((0, i,k))
visited[i][k] == 1
while q:
days, x, y = q.popleft()
for dir_x, dir_y in dir:
if 0<= x+dir_x < N and 0<= y+dir_y < M:
if tomatos[x+dir_x][y+dir_y] == 0 and visited[x+dir_x][y+dir_y] == 0:
tomatos[x+dir_x][y+dir_y] = 1 # ์ต์ ํ ๋งํ ๋ง๋ค๊ธฐ
visited[x+dir_x][y+dir_y] = 1
max_days = days+1
q.append((days +1, x+dir_x, y+dir_y))
for i in range(N):
for k in range(M):
if tomatos[i][k] == 0:
check = False
if check:
print(max_days)
else:
print(-1)
tomato[ny][nx] = tomato[y][x] + 1 | ||
queue.append((ny, nx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
22 ์ ๊ทธ๋ฅ ์ด์ tamao ์์ +1 ํด๋ฒ๋ฆฌ๋ฉด ๋ฐ๋ก days ์ ์ฅํ ํ์๊ฐ ์๊ฒ ๋ค์ ใทใท
if any(0 in l for l in tomato): | ||
print(-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
22 ์ด๋ ๊ฒ ํ๋ฉด ๊ตณ์ด for๋ฌธ์ ๋๊ฐ ์ค์ฒฉํ ํ์๊ฐ ์๋ค์ ๐๐
if any(0 in l for l in tomato): | ||
print(-1) | ||
else : | ||
test = max(map(max, tomato)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํธ๋ผ.. ์๋ ๊ฒ ํ๋ฉด tomato์ ๋ํ ์ต๋๊ฐ์ ๋ฐ๋ก ์ฐพ์ ์ ์๊ตฐ์..
์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ ์ข๋ค์ ใทใท ๐ ๋ฐฐ์๊ฐ๋๋ค (_ _)
ํ์ฅฌ๋ฅด ๋๋ ์์กด ๋น์ท ๋๋ฐ |
์ ๋ ๋ ์ง ์ฒ๋ฆฌ ๋ถ๋ถ์์ ์ฒ์์ max_days๋ฅผ ๋ค 1์ฉ ์ถ๊ฐํ๋ฉด์ ํ๋๋ฐ ๊ทธ๋ ๊ฒ ํ๋๊น ์์ ์์ 25์ธ๊ฐ ๋์ค๋๋ผ๊ตฌ์ ใทใท ๊ทธ๋์ ์์ ๊ฐ์ด ๋ฐฉ์์ ๋ฐ๊ฟจ์ต๋๋ค! ์ด์งํผ 0์ด ์์ผ๋ฉด ๋์๊ฐ๋๊น 1๋ก ๋ฐ๊ฟ์ฃผ์ง ์๊ณ ๋ ์ง๋ก ๋ฐ๊ฟ์คฌ์ต๋๋น |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋จผ์ ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ์ ๋ฏผ์๋์ด๋ ๊ฑฐ์ ๋๊ฐ์ด ํ์์ต๋๋ค. ๋์ visited ๋ ์ฐ์ง ์์๊ตฌ์.
private val dx_1 = listOf(0, 0, 1, -1)
private val dy_1 = listOf(1, -1, 0, 0)
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (m, n) = br.readLine().split(" ").map { it.toInt() }
val graph = Array(n) { Array(m) { 0 } }
val q = ArrayDeque<Pair<Int, Int>>()
repeat(n) {
val line = br.readLine().split(" ").map { it.toInt() }
line.forEachIndexed { index: Int, item: Int ->
graph[it][index] = item
if (item == 1) {
q.add(it to index)
}
}
}
while (q.isNotEmpty()) {
val now = q.removeFirst()
for (i in 0..3) {
val nextX = now.first + dx_1[i]
val nextY = now.second + dy_1[i]
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) {
continue
}
if (graph[nextX][nextY] != 0) {
continue
}
graph[nextX][nextY] = graph[now.first][now.second] + 1
q.add(nextX to nextY)
}
}
//flatten() -> 2์ฐจ์์ 1์ฐจ์์ผ๋ก ๋ฐ๊พผ๋ค
val flattenGraph = graph.flatten()
if (flattenGraph.contains(0)) println(-1) else println(flattenGraph.max() - 1)
}
๋ง์ง๋ง์ max ๊ฐ ๊ณ์ฐ์ด๋ 0์ ์ฐพ๋ ๋ถ๋ถ์์ ์๊ฐ์ ๋ ๋น ๋ฅด๊ฒ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๊น ์๊ฐํ์ต๋๋ค.
- ์ฒ์ ๋ฐ์ดํฐ ๋ฐ์ ๋ 0 ์ธ ๊ฒ๋ค (์์ต์ ํ ๋งํ ) ๋ค ์ ๊ฐ์๋ฅผ ์ ์ฅํด๋๊ณ ํ๋ ํ์ํ ๋๋ง๋ค count --
- max ๋ฅผ 1๋ก ์ด๊ธฐํํ๊ณ ๊ณ์ ์ ๋ฐ์ดํธ
private val dx_1 = listOf(0, 0, 1, -1)
private val dy_1 = listOf(1, -1, 0, 0)
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (m, n) = br.readLine().split(" ").map { it.toInt() }
val graph = Array(n) { Array(m) { 0 } }
val q = ArrayDeque<Pair<Int, Int>>()
// ์๊ฑฐ ์ถ๊ฐ
var tomatoCount = 0
var maxDate = 1
repeat(n) {
val line = br.readLine().split(" ").map { it.toInt() }
line.forEachIndexed { index: Int, item: Int ->
graph[it][index] = item
if (item == 1) q.add(it to index)
if (item == 0) tomatoCount++ // ์๊ฑฐ ์ถ๊ฐ
}
}
while (q.isNotEmpty()) {
val now = q.removeFirst()
for (i in 0..3) {
val nextX = now.first + dx_1[i]
val nextY = now.second + dy_1[i]
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) {
continue
}
if (graph[nextX][nextY] != 0) {
continue
}
val date = graph[now.first][now.second] + 1
graph[nextX][nextY] = date
q.add(nextX to nextY)
tomatoCount-- // ์๊ฑฐ ์ถ๊ฐ
maxDate = date // ์๊ฑฐ ์ถ๊ฐ
}
}
if (tomatoCount > 0) println(-1) else println(maxDate - 1) // ์ด์ ๊ณ์ฐ ์ํด๋ ๋จ!
}
์ค์ ๋ก ์กฐ๊ธ ์ค์์ต๋๋ค..์ ์๋ฏธํ ์ฐจ์ด์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง
ํ ๋งํ ์ฌ๋ฐ์๋ค์!
if tomato[ny][nx] != -1 and visited[ny][nx] == 0: | ||
visited[ny][nx] = 1 | ||
tomato[ny][nx] = tomato[y][x] + 1 | ||
queue.append((ny, nx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ ์ง ์ ๋ณด๋ฅผ ํ ๋งํ ์ ๋ํ๋ค๋ฉด visited ์์ด ๊ฐ๋ฅํ ๊ฒ ๊ฐ๋ค์! ์ด์ฐจํผ 0์ด๋ฉด ํ์ํ์ง ์์ ํ ๋งํ ์ผํ ๋๊น์~
if tomato[ny][nx] != -1 and visited[ny][nx] == 0: | |
visited[ny][nx] = 1 | |
tomato[ny][nx] = tomato[y][x] + 1 | |
queue.append((ny, nx)) | |
if tomato[ny][nx] == 0: | |
tomato[ny][nx] = tomato[y][x] + 1 | |
queue.append((ny, nx)) |
๐ ๋ฌธ์ ๋งํฌ
ํ ๋งํ
โ๏ธ ์์๋ ์๊ฐ
20๋ถ
๐ง TMI
์ธํฐ๋ท ์ฐธ๊ณ X
๋ค๋ฅธ ์ฝ๋ ์ฐธ๊ณ X
์ํธ๋ง์ ํธ๋๊น ๊ธฐ๋ถ์ด ๋ฟ๋ฏํ๊ตฌ๋ง์
์ด๋์ ๋ ๊ธฐ๋ณธ์ ์ธ BFS๋ฌธ์ ๋ ์ด์ ํ ์ ์์๋ฏ..
โจ ์๋ ์ฝ๋
์ฒ์ ์๊ฐํ ๋ถ๋ถ์ผ๋ก๋
์ ์ฌ์ง๊ณผ ๊ฐ์ด ๋์ํ๊ฒ ์๊ฐํ์ต๋๋ค.
๋ฐฉ์์ผ๋ก ํ์์ต๋๋ค!!!!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๊ทธ๋ฆฌ๊ณ VSCode์์ ํ๋ฉด tempCodeRunnerFile์ด ํ๋ฒ์ฉ ์๊ฒจ์ ์๋ ์ค์ ์ ์ผ์ ์์๊ธฐ๊ฒ ํด์คฌ์ต๋๋ค!!!
์ฒซ PR์ธ๋ฐ ์๋ถํ๋๋ฆฝ๋๋ค!!๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ