-
Notifications
You must be signed in to change notification settings - Fork 0
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
15-g0rnn #60
base: main
Are you sure you want to change the base?
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.
์ด๋ฒ ๋ฌธ์ ๋ ๋ญ๊ฐ ์๊ฐํ๋๋ก ์ ์งํ๋ ๊ฒ ๊ฐ์์.
์ ๋ ๊ท ํธ๋ ์ฒ๋ผ ๋ฌธ์ ์ ํ์ ์๋ ๊ฒ์ด ์ค์ํ๋ค๊ณ ์๊ฐํ๊ณ ์์ต๋๋ค. ๊ทธ๋์ ๋ฌธ์ ์ ํ์ ๋จผ์ ์๊ฐํด๋ดค์ต๋๋ค.
๋ฐฑํธ๋ํน์ผ๋ก ๋ฏธ๋ก๋ฅผ ํํํ๋ฏ์ด ์งํํด์ผ ํ๋ค๋ ๊ฒ์ด ๋ ์ฌ๋์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ์์ง ์ฐ์ต์ด ๋ถ์กฑํด์ ๊ทธ๋ฐ์ง ์์ํ ์ค๋ฅ๋ฅผ ์ก๋๋ฐ ์๊ฐ์ ์จ์ 30๋ถ์ด์ ๊ฑธ๋ ธ์ต๋๋ค.
์ฝ๋๋ ๊ท ํธ๋๊ณผ ๋น์ทํฉ๋๋ค.
๊ทผ๋ฐ ์ด์ง ๋ค๋ฅธ ๋ถ๋ถ์ด ์๋๋ฐ ๊ทธ๊ฑด cnt
๋ฅผ ์ด๋ป๊ฒ ๊ตฌํ๋๊ฐ ์
๋๋ค.
์ ๋ dfs ๊ตฌํํจ์ ์์์ return์ ํ๋๋ก ํด๋ดค์ต๋๋ค.
(ํฌ๊ฒ ๋ค๋ฅผ๊ฑด ์๋ค์ฉ)
int solution(int x, int y) {
int maxCnt = 0;
for (int dir = 0; dir < 4; dir++) {
int x_ = x + offset[dir][1];
int y_ = y + offset[dir][0];
if (isMoveable(x_, y_)) {
visited[board[y_][x_] - 'A'] = true;
maxCnt = max(maxCnt, solution(x_, y_));
visited[board[y_][x_] - 'A'] = false;
}
}
return maxCnt + 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.
์ ๊ฐ ์ฌ๋ ธ๋ 17PR๊ณผ ๋น์ทํ์ง๋ง ์์ ๋ค๋ฅธ๋ฌธ์ ์์ต๋๋ค.
17-wnmsir๋ ๊ฒ์๊ฐ๋ฐ์ด๋ผ๋ ๊ตฌํ๋ฌธ์ ์ธ๋ฐ, ์ด๋ ๊ตฌํ๊ท์น์ด ์ ํด์ง๋๋ก ๊ตฌํ๋ง ํ๋ฉด ๋๋ ๋ฌธ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ชจ๋ ๊ฒฝ์ฐ์์๋ฅผ ๊ณ ๋ คํ ํ์ ์์ด ๋ฌธ์ ์์ ์ ์ํ ์กฐ๊ฑด๋ง ์ง์ผ๊ฐ๋ฉฐ ๊ทธ ์กฐ๊ฑด์ด ์ฐธ์ด๋ผ๋ฉด ์คํํด์ผํ ์ฝ๋๋ง ๊ตฌํํ๋ฉด ๋์ต๋๋ค.
ํ์ง๋ง ์ด๋ฌธ์ ๋ ๊ทธ๊ฒ๊ณผ๋ ๋ค๋ฆ ๋๋ค. ์กฐ๊ฑด๋๋ก ๊ฐ๋ค๊ฐ ๊ธธ์ด ์๋๋ผ๋ฉด ๊ฑฐ๊ธฐ์ ๊ฒ์์ ์ข ๋ฃํ๋๊ฒ์ด ์๋, ๋ค์ ๋์์ ์๋ก์ด ๊ธธ์ ์๋ํด๋ณด๊ณ ๊ฐ๋ ๋ชจ๋ ์นธ๋ค์ ์๋ฅผ ์ ๋ฐ์ดํธํด๊ฐ๋ฉฐ ๊ฒฐ๊ตญ ๋ชจ๋ ๊ธธ์ ์๋ํด๋ณด๊ณ ๊ฐ์ฅ ๋ง์ ์นธ์ ์ง๋ฌ์๋๊ฐ ๋ต์ด๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ฆ ๋ฐฑํธ๋ํน์ ๋ต์ด ํ์ํฉ๋๋ค.
4๋ฐฉํฅ์ ๋ชจ๋ ๊ฒ์ฌํ ์ ์๊ฒ dx, dy๋ฅผ ๊ตฌ์ฑํด์ค๋๋ค.
dfs๋ก ๋ฐฑํธ๋ํน์ ์งํํ๋๋ฐ, ans๋ฅผ ์ ์ญ๋ณ์๋ก ๋ค๊ณ ์ ๊ฐ ์ฌ๊ทํธ์ถ๋ง๋ค ์ต๋๋ก ์ด๋๊น์ง ์งํ๊ฐ๋ฅํ์ง ๋งค๋ฒ ์
๋ฐ์ดํธ ํด์ค๋๋ค.
๊ณ์ ์งํํ๋ค๊ฐ if๋ฌธ์ ๊ฑธ๋ ค ๊ฐ์ง๋ชปํ๋ ์ํฉ์ด ์ค๊ฒ๋๋ฉด ๋์ด์ ์ฌ๊ท๋ ๋ฐ์ํ์ง ์๊ณ ํด๋น์นธ์ dfs๋ฐ๋ก ์๋์ค์ ์ํด ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๊ฐ ์ทจ์๋๊ณ cnt๋ ๋์ด์ ์ฌ๋ผ๊ฐ์ง ์๊ฒ ๋ฉ๋๋ค. ๊ทธ๋ผ ์ด์ ์ ํธ์ถ๋ dfs๊ฐ ๊ณ์ํด์ ์งํํ๊ฒ๋๊ณ ๋จ์ for๋ฌธ์ด ์งํ๋๋ฉฐ ๊ฐ๋ฅํ ๋ชจ๋ ๋ฐฉํฅ์ ๊ฐ๋ณด๊ฒ ๋ฉ๋๋ค.
R, C = map(int, input()).split())
board = [list(input() for _ in range(R)]
# 4๋ฐฉํฅ: ์ค๋ฅธ์ชฝ, ์ผ์ชฝ, ์๋, ์
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
ans = 0
def dfs(x, y, cnt, visited):
global ans
ans = max(ans, cnt)
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < R and 0 <= ny < C and board[nx][ny] not in visited:
visited.add(board[nx][ny])
dfs(nx, ny, cnt + 1, visited)
visited.remove(board[nx][ny]) # ๋ฐฑํธ๋ํน
visited = set()
visited.add(board[0][0])
dfs(0, 0, 1, visited)
print(ans)
๐ ๋ฌธ์ ๋งํฌ
์ํ๋ฒณ
โ๏ธ ์์๋ ์๊ฐ
40m
โจ ์๋ ์ฝ๋
์ด๋ค ๋ฌธ์ ์ธ์ง ๊ฐ์ด ์ค์๋์? ์ด๋ฒ ๋ณต์ต์ ์งํํ๋ฉฐ ๋๋ ๊ฒ์ ๋ฌธ์ ์ ์ ํ์ ํ์ ํ๋๊ฒ ์ค์ํ๋ค๋ ๊ฒ์ ๋๊ผ์ต๋๋ค. ์ ๋ ์ด ๋ฌธ์ ๋ฅผ
graph
์ฒ๋ผ ๋ณด๊ณbacktracking
์ ์ฌ์ฉํ์ฌ ํ์์ต๋๋ค. ์๋ํ์ง ์์์ง๋ง bfs๋ก๋ ํ๋ฆด๊ฑฐ ๊ฐ์์backtracking
์ ๋ค์์ ๊ธฐ์ตํ๋ฉด ํ๊ธฐ ์ฌ์ธ๊ฒ๋๋ค.์ด์ ๋ฌธ์ ๋ค๊ณผ ์ด๋ฒ ๋ฌธ์ ์ ํด๋น ๋ก์ง์ด ์จ์ด์์ต๋๋ค. ์ด์ ๋ฌธ์ ์ ํ๊ฐ์ง ๋ค๋ฅธ ์ ์ ์ ๋ต์ ๊ธฐ๋กํ๋ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์๋ฐ ๊ทธ ๋ถ๋ถ์ ์๊ฐํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ