Skip to content
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

3-kangrae-jo #10

Merged
merged 8 commits into from
Nov 1, 2024
Merged

3-kangrae-jo #10

merged 8 commits into from
Nov 1, 2024

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Oct 5, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/7576

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50m

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ

์ด ๋ฌธ์ œ๋ฅผ ์ฝ์œผ๋ฉด์„œ ๋‚ฏ์„  ๋Š๋‚Œ์€ ๋“ค์ง€์•Š์•˜๋‹ค.
๋ฌธ์ œ๋ฅผ ์ฝ์œผ๋ฉด ์ฝ์„ ์ˆ˜๋ก 'ํ’€์ด ์ž๋ผ๋Š” ๋ฌธ์ œ', '๋ฏธ๋กœ์ฐพ๊ธฐ ๋ฌธ์ œ' ๊ฐ€ ๋– ์˜ฌ๋ž๋‹ค.

๋ฌธ์ œ๋ฅผ ๋‹ค ์ฝ๊ณ  ์ด ๋ฌธ์ œ๋Š” ํ™•์‹ ์˜ BFS๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ํ™•์‹ ํ–ˆ๋‹ค.
๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์„ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•˜์ž๋ฉด
ํ˜„์žฌ ์ƒํƒœ์—์„œ ๋ป—์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ๋“ค๋กœ ๋ป—์–ด๋‚˜๊ฐ€๋ณด๋Š” ๊ฒƒ์ด๋‹ค.
๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์€ ํ•œ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ํŒŒ๊ณ ๋“ค์ง€๋งŒ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์€ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค.
๊ทธ๋ž˜์„œ ํ† ๋งˆํ† ์˜ ์ƒํƒœ๋ฅผ ํ•˜๋ฃจํ•˜๋ฃจ์— ๋”ฐ๋ผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ์— ์ข‹์€ ๋ฐฉ์‹์ด๋ผ๊ณ  ์ƒ๊ฐ๋œ๋‹ค.
image

์ˆ˜๋„ ์ฝ”๋“œ

  1. ์ž…๋ ฅ ๋ฐ›์œผ๋ฉฐ queue์— ์ด๋ฏธ ์ต์–ด์žˆ๋Š” ํ† ๋งˆํ†  push
  2. ๋‚ ์งœ ๊ธฐ์ค€์œผ๋กœ ํ˜„์žฌ queue์— ๋“ค์–ด์žˆ๋Š” ํ† ๋งˆํ† ๋“ค๋งŒ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉฐ ๋ฐ˜๋ณต
    2-1. ๊บผ๋‚ธ ํ† ๋งˆํ† ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ํ† ๋งˆํ†  ์ƒํƒœ ํ™•์ธ
    2-2. ํ† ๋งˆํ†  ์ƒํƒœ๊ฐ€ 0์ด๋ผ๋ฉด 1๋กœ ์„ค์ • ํ›„ queue์— ๋„ฃ๊ธฐ
  3. 2๋ฒˆ๊ณผ์ • ๋ฐ˜๋ณตํ•˜๋ฉฐ queue๊ฐ€ empty์ผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
int tomato() {
    int level = -1;

    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size; i++) {
            int y = q.front().first;
            int x = q.front().second;
            q.pop();

            for (int dir = 0; dir < 4; dir++) move(x, y, dir);
        }
        level++;
    }

    return level;
}

์ถ”๊ฐ€ ์„ค๋ช…

๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

์ฒซ์งธ๋กœ,
์‰ฌ์šด BFS๋ฌธ์ œ๋Š” ์ถœ๋ฐœ์ ์ด ํ•˜๋‚˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฌธ์ œ๋Š” ์ถœ๋ฐœ์ ์˜ ๊ฐœ์ˆ˜์™€ ์œ„์น˜๊ฐ€ ์ •ํ•ด์ ธ์žˆ์ง€ ์•Š๋‹ค.
์ต์€ ํ† ๋งˆํ† ๋Š” ์–ด๋””์—๋‚˜ ์žˆ์„ ์ˆ˜ ๋„ ์žˆ๊ณ , ์• ์ดˆ์— ์—†์„ ์ˆ˜ ๋„์žˆ๋‹ค.
์ด๋Ÿฐ ์กฐ๊ฑด์„ ์ž˜ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ด๋ฒˆ ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

๋‘˜์งธ๋กœ,
ํ† ๋งˆํ† ๋Š” ํ•˜๋ฃจ ๋‹จ์œ„๋กœ ๋ณ€ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (์ฒซ๋ฒˆ์งธ ํ•ต์‹ฌ๊ณผ ์—ฐ๊ด€๋œ ๊ฒƒ์ž„)
์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ด๋ƒ๋ฉด ๊ทธ๋ƒฅ while๋ฌธ ์•ˆ์— level๋ฅผ ++ ์‹œํ‚ค๋Š” ๋“ฑ์˜ ๋‹จ์ˆœํ•œ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.
์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” '์ˆ˜๋„ ์ฝ”๋“œ 2๋ฒˆ'ํ•ญ๋ชฉ์„ ์ž˜ ์ดํ•ดํ•ด์•ผํ•œ๋‹ค.
(์ œ๊ฐ€ ์ข€ ๋” ์ž˜ ์„ค๋ช…ํ•ด์•ผํ•˜๋Š”๋ฐ ๊ธ€๋กœ ํ‘œํ˜„ํ•˜๋Š”๊ฒŒ ์‚ด์ง ์–ด๋ ต๋„ค์š”. ์ฝ”๋“œ ๊ฐ™์ด ๋ณด์‹œ๋Š”๊ฑฐ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.)

ํ˜„์žฌ ๋‚ ์งœ์—๋Š” ๋”ฑ ์ „ ๋‚ ์— ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค.
๊ทธ ํ† ๋งˆํ† ๋“ค์— ๋Œ€ํ•ด BFS๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  level์„ ++์‹œ์ผœ์•ผ ์ œ๋Œ€๋กœ ๋‚ ์งœ๊ฐ€ ์„ธ์–ด์ง„๋‹ค.

์ถ”๊ฐ€์ ์ธ ์ •๋ณด๋กœ๋Š” ์˜ˆ์ „์— ๊ทผํ˜ธ๋‹˜์ด ํ‘ธ์…จ๋˜ ๋ฌธ์ œ์ฒ˜๋Ÿผ offset์„ ํ™œ์šฉํ•œ ์ขŒํ‘œ์ด๋™์„ ํ–ˆ๋‹ค๋Š” ๊ฒƒ ์ •๋„๊ฐ€ ์žˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ด ์ฝ”๋“œ๋ฅผ ์ž˜ ์ˆ˜์ •ํ•˜๋ฉด ๋ฏธ๋กœ์ฐพ๊ธฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ตœ๋‹จ๊ฒฝ๋กœ๋„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ €๋Š” ์ฃผ๋กœ mainํ•จ์ˆ˜์— ๋ณ€์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜์— ๋„˜๊ธฐ๋Š” ์Šคํƒ€์ผ์˜ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ž‘์„ฑํ–ˆ๋‹ค.

์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋ญ”๊ฐ€ vector์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ž๊พธ ์ ์œผ๋‹ˆ ์ฝ”๋“œ๊ฐ€ ๋”๋Ÿฌ์›Œ ์ง€๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.
(& ๋กœ ๋ฐ›์œผ๋ฉด ์Šคํƒ ๊ณต๊ฐ„ ๋ฌธ์ œ๋Š” ๊ฑฐ์˜ ์—†์„ ๊ฒƒ์ž„)

๊ทธ๋ž˜์„œ ์ „์—ญ๋ณ€์ˆ˜๋กœ vector๋ฅผ ๋บ๋Š”๋ฐ, ์ด์ „๊นŒ์ง€๋Š” .resize()๋ฅผ ๋งŽ์ด ํ™œ์šฉํ–ˆ๋‹ค.
๊ทผ๋ฐ 2์ฐจ์› ๋ฐฐ์—ด์ด๋‹ค๋ณด๋‹ˆ ๋ญ”๊ฐ€ ์–ด๋ ค์› ๊ณ  ์ฐพ์•„๋ณด๋‹ˆ ๊ทธ๋ƒฅ
box = vector<vector<int> >(N, vector<int>(M, 0));
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

์•ž์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•  ๊ธฐ์ˆ ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค!! ๊ฐ•๋ž˜๋‹˜์€ ๋‹จ๊ธฐ๊ฐ„์ด์ง€๋งŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ‘ธ๋Š” ์š”๋ น์ด ์ ์  ๋Š๋Š”๊ฒƒ ๊ฐ™๋„ค์š”!

์ €๋„ ํ•œ๋ฒˆ ํ’€์–ด๋ณด๋‹ˆ ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๋ฅผ ๋จผ์ € ๋ณด๊ณ ๋‚˜๋‹ˆ ํ๋ฅผ ์“ฐ๋Š”๊ฒŒ ๋จธ๋ฆฌ์— ๋ฐ•ํ˜€์„œ ๋– ๋‚˜์งˆ ์•Š๋”๋ผ๊ตฌ์š”.. ๊ทธ๋ž˜์„œ ์ €๋„ ํ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ ๋•๋ถ„์— bfs์—์„œ ์‹œ์ž‘์ง€์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ ์–ด๋–ป๊ฒŒ ํ•ด์ณ๋‚˜๊ฐ€๋ฉด ๋ ์ง€ ๊ฐ์„ ์žก์•˜์Šต๋‹ˆ๋‹ค!!!

int matureTomato() {
	int day = -1;
	while (!q.empty()) {
		int tomatoes = q.size();

		for (int _ = 0; _ < tomatoes; _++) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();

			for (int dir = 0; dir < 4; dir++) {
				int nx = x + offset[dir][0];
				int ny = y + offset[dir][1];

				if (canMove(nx, ny)) {
					box[nx][ny] = 1;
					q.push(make_pair(nx, ny));
				}
			}
		}
		day++;
	}
	return day;
}

(๋ฌธ์ œ ํ‘ธ๋Š”๋ฐ ๋งŽ์ด ๋„์™€์ค˜์„œ ๊ณ ๋งˆ์›Œ... ๋งˆ์ง€๋ง‰ ์ž…๋ ฅ์‹ค์ˆ˜๋Š” ์ง„์งœ ์–ด์ฒ˜๊ตฌ๋‹ˆ๊ฐ€ ์—†๋„ค..ใ…‹ใ…‹ใ…‹)

kangrae-jo/BFS/3-kangrae-jo.cpp Show resolved Hide resolved
kangrae-jo/BFS/3-kangrae-jo.cpp Show resolved Hide resolved
Copy link
Collaborator

@wnsmir wnsmir left a 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

def bfs():
while queue:
x, y = queue.popleft() # ํ˜„์žฌ ์œ„์น˜ (x, y)๋ฅผ ๊บผ๋ƒ„

    # ์ƒ
    if x - 1 >= 0 and box[x - 1][y] == 0:
        box[x - 1][y] = box[x][y] + 1
        queue.append((x - 1, y))

    # ํ•˜
    if x + 1 < N and box[x + 1][y] == 0:
        box[x + 1][y] = box[x][y] + 1
        queue.append((x + 1, y))

    # ์ขŒ
    if y - 1 >= 0 and box[x][y - 1] == 0:
        box[x][y - 1] = box[x][y] + 1
        queue.append((x, y - 1))

    # ์šฐ
    if y + 1 < M and box[x][y + 1] == 0:
        box[x][y + 1] = box[x][y] + 1
        queue.append((x, y + 1))

์ž…๋ ฅ ์ฒ˜๋ฆฌ

M, N = map(int, input().split()) # M: ๊ฐ€๋กœ ํฌ๊ธฐ, N: ์„ธ๋กœ ํฌ๊ธฐ
box = [list(map(int, input().split())) for _ in range(N)] # ํ† ๋งˆํ†  ์ƒ์ž์˜ ์ƒํƒœ ์ž…๋ ฅ

queue = deque()

์ต์€ ํ† ๋งˆํ† (1)์˜ ์œ„์น˜๋ฅผ ํ์— ๋„ฃ๊ธฐ

for i in range(N):
for j in range(M):
if box[i][j] == 1:
queue.append((i, j))

BFS ์‹คํ–‰

bfs()

๊ฒฐ๊ณผ ๊ณ„์‚ฐ

max_days = 0
for i in range(N):
for j in range(M):
if box[i][j] == 0: # ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด
print(-1)
exit()
max_days = max(max_days, box[i][j])

์ฒ˜์Œ ์‹œ์ž‘์ด 1์ด๋ฏ€๋กœ 1์„ ๋นผ์„œ ๋‚ ์งœ ๊ณ„์‚ฐ

print(max_days - 1 if max_days > 1 else 0)

์ €๋„ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๋ณด๋‹ˆ ์™œ ๊ฐ๊ฐ์˜ ํ† ๋งˆ๋„์— ๋Œ€ํ•ด ์ต์„์ˆ˜๋ก +1์„ ํ•ด์ค˜์•ผํ•˜๋Š”์ง€ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ BFS๋ฌธ์ œ์™€ ๋‹ฌ๋ผ ์ฐธ์‹ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ์ง๊ด€์ ์ด๊ฒŒ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์ฒดํฌํ•˜๋Š” ์ฝ”๋“œ๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

@kokeunho
Copy link
Collaborator

๊ฐ•๋ž˜๋‹˜ ๋•๋ถ„์— BFS์™€ DFS์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ๋‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ๊ฐ ์–ด๋Š ๋•Œ์— ๋” ์ ํ•ฉํ•œ์ง€ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
์ €๋„ ์˜คํ”„์…‹์„ ํ•œ๋ฒˆ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ๋‚˜๋‹ˆ ํ™œ์šฉ๋„๊ฐ€ ์ข‹์€ ๋ฐฉ์‹์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์•„์ง ํ ํ™œ์šฉ๋„ ์ž˜ ๋ชปํ•˜๋Š”๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด์„œ ์–ด๋Š์ •๋„ ๊ฐ์„ ์žก๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

@kangrae-jo kangrae-jo merged commit fbf0be5 into main Nov 1, 2024
@kangrae-jo kangrae-jo deleted the 3-kangrae-jo branch November 1, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants