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

23-seongwon030 #85

Merged
merged 1 commit into from
Aug 10, 2024
Merged

23-seongwon030 #85

merged 1 commit into from
Aug 10, 2024

Conversation

seongwon030
Copy link
Collaborator

@seongwon030 seongwon030 commented Aug 5, 2024

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

๊ฒŒ์ž„

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

2์‹œ๊ฐ„

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

์ด ๋ฌธ์ œ๋Š” dfs๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์›€์ง์ด๋Š” ํšŸ์ˆ˜๋ฅผ ๊ณ„์† ๊ฐฑ์‹ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” dp๋„ ์‚ฌ์šฉํ•œ๋‹ค. ์ผ๋‹จ ํ•„์š”ํ•œ ๋ฆฌ์ŠคํŠธ๋“ค์„ ์ •๋ฆฌํ–ˆ๋‹ค.

์ƒํ•˜์ขŒ์šฐ ์ด๋™

dx = [-1,1,0,0]
dy = [0,0,-1,1]

๋ฐฉ๋ฌธ์—ฌ๋ถ€ ์ฒดํฌ

visited = [[False]*m for _ in range(n)] 

์›€์ง์ธ ํšŸ์ˆ˜ ๊ฐฑ์‹ 

dp = [[0]*m for _ in range(n)]

์ด์   dfs ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•˜๋ฉฐ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‚˜๋ˆˆ๋‹ค. ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์กฐ๊ฑด1 : ์ด๋™ ํ›„์˜ ์ขŒํ‘œ๊ฐ€ ๋ฒ”์œ„ ๋‚ด์— ์žˆ์–ด์•ผ ํ•œ๋‹ค.
์กฐ๊ฑด2 : ์ด๋™ ํ›„์˜ ์ขŒํ‘œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด H๊ฐ€ ์•„๋‹ˆ์–ด์•ผ ํ•œ๋‹ค.
์กฐ๊ฑด3 : ์ด์ „๋ณด๋‹ค ์›€์ง์ธ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์•„์•ผ ํ•œ๋‹ค.

if 0 <= nx < n and 0 <= ny < m and k[nx][ny] != 'H' and cnt+1 > dp[nx][ny]:

๋˜ํ•œ ์ด๋™ ํ›„์˜ ์ขŒํ‘œ๊ฐ€ ๋ฐฉ๋ฌธํ•œ ์  ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด์•ผ ํ•œ๋‹ค.

๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ
-1์„ ์ถœ๋ ฅํ•˜๊ณ  dfs๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
dp์—์„œ ์ด๋™ํ›„์˜ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋‹ค์‹œ ํ•ด๋‹น ์ขŒํ‘œ์—์„œ dfs๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

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

dp์™€ dfs๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

๋”ฑ ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ๊ทธ๋Œ€๋กœ๊ตฐ์š”. ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ๋ฌดํ•œ์œผ๋กœ ๋Œ ๊ฑฐ๋‹ˆ๊นŒ ๋ฐ”๋กœ ํƒˆ์ถœ. ๊ทธ ์™ธ์˜ ๊ฒƒ๋“ค์€ dp๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋  ๋•Œ๋งŒ ํƒ์ƒ‰ํ•˜๋Š” ์‹. ๋‹ค์Œ์— c++๋กœ ํ•œ๋ฒˆ ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

๋‹ค๋ฅธ๊ฑด ์ดํ•ด๊ฐ€ ๊ฐ€๋Š”๋ฐ dp ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋„ค์š”.. ๊ทธ๋ƒฅ dfs๋กœ ๋ฌดํ•œ ๋ฐฉ๋ฌธ ํ•ด๋ณด๋ฉด ๋˜์ง€ ์•Š๋‚˜์š”?

@InSange
Copy link
Collaborator

InSange commented Aug 8, 2024

p ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋„ค์š”.. ๊ทธ๋ƒฅ dfs๋กœ ๋ฌดํ•œ ๋ฐฉ๋ฌธ ํ•ด๋ณด๋ฉด ๋˜์ง€ ์•Š๋‚˜์š”?

์•„๋งˆ ๋ถˆํ•„์š”ํ•œ ๋ฐฉ๋ฌธ์ž์ฒด๋ฅผ ์—†์• ์ฃผ๊ธฐ ์œ„ํ•œ dpํ˜•์‹์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋ฌดํ•œ๋Œ€๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ํ•œ ๋ฒˆ๋งŒ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋˜์ง€๋งŒ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์„ ํŒ๋ณ„ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ํ•ด๋‹น ์œ„์น˜์—์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ˆ˜์š”๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ๋Œ๋ฆฌ๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์งง๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์–ด์„œ ๊ทธ๋ ‡์ง€ ์•Š์„๊นŒ์š”? ํ•ด๋ณด์ง€๋Š” ์•Š์•˜์ง€๋งŒ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ค‘์— ์‹œ๊ฐ„ ์ดˆ๊ณผ ๊ฐ€๋Šฅ์„ฑ๋„ ๊ณ ๋ คํ•ด๋ด์•ผํ•˜์ง€ ์•Š์„๊นŒ์šฉ

@yuyu0830
Copy link
Collaborator

yuyu0830 commented Aug 8, 2024

p ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋„ค์š”.. ๊ทธ๋ƒฅ dfs๋กœ ๋ฌดํ•œ ๋ฐฉ๋ฌธ ํ•ด๋ณด๋ฉด ๋˜์ง€ ์•Š๋‚˜์š”?

์•„๋งˆ ๋ถˆํ•„์š”ํ•œ ๋ฐฉ๋ฌธ์ž์ฒด๋ฅผ ์—†์• ์ฃผ๊ธฐ ์œ„ํ•œ dpํ˜•์‹์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋ฌดํ•œ๋Œ€๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ํ•œ ๋ฒˆ๋งŒ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋˜์ง€๋งŒ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์„ ํŒ๋ณ„ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ํ•ด๋‹น ์œ„์น˜์—์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ˆ˜์š”๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ๋Œ๋ฆฌ๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์งง๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์–ด์„œ ๊ทธ๋ ‡์ง€ ์•Š์„๊นŒ์š”? ํ•ด๋ณด์ง€๋Š” ์•Š์•˜์ง€๋งŒ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ค‘์— ์‹œ๊ฐ„ ์ดˆ๊ณผ ๊ฐ€๋Šฅ์„ฑ๋„ ๊ณ ๋ คํ•ด๋ด์•ผํ•˜์ง€ ์•Š์„๊นŒ์šฉ

์•„ ํƒ์ƒ‰์„ ์ด์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ ๊ทธ๋ ‡๊ฒŒ ๋  ์ˆ˜๊ฐ€ ์žˆ๊ฒ ๋„ค์š”! ๋ฌธ์ œ ์ฃผ์ธ์„ ๋Œ€์‹ ํ•œ ์„ค๋ช… ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

@yuyu0830 yuyu0830 closed this Aug 8, 2024
@yuyu0830 yuyu0830 reopened this Aug 8, 2024
Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

์Šน์ธ

@seongwon030
Copy link
Collaborator Author

๋‹ค๋ฅธ๊ฑด ์ดํ•ด๊ฐ€ ๊ฐ€๋Š”๋ฐ dp ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋„ค์š”.. ๊ทธ๋ƒฅ dfs๋กœ ๋ฌดํ•œ ๋ฐฉ๋ฌธ ํ•ด๋ณด๋ฉด ๋˜์ง€ ์•Š๋‚˜์š”?

๋‹จ์ˆœ DFS๋กœ ํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์•˜์–ด์š”. ๋ชจ๋“  ๊ณณ์„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•„๋„ ๊ฐ ์นธ๋งˆ๋‹ค ์›€์ง์ธ ์ตœ๋Œ€ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋ฉด ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š๋‚˜ ํ•ด์„œ dp ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

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.

3 participants