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-seongwon030 #11

Merged
merged 1 commit into from
Mar 23, 2024
Merged

3-seongwon030 #11

merged 1 commit into from
Mar 23, 2024

Conversation

seongwon030
Copy link
Collaborator

@seongwon030 seongwon030 commented Mar 19, 2024

πŸ”— 문제 링크

ν† λ§ˆν† 

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

3μ‹œκ°„

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

첫번째 풀이

## ν† λ§ˆν† 

from collections import deque

m,n,h = map(int,input().split())
tomato = []
for i in range(h):
  tomato.append([list(map(int,input().split())) for _ in range(n)])


queue = deque([]) # μ’Œν‘œ -> 리슀트
dx, dy, dz = [-1,1,0,0,0,0], [0,0,-1,1,0,0,], [0,0,0,0,-1,1] # 쒌,우,ν•˜,상,μ•„λž˜,μœ„ -> μ’Œν‘œ 이동
ans = 0 # μ΅œμ†Œ λ‚ μ§œ
cnt = 0

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

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

flag = False 

for i in range(h):
    for j in range(n): 
      for k in range(m): 
        if tomato[i][j][k] == 0: 
          flag = True
          break
        else:
          continue
      max_list = tomato[i][j]
      ans = max(ans, max(max_list))
      
if cnt == 0:
  print(0)
elif flag:
  print(-1)
else:
  print(ans-1)
  1. 3μ°¨μ›λ¦¬μŠ€νŠΈλ‘œ ν† λ§ˆν†  μœ„μΉ˜ λ°›κΈ°
  2. dx,dy,dz에 쒌,우,ν•˜,상,μ•„λž˜,μœ„ 의 μœ„μΉ˜λ₯Ό 각각 리슀트둜 μ €μž₯
  3. 반볡문 μˆœνšŒν•˜λ©° 1이 μžˆλ‹€λ©΄ 큐에 ν•΄λ‹Ή μœ„μΉ˜λ₯Ό μ €μž₯
  4. nx,ny,nzλ₯Ό λ‹€μŒ μœ„μΉ˜λ‘œ μ„€μ •ν•˜κ³  첫 번째 μ’Œν‘œλΆ€ν„° νμ—μ„œ κΊΌλ‚΄κΈ°
  5. μ’Œν‘œλ₯Ό λ²—μ–΄λ‚˜λ©΄ μ•ˆλ˜κ³  0인 κ²½μš°μ—λ§Œ κ·Έ λ‹€μŒ 칸으둜 이동
  6. ν•΄λ‹Ή μ’Œν‘œλ₯Ό λ‹€μ‹œ 큐에 λ„£κ³  cnt에 1을 더함
  7. flag둜 0이 μžˆλŠ” κ²½μš°μ— true둜 λ³€ν™˜
  8. 3μ°¨μ›λ¦¬μŠ€νŠΈ 쀑 제일 μ•ˆμ— μžˆλŠ” 1차원 리슀트의 μ΅œλŒ“κ°’μ„ 가져와 κΈ°μ‘΄ ans와 λΉ„κ΅ν•˜μ—¬ λ‚˜μ˜¨ μ΅œλŒ“κ°’μ„ 계산
  9. λ‹€ μ™„λ£Œν•˜μ—¬ cntκ°€ 0이면 ν† λ§ˆν† κ°€ 읡은 μƒνƒœμ΄λ―€λ‘œ 0좜λ ₯
  10. flagκ°€ Trueλ©΄ μ•ˆ 읡은 ν† λ§ˆν† κ°€ μžˆμœΌλ―€λ‘œ -1좜λ ₯
  11. κ·Έ μ™Έμ—” ν† λ§ˆν† κ°€ λ‹€ 읡은 μƒνƒœμ΄λ―€λ‘œ 좜발점이 1인 점을 μƒκ°ν•˜μ—¬ μ΅œμ†ŒμΌμˆ˜μΈ ans-1좜λ ₯

문제점

λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ°œμƒ !!

λ‘λ²ˆμ§Έ 풀이

if cnt == 0:
  if flag:
    print(-1)
  else:
    print(0)
elif flag:
  print(-1)
else:
  print(ans-1)

ν† λ§ˆν† κ°€ λͺ¨λ‘ 0인 μƒνƒœ 즉, 읡지 μ•Šμ€ μƒνƒœκ°€ μ‘΄μž¬ν•œλ‹€λŠ” 것을 μ˜ˆμ™Έλ‘œ 두지 μ•Šμ•˜λ‹€.

  1. cntκ°€ 0인경우 두 가지 κ°€λŠ₯성이 μ‘΄μž¬ν•œλ‹€. λͺ¨λ‘ μ΅μ€μƒνƒœ or 읡은 게 ν•˜λ‚˜λ„ μ—†λŠ” μƒνƒœ
  2. λ§Œμ•½ cntκ°€ 0이면 flagκ°€ True일 λ•Œ -1, μ•„λ‹ˆλ©΄ 0 좜λ ₯
  3. cntκ°€ 0이 μ•„λ‹ˆλΌλ©΄ flagκ°€ True일 λ•Œ -1 좜λ ₯
  4. κ·Έ μ™Έμ—” μ΅œμ†Œ 일수λ₯Ό 좜λ ₯

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

κΈ°μ‘΄ ν† λ§ˆν† μ—μ„œ ν•œ 차원이 λŠ˜μ–΄λ‚œ 것 μ™Έμ—λŠ” 차이가 μ—†μ—ˆλ„€μš”. 근데 λ„ˆλ¬΄ λΉ„νš¨μœ¨μ μœΌλ‘œ ν‘Ό 것 κ°™μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ ν•œ 번 ν’€μ–΄λ΄μ•Όκ² λ„€μš”.

@tgyuuAn
Copy link
Member

tgyuuAn commented Mar 19, 2024

μ½”λ“œ λΈ”λŸ­μ— python을 μ“°μ‹ λ‹€λ©΄ ```뒀에 python을,

c++을 μ“°μ‹ λ‹€λ©΄ ```뒀에 cppλ₯Ό λ„£μœΌλ©΄ μ½”λ“œ ν•˜μ΄λΌμ΄νŒ…μ΄ λ“€μ–΄κ°‘λ‹ˆλ‹€!

java, kotlin λ“±λ“± λ‹€ λ˜μ–΄μš”!

@seongwon030
Copy link
Collaborator Author

였였 λ§ˆν¬λ‹€μš΄μ— μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ λͺ°λžμŠ΅λ‹ˆλ‹€ κ°μ‚¬ν•©λ‹ˆλ‹Ή λ‹€μŒμ—λŠ” ν•˜μ΄λΌμ΄νŒ… μ μš©ν•΄μ„œ μ¨λ³΄κ² μŠ΅λ‹ˆλ‹€!!

@tgyuuAn
Copy link
Member

tgyuuAn commented Mar 19, 2024

였였 λ§ˆν¬λ‹€μš΄μ— μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ λͺ°λžμŠ΅λ‹ˆλ‹€ κ°μ‚¬ν•©λ‹ˆλ‹Ή λ‹€μŒμ—λŠ” ν•˜μ΄λΌμ΄νŒ… μ μš©ν•΄μ„œ μ¨λ³΄κ² μŠ΅λ‹ˆλ‹€!!

μ˜¬λ €μ ΈμžˆλŠ” PR도 μˆ˜μ •ν•˜κΈ°λ‘œ λ°”κΏ€ 수 μžˆμ–΄μš”.

μ§€κΈˆ ν•΄!!!!!!!!!!!

@seongwon030
Copy link
Collaborator Author

였였 λ§ˆν¬λ‹€μš΄μ— μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ λͺ°λžμŠ΅λ‹ˆλ‹€ κ°μ‚¬ν•©λ‹ˆλ‹Ή λ‹€μŒμ—λŠ” ν•˜μ΄λΌμ΄νŒ… μ μš©ν•΄μ„œ μ¨λ³΄κ² μŠ΅λ‹ˆλ‹€!!

μ˜¬λ €μ ΈμžˆλŠ” PR도 μˆ˜μ •ν•˜κΈ°λ‘œ λ°”κΏ€ 수 μžˆμ–΄μš”.

μ§€κΈˆ ν•΄!!!!!!!!!!!

λ°”λ‘œ μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹·!!

@InSange
Copy link
Collaborator

InSange commented Mar 20, 2024

였였 λ§ˆν¬λ‹€μš΄μ— μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ λͺ°λžμŠ΅λ‹ˆλ‹€ κ°μ‚¬ν•©λ‹ˆλ‹Ή λ‹€μŒμ—λŠ” ν•˜μ΄λΌμ΄νŒ… μ μš©ν•΄μ„œ μ¨λ³΄κ² μŠ΅λ‹ˆλ‹€!!

μ˜¬λ €μ ΈμžˆλŠ” PR도 μˆ˜μ •ν•˜κΈ°λ‘œ λ°”κΏ€ 수 μžˆμ–΄μš”.

μ§€κΈˆ ν•΄!!!!!!!!!!!

γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹

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.

자 ν•˜μ΄νΌ ν† λ§ˆν† λ₯Ό μœ„ν•΄ λ‹¬λ €κ°€λ΄…μ‹œλ‹€~!

# cnt 0이면
# λͺ¨λ‘ μ΅μ–΄μžˆκ±°λ‚˜ -> flagλŠ” False
# μ΅μ€κ²Œ μ—†κ±°λ‚˜ (무쑰건 0이 ν•˜λ‚˜λŠ” 쑴재) -> flagλŠ” True
if cnt == 0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ €λŠ” λ§ˆμ§€λ§‰μ—

if (allCheck && day > 0)
		cout << day - 1;
	else if (allCheck && day == 0)
		cout << 0;
	else
		cout << -1;

μ΄λŸ°μ‹μœΌλ‘œ λ™μ‹œμ— λΉ„κ΅ν•΄μ„œ 길이λ₯Ό μ€„μ—¬λ³΄μ•˜μŠ΅λ‹ˆλ‹Ή

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.

μ’Œν‘œμ— μ΅œλŒ€κ°’μ„ μ €μž₯ν•˜κ³  λ§ˆμ§€λ§‰μ— 탐색을 ν–ˆκ΅°μš”! μ €λž‘ λ‹€λ₯Έ 접근이라 μ‹ κΈ°ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜λ„ 탐색을 2개의 큐λ₯Ό ν†΅ν•΄μ„œ λ‚ μ§œλ₯Ό λ°”κΏ”κ°€λ©° νƒμƒ‰ν•˜λ˜κ°€ 탐색 λ…Έλ“œλ₯Ό {탐색 μ—¬λΆ€, 탐색 λ‚ μ§œ} 이런 μ‹μœΌλ‘œ λ§Œλ“€μ–΄ λ§ˆμ§€λ§‰ 배열을 μˆœνšŒν•˜λ©° λ‚ μ§œ ν™•μΈν•˜λŠ” μ½”λ“œλ₯Ό 없앨 수 μžˆμ„ 것 κ°™μ•„μš”!

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.

μ•ˆμ΅μ€ ν† λ§ˆν† κ°€ μžˆλ‹€λŠ”κ±Έ flagλ₯Ό 톡해 νŒλ³„ν•΄μ£Όμ—ˆκ΅°μš” μš”μ¦˜ ν† λ§ˆν† κ°€ λŒ€μ„ΈμΈκ²ƒ κ°™μœΌλ‹ˆ
저도 ν’€λŸ¬κ°€λ³΄κ² μŠ΅λ‹ˆλ‹€

@seongwon030 seongwon030 merged commit f6e87d7 into main Mar 23, 2024
1 check passed
@seongwon030 seongwon030 deleted the 3-seongwon030 branch March 23, 2024 04:38
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