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

2-seongwon030 #7

Merged
merged 4 commits into from
Mar 19, 2024
Merged

2-seongwon030 #7

merged 4 commits into from
Mar 19, 2024

Conversation

seongwon030
Copy link
Collaborator

πŸ”— 문제 링크

ν† λ§ˆν† 

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1일

✨ μˆ˜λ„ μ½”λ“œ

  1. ν† λ§ˆν† κ°€ 1인 μ’Œν‘œλΆ€ν„° λ™μ‹œμ— μ‹œμž‘ν•œλ‹€.
  2. μ’Œν‘œμ˜ 크기λ₯Ό λ„˜κΈ°λ©΄ μ•ˆλœλ‹€.
  3. 큐에 μ’Œν‘œλ₯Ό λ„£μœΌλ©΄μ„œ λ‹€μŒμœΌλ‘œ 계속 λ„˜μ–΄κ°€κΈ°
  4. 큐가 빌 λ•ŒκΉŒμ§€ 반볡

첫번째 풀이


from collections import deque

m,n = map(int,input().split())
tomato = [list(map(int,input().split())) for _ in range(n)]
queue = deque([]) # μ’Œν‘œ -> 리슀트

dx, dy = [-1,1,0,0], [0,0,-1,1] # 쒌,우,ν•˜,상 -> μ’Œν‘œ 이동 
ans = 0 # μ΅œμ†Œ λ‚ μ§œ 

for i in range(n):
  for j in range(m):
    if tomato[i][j] == 1: # 1인 경우 ν† λ§ˆν† κ°€ μžˆμœΌλ―€λ‘œ 큐에 append
      queue.append([i,j])

def bfs():
  while queue:
    x,y = queue.popleft() # 첫 번째 μ’Œν‘œ 
    for i in range(4):
      nx,ny = dx[i] + x, dy[i] + y
      # μ’Œν‘œ 크기λ₯Ό λ„˜μ–΄κ°€μ„  μ•ˆλ˜κ³  값이 0이어야 
      if 0 <= nx < n and 0 <= ny < m and tomato[nx][ny] == 0:
        tomato[nx][ny] = tomato[x][y] + 1 # κ·Έ λ‹€μŒ 칸으둜 이동
        queue.append([nx,ny])

bfs()
for i in tomato:
  for j in i:
    if j == 0:
      print(-1)
      exit(0)
    ans = max(ans, max(i))
print(ans-1)
  1. ν† λ§ˆν† μ˜ μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” μœ„μΉ˜λ₯Ό 2μ°¨μ›λ°°μ—΄λ‘œ μ €μž₯
  2. μ§„ν–‰μƒνƒœλ₯Ό μ•Œλ €μ£ΌλŠ” 덱을 리슀트둜 μ„€μ •
  3. μ’Œν‘œλ₯Ό μƒν•˜μ’Œμš°λ‘œ λ‚˜λˆˆ 리슀트 μ„€μ •
  4. 값이 1인경우 큐에 append
  5. μ’Œν‘œλ₯Ό κΊΌλ‚΄μ–΄ x,yμ’Œν‘œλ₯Ό nx,ny만큼 κ³„μ‚°ν•œ κ²°κ³Όκ°€ μ’Œν‘œν¬κΈ°λ₯Ό λ„˜μ–΄κ°€μ„  μ•ˆλ˜λ©° 0μΌλ•Œλ§Œ ν† λ§ˆν† μ˜ μƒνƒœκ°€ 0μ—μ„œ 1둜 변함.
  6. ν•΄λ‹Ή μ’Œν‘œλ₯Ό 큐에 λ‹€μ‹œ append
  7. 5와 6을 큐가 λΉ„μ–΄μžˆμ„ λ•ŒκΉŒμ§€ 반볡
  8. ν† λ§ˆν†  μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” 리슀트λ₯Ό 반볡문으둜 λŒλ©΄μ„œ 0인 값이 있으면 -1을 좜λ ₯ν•˜κ³  exit.
  9. μ—†μœΌλ©΄ tomato λ₯Ό μˆœνšŒν•˜λ©° 각 칸의 μ΅œλŒ“κ°’μ„ μ°Ύκ³  (max(i)λŠ” ν•œ ν–‰μ˜ μ΅œλŒ“κ°’), λͺ¨λ“  ν–‰μ˜ μ΅œλŒ“κ°’ 쀑 κ°€μž₯ 큰 값을 ans에 μ €μž₯.
  10. 읡은 ν† λ§ˆν† μ˜ 값은 1둜 μ‹œμž‘ν•˜λ―€λ‘œ κ±Έλ¦¬λŠ” μΌμˆ˜λŠ” 1을 λΊ€ ans-1

문제점

=> -1일 λ•Œ 계속 좜λ ₯ν•˜λŠ” 문제

λ‘λ²ˆμ§Έ 풀이

flag = False # μ•ˆ 읡은 ν† λ§ˆν†  μžˆλŠ”μ§€

for i in tomato:
  if 0 in i:
    flag = True
    break

if flag: # μ•ˆ 읡은 ν† λ§ˆν† κ°€ μ‘΄μž¬ν•˜λ―€λ‘œ -1좜λ ₯
  print(-1)
else:
  for i in tomato:
    ans = max(ans,max(i))
  print(ans-1)
  1. μ•ˆ 읡은 ν† λ§ˆν† μ˜ 유무λ₯Ό μ•ŒκΈ° μœ„ν•΄ flag 에 λΆˆλ¦¬μ–Έ 값인 false둜 μ €μž₯
  2. tomato 리슀트λ₯Ό μˆœνšŒν•˜λ©° 0이 μžˆλŠ” κ²½μš°μ—” flagλ₯Ό True둜 λ°”κΎΈκ³  break
  3. flagκ°€ Trueλ©΄ μ•ˆ 읡은 ν† λ§ˆν† κ°€ μžˆμœΌλ―€λ‘œ -1을 좜λ ₯
  4. μ•„λ‹ˆλ©΄ 첫 번째 ν’€μ΄μ—μ„œμ™€ 같이 ans 좜λ ₯

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

x와 yμ’Œν‘œλ₯Ό λͺ¨λ‘ μ‚¬μš©ν•΄μ„œ μ΄λ™ν•˜λŠ” κ²½μš°λŠ” 처음 μ ‘ν•΄λ΄€κ³  2차원 리슀트λ₯Ό μ΄μš©ν•΄ ν’€ 수 μžˆλŠ”κ²Œ μ°Έμ‹ ν–ˆλ‹€.

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.

저도 ν† λ§ˆν†  μ°Έ μ’‹μ•„ν•˜λŠ”λ°μš”?
λ§Œμ•½ μ’€ 더 μ‹œκ°„λ³΅μž‘λ„λ₯Ό μ‹ κ²½μ“°κ³  μ‹Άλ‹€λ©΄ μž…λ ₯ 받을 λ•Œ 덜 읡은 ν† λ§ˆν† 0 개수λ₯Ό cnt 둜 λ”°λ‘œ λΆ„λ₯˜ν•˜μ—¬ 읡을 λ•Œ κ°μ†Œν•΄μ£ΌλŠ” μ‹μœΌλ‘œν•˜μ—¬ qκ°€ λΉ„μ—ˆμ„ λ•Œ cnt 값에 따라 κ²°κ³Όλ₯Ό λ°˜ν™˜ν•΄μ€„ 수 μžˆμ„ 것 κ°™μŠ΅λ‹ˆλ‹€!
BFS λ¬Έμ œλ“€μ€ 쑰금만 λ‚œμ΄λ„κ°€ μ˜¬λΌκ°€κ²Œ 되면 μ˜ˆμ™Έ 처리λ₯Ό μ‹ μ€‘ν•˜κ²Œ 잘 μ°Ύμ•„μ•Όν•˜λŠ”λ° κΉ”λ”ν•˜κ²Œ 잘 μ°Ύμ•„λ‚΄μ„œ 보기 μ’‹μ•˜λ˜ 것 κ°™μŠ΅λ‹ˆλ‹€!
ν† λ§ˆν† λŠ” 3차원도 μ‘΄μž¬ν•œλ‹€λŠ” κ±° μ•„μ‹œλ‚˜μš”? λ§›μžˆλŠ” ν† λ§ˆν† ! 문제 μΆ”μ²œ λ“œλ¦½λ‹ˆλ‹€!

@seongwon030
Copy link
Collaborator Author

읡은 ν† λ§ˆν† μ˜ μœ„μΉ˜λ§Œ κ³ λ €ν•΄μ„œ μ‹œκ°„λ³΅μž‘λ„λ₯Ό κ³ λ €ν•˜μ§€λŠ” λͺ»ν–ˆλ„€μš”.. κ°μ‚¬ν•©λ‹ˆλ‹€!! 3차원도 ν•œ 번 ν’€λŸ¬ κ°€λ³΄κ² μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

@yuyu0830
Copy link
Collaborator

11차원 ν† λ§ˆν† λ„ μžˆλ‹€λŠ” 것 μ•„μ‹œλ‚˜μš”? ν•˜μ΄νΌ ν† λ§ˆν† 
3차원 ν† λ§ˆν†  원리λ₯Ό μ΄ν•΄ν•˜μ‹œλ©΄ 금방 ν‘Έμ‹€ 수 μžˆμœΌμ‹€ κ±°μ˜ˆμš” γ…Žγ…Ž

λ‹€λ₯Έ μ½”λ“œ 뢀뢄도 μ΄ν•΄ν•˜κΈ° μ‰¬μ›Œμ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€.
μ•„μ‹€ μˆ˜λ„ μžˆμ§€λ§Œ νŒŒμ΄μ¬μ€ for-else λΌλŠ” μž¬λ°ŒλŠ” 문법이 μžˆλ”λΌκ΅¬μš”.
잘 μ‚¬μš©ν•˜λ©΄ flag λ³€μˆ˜ 없이도 지 수 μžˆμ„ 것 κ°™μ•„μš”!

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

11차원 ν† λ§ˆν† λ„ μžˆλ‹€λŠ” 것 μ•„μ‹œλ‚˜μš”? ν•˜μ΄νΌ ν† λ§ˆν†  3차원 ν† λ§ˆν†  원리λ₯Ό μ΄ν•΄ν•˜μ‹œλ©΄ 금방 ν‘Έμ‹€ 수 μžˆμœΌμ‹€ κ±°μ˜ˆμš” γ…Žγ…Ž

λ‹€λ₯Έ μ½”λ“œ 뢀뢄도 μ΄ν•΄ν•˜κΈ° μ‰¬μ›Œμ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€. μ•„μ‹€ μˆ˜λ„ μžˆμ§€λ§Œ νŒŒμ΄μ¬μ€ for-else λΌλŠ” μž¬λ°ŒλŠ” 문법이 μžˆλ”λΌκ΅¬μš”. 잘 μ‚¬μš©ν•˜λ©΄ flag λ³€μˆ˜ 없이도 지 수 μžˆμ„ 것 κ°™μ•„μš”!

λ‹€μŒμ— flag없이도 짜 λ΄μ•Όκ² λ„€μš”! 문제 μΆ”μ²œ κ°μ‚¬ν•©λ‹ˆλ‹€ γ…Žγ…Ž

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

저도 μ–Όλ§ˆμ „μ— ν’€μ—ˆλ˜ 문제인데 bfsλ₯Ό ν•΄μ£Όκ³ λ‚˜μ„œ μ•ˆμ΅μ€ ν† λ§ˆν† κ°€ μžˆλŠ”μ§€ ν™•μΈν•˜κΈ° μœ„ν•΄
λ‹€μ‹œ νƒμƒ‰ν•˜λŠ” κ³Όμ •μ—μ„œ μ’€ νž˜λ“€μ—ˆλ˜ κ²½ν—˜μ΄μžˆλ„€μš”.. κΉ”λ”ν•œ μ½”λ“œ 잘 보고 κ°‘λ‹ˆλ‹€ !

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.

5 participants