๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€ ๊ธฐ๋ก

[๊ตฌํ˜„: python]๊ฐœ๋…๊ณผ ๋ฌธ์ œ ํ’€์ด ์ •๋ฆฌํ•˜๊ธฐ

by hyeonha 2024. 3. 28.

๋ชฉ์ฐจ

    ๊ตฌํ˜„

    ๊ตฌํ˜„์ด๋ž€ ๋จธ๋ฆฟ ์†์— ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ์Šค์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์ด๋‹ค.

     

    ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋Š” ํŒŒ์ด์ฌ์—์„œ ์‰ฌ์šด ํŽธ์ด๋‹ค. 

    ๊ตฌํ˜„ ์œ ํ˜• ๋ฌธ์ œ๋Š” ๋งŽ์€ ์—ฐ์Šต์ด ํ•„์š”ํ•˜๋‹ค 

     

    2์ฐจ์› ๊ณต๊ฐ„

    ํ–‰๋ ฌ : 2์ฐจ์› ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ

    ํŒŒ์ด์ฌ : 2์ฐจ์› ๋ฆฌ์ŠคํŠธ 

    ๋ฐฉํ–ฅ ๋ฒกํ„ฐ

    x : ํ–‰

    y : ์—ด


    ์˜ˆ์ œ 1 ์ƒํ•˜์ขŒ์šฐ  ํ’€์–ด๋ณด๊ธฐ

    1๏ธโƒฃ์ด๋™ํ•˜๋Š” ๋กœ์ง

    - ๋ฐฉํ–ฅ๋ฒกํ„ฐ๋ฅผ ์ด์šฉํ•ด๋ณด์ž

     

    L  : ํ–‰์€ ๊ทธ๋Œ€๋กœ ์—ด์€ -1 

    R : ํ–‰์€ ๊ทธ๋Œ€๋กœ ์—ด์€ +1

    U : ํ–‰์€ -1 ์—ด์€ ๊ทธ๋Œ€๋กœ 

    D : ํ–‰์€ +1 ์—ด์„ ๊ทธ๋Œ€๋กœ 

     

    ๊ฐ๊ฐ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ๋กœ ๋‚˜ํƒ€๋‚˜๋ฉด

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

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

     

     

    2๏ธโƒฃ์ •์‚ฌ๊ฐํ˜•์„ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธ ๋กœ์ง

    ํ˜„์žฌ์˜ ์œ„์น˜๊ฐ’์˜ ์ขŒํ‘œ๊ฐ€ >=1 ์ด์–ด์•ผํ•จ

    x >=1 ๊ทธ๋ฆฌ๊ณ  y >=1 ์ธ ๊ฒฝ์šฐ ๊ทธ๋ฆฌ๊ณ  ๋‘˜๋‹ค  n๋ณด๋‹ค๋Š” ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

     

    ์ฝ”๋“œ ๊ตฌํ˜„ ๊ณผ์ •

    - ์˜ˆ์ƒํ–ˆ๋˜ ๊ฒฐ๊ณผ : x์™€ y๊ฐ€ 1๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋งŒ ์•„๋ž˜์˜ ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด x์™€ y๊ฐ€ ์—…๋ฐ์ดํŠธ๋  ๊ฒƒ์ด๋‹ค.

    - ์‹ค์ œ ๊ฒฐ๊ณผ : ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋Œ์•˜๋‹ค.....

    # ํ˜„์žฌ ์œ„์น˜
    x,y=1,1
    
    #์ด๋™ ๋กœ์ง
    
    
    dx = [0,0,-1,1]
    dy =[-1,1,0,0]
    
    while(x,y >=1 ):
      for i in list:
        if i=="L":
          x = x + dx[0]
          y = y+dy[0]
        elif i=="R":
          x = x + dx[1]
          y = y+dy[1]
          print(x,y)
        elif i=="U":
          x = x + dx[2]
          y = y+dy[2]
        else:
          x = x + dx[3]
          y = y+dy[3]

     

    for i in list:
        while(x,y >=1 ):
          if i=="L":
            x = x + dx[0]
            y = y+dy[0]
          elif i=="R":
            x = x + dx[1]
            y = y+dy[1]
            print(x,y)
          elif i=="U":
            x = x + dx[2]
            y = y+dy[2]
          else:
            x = x + dx[3]
            y = y+dy[3]

     

    ์›์ธ  

    1๏ธโƒฃ์šฐ์„  ์กฐ๊ฑด์€ x์™€ y๊ฐ€ >= 1 ๊ทธ๋ฆฌ๊ณ  <=  n ์ด์–ด์•ผํ•œ๋‹ค.

    2๏ธโƒฃํ˜„์žฌ ๋กœ์ง์€ ์กฐ๊ฑด์ด ํ•ญ์ƒ ์ฐธ์ด๋‹ค.

    ๊ทผ๋ฐ ์•„๋ž˜์—์„œ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜๋Š”๋ฐ ์™œ ํ•ญ์ƒ ์ฐธ์ผ๊นŒ?

     

    โ“while(x, y >=1 ) ์ด๋ถ€๋ถ„์€ ํŠœํ”Œ์ธ (x,y)>=1 ์ธ์ง€๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ (x,y)>=1  ์ด๋ถ€๋ถ„์€ ํŒŒ์ด์ฌ์˜ ํŠœํ”Œ ๋น„๊ต์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ๋น„๊ต๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

     

    ๊ทธ๋ž˜์„œ x๊ฐ€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ์ฐธ์ด ๋˜๊ณ  y์˜ ๊ฐ’์— ์ƒ๊ด€์—†์ด ์กฐ๊ฑด์ด ํ•ญ์ƒ ์ฐธ์ด ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค.

     

    ํ•˜์ง€๋งŒ ๋‚ด๊ฐ€ ์˜๋„ํ•œ ๊ฒƒ์€ x๋„ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , y๋„ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋กœ์ง์ด ๋Œ์•„๊ฐ€๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค!!

     

    ๋”ฐ๋ผ์„œ x>=1 and y>=1 ์ฒ˜๋Ÿผ ๋”ฐ๋กœ ๊ฒ€์‚ฌํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

     

    3๏ธโƒฃ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ์š”์†Œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.

    x์™€ y๊ฐ€ ์ •์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ ์ด๋™ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋ฏ€๋กœ continue๋ฅผ ํ†ตํ•ด ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ€๋„๋ก ํ•ด์ค€๋‹ค.

     

    n = int(input())
    list = input().split()
    
    # ํ˜„์žฌ ์œ„์น˜
    x,y=1,1
    
    dx = [0,0,-1,1]
    dy =[-1,1,0,0]
    
    move_types =["L","R","U","D"]
    
    for i in list:
      for j in range(4):
        if i ==move_types[j]:
          nx = x+dx[j]
          ny = y+dy[j]
      if nx <1 or ny<1 or nx>n or ny >n :
        continue
      x,y = nx,ny
          
    print(x,y)

     

    ๋ฐฐ์šด์  

    1๏ธโƒฃwhile(์กฐ๊ฑด)์„ ์ ์–ด์ค€ ํ›„์— ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์•Œ์•„์„œ ๋‹ค์Œ ๋กœ์ง์œผ๋กœ ๋„˜์–ด๊ฐˆ ๊ฑฐ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ continue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์„œ ๋‹ค์Œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ€์ฃผ๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค!

    continue : ํ˜„์žฌ ๋ฐ˜๋ณต์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ์ง์ ‘ ์ ํ”„ํ•œ๋‹ค. 

     

    2๏ธโƒฃ์กฐ๊ฑด์„ ์ ์–ด์ค„ ๋•Œ x>=1 ๊ทธ๋ฆฌ๊ณ  y>=1์ธ ์กฐ๊ฑด์„ ๊ฑธ์–ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด 

     

    โŒ(x,y)>=1 ์˜ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ์•ˆ๋œ๋‹ค.

    ๐Ÿค๋ฐ˜๋“œ์‹œ x>=1 and y>=1 ์ฒ˜๋Ÿผ ๊ฐ๊ฐ ๋”ฐ๋กœ ์กฐ๊ฑด์„ ์ž‘์„ฑํ•ด์ค˜์•ผํ•œ๋‹ค!

     

    3๏ธโƒฃ์ด๋™ ํƒ€์ž…์€ L R U D์˜€๋‹ค. ์ด ๋กœ์ง์€ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค๋งŒ ๋‹ฌ๋ผ์ง€๋Š” ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๋ค๋‹ค.

    ๋”ฐ๋ผ์„œ ์ด๋™ ํƒ€์ž…๋„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์ค€ ํ›„ ๋ฐฉํ–ฅ๋ฒกํ„ฐ์™€ ํ•จ๊ป˜ ๊ฐ™์€ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    4๏ธโƒฃx์™€ y๋ฅผ ๊ฐ™์ด ์ถœ๋ ฅํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ๋Š” print(x,y)๋ฅผ ํ•ด์ฃผ๋ฉด 3 2 ์ฒ˜๋Ÿผ ์ถœ๋ ฅ์ด ๋œ๋‹ค.


    ์˜ˆ์ œ 2 : ์‹œ๊ฐ ํ’€์–ด๋ณด๊ธฐ

    ๋‚ด ์ฒ˜์Œ ์ ‘๊ทผ

    n  = int(input())
    
    count = 0
    
    #1 
    # 3 13 23 
    if (n<13):
      hour = 1
    elif(n<23):
      hour = 2
    else:
      hour = 3
    count += 3600 *hour
    
    # hour = 3 13 23 ์ฆ‰ 24 - 3 = 21
    if(n<3):
      hour = n+1
    if (n<13): 
      hour = n
    
    elif (n<23): 
      hour = n-1
    else:
      hour = n-2
    count += 15 * hour * 60 
    
    #3 
    if(n<3):
      hour = n+1
    if (n<13): 
      hour = n
    
    elif (n<23): 
      hour = n-1
    else:
      hour = n-2
    count += hour * 45 * 15 
    
    print(count)

     

    ์ฑ…์„ ํ†ตํ•ด ์•Œ๊ฒŒ ๋œ ์ฝ”๋“œ

    ์–ด๋–ป๊ฒŒ ์‹œ๊ฐ๋“ค์„ ๋‚˜ํƒ€๋‚ผ์ง€ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„ ์ •์ˆ˜ํ˜•๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•œ๋‹ค๋Š” ์ƒ๊ฐ์œผ๋กœ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ด ์•ˆ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

    ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ€๋Šฅํ–ˆ๋˜ ๊ฒƒ์ด์—ˆ๋‹ค.

    n  = int(input())
    
    count = 0
    
    for i in range(n+1):
      for j in range(60):
        for k in range(60):
          if '3' in str(i) + str(j) + str(k):
            count +=1
    print(count)

     

    ์‹œ๊ฐ ์ž์ฒด๋ฅผ ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค.

    5์‹œ 59๋ถ„ 59์ดˆ๋ผ๋ฉด '055959'๋กœ ๋ณ€ํ™˜๋˜์–ด 3์ด ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•œ๋‹ค.

     

    ์™„์ „ ํƒ์ƒ‰ ์œ ํ˜•

     

    ์™„์ „ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ฒ€์‚ฌํ•ด๋ณด๋Š” ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์ด๋‹ค. ์œ„์˜ ๊ฒฝ์šฐ๋„ ์™„์ „ ํƒ์ƒ‰์— ํ•ด๋‹นํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งค์šฐ ํด ๊ฒฝ์šฐ์—๋Š” ๋น„ํšจ์œจ์ ์ธ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. 

    ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ํ™•์ธ(ํƒ์ƒ‰) ํ•ด์•ผํ•  ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 100๋งŒ๊ฐœ ์ดํ•˜์ผ ๋•Œ ์™„์ „ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜๋ฉด ์ ์ ˆํ•˜๋‹ค.

     

    ๋ฐฐ์šด ์ 

    1๏ธโƒฃ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 100๋งŒ๊ฐœ ์ดํ•˜์ผ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•ด๋ณด๋Š” ์™„์ „ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ณต์žกํ•œ ๋กœ์ง๊ตฌํ˜„๋ณด๋‹ค ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    2๏ธโƒฃํŠน์ • ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•  ๋•Œ์—๋Š” str.contains('๊ฒ€์‚ฌํ•˜๊ณ ํ”ˆ ๋ฌธ์ž') ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

    3๏ธโƒฃ ํŠน์ • ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•  ๋•Œ์—๋Š”  ํŠน์ • ๋ฌธ์ž in ๋ฌธ์ž์—ด์„ ํ†ตํ•ด์„œ๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 


    ๐Ÿ‘‘์™•์‹ค์˜ ๋‚˜์ดํŠธ ๋ฌธ์ œ 

    ๋‚ด ์ฝ”๋“œ

    position = input()
    
    # ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜
    x =  int(position[1])
    y = ord(position[0]) - ord('a') +1
    
    count =0
    
    # ์ด๋™ ๋กœ์ง
    
    dx = [-2,-1,1,2,2,1,-1,-2]
    dy = [1,2,2,1,-1,-2,-2,-1]
    
    for i in range(8):
      nx = x+dx[i]
      ny = y+dy[i]
      if nx<1 or ny<1 or nx>8 or ny>8:
        continue
      else:
        count +=1
    print(count)

     

    dx,dy๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด์•˜๋‹ค.

    ์ค‘๋‹จ์กฐ๊ฑด์ด ๋‚˜์˜ค๋ฉด ๋‹ค์Œ ์ธ๋ฑ์Šค๋กœ ๋„˜์–ด๊ฐ€๊ณ , ๋งŒ์•ฝ ์ด๋™์„ ์ˆ˜ํ–‰ํ•œ ์ขŒํ‘œ๊ฐ€  8*8 ๋‚ด๋ถ€์— ์œ„์น˜ํ•œ๋‹ค๋ฉด count๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์ฃผ์—ˆ๋‹ค.

     

    ๋ฌธ์ œ ํ•ด๊ฒฐ ๋กœ์ง์„ ์•„๋ž˜ 2๊ฐ€์ง€ ๊ณผ์ •์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ƒ๊ฐํ–ˆ๋‹ค. 

     

    1๏ธโƒฃ์ขŒํ‘œ ๋ณ€ํ™˜ 

    ์ฒ˜์Œ์—๋Š” ์•ŒํŒŒ๋ฒณ์„ ์—ด ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•ด์ค„ ๋•Œ  ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ { key : value } ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ์ˆ˜์ธ ord๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๊ณ  ord๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

     

    2๏ธโƒฃ์ด๋™ ๋กœ์ง 

    ์ด๋™ ๋กœ์ง์„ ์œ„ํ•ด ์ฒ˜์Œ์—๋Š” ๋ฆฌ์ŠคํŠธ ์•ˆ์—[(-2,1) (-1,2) (1,2) , (2,1), (2,-1), (1,-2), (-1,-2), (-2,-1)] ์ด๋ ‡๊ฒŒ 8๊ฐ€์ง€์˜ ์ด๋™ ์‹œ ์ขŒํ‘œ ๋ณ€ํ™”๋ฅผ ๋‹ด์•„์ฃผ๊ณ  ์ด ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ  ํŠœํ”Œ์„ ์ธ๋ฑ์Šค๋กœ ์ฒ˜๋ฆฌํ•ด์„œ nx ์™€ ny๋ฅผ ๊ตฌํ•ด์ฃผ๋Š” ๋กœ์ง์„ ๋– ์˜ฌ๋ ธ๋‹ค.

     

    dx, dy์™€ i ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด๋ณด๊ณ ์‹ถ์–ด์„œ ์œ„์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ๋‹ค.

     

    ๐Ÿค์ƒˆ๋กœ ๋ฐฐ์šด์  

    1๏ธโƒฃ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ ๊ตฌํ•˜๊ธฐ

    ํŒŒ์ด์ฌ์—์„œ ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

    ord(๋ฌธ์ž) - ord('a') +1

     

    2๏ธโƒฃ๋ฌธ์ž์—ด์— ์ ‘๊ทผํ•˜๊ธฐ

    ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ž…๋ ฅ์ด b3์˜ ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง„๋‹ค.  3์€ ํ–‰ ๊ฐ’์ด ๋˜๊ณ  b๋Š” ์—ด ๊ฐ’์ด ๋œ๋‹ค. 

    ์ด ๋•Œ ๋ฌธ์ž์—ด์— ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. b3์„ position ๋ณ€์ˆ˜์— ๋‹ด์•˜๋‹ค๋ฉด position[0]์€ b๊ฐ€ ๋˜๊ณ  position[1]์€ 3์ด ๋œ๋‹ค.

     

    ๐Ÿฃ๋‹ค์–‘ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

    1๏ธโƒฃ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ ๊ตฌํ•˜๊ธฐ

    ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ •์˜ํ•ด๋‘๊ณ  position[0]์— ์ €์žฅ๋œ ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ๋ฅผ์•Œ ์ˆ˜ ์žˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด col['a']๋ฅผ ํ†ตํ•ด value ๊ฐ’์ธ 1์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

    col = {'a':1,'b':2, 'c': 3,'d':4 , 'e':5,'f':6,'g':7,'h':8}
    y = col[position[0]]

     

    2๏ธโƒฃ์ด๋™ ๋กœ์ง 

    # ์ด๋™ ๋กœ์ง 2
    steps =[(-2,1), (-1,2), (1,2) , (2,1), (2,-1), (1,-2), (-1,-2), (-2,-1)] 
    
    
    for i in steps:
      nx = x + i[0]
      ny = y + i[1]
      if nx<1 or ny<1 or nx>8 or ny>8:
        continue
      else:
        count +=1
    print(count)

     

    ๐ŸŽฎ๊ฒŒ์ž„ ๊ฐœ๋ฐœ ๋ฌธ์ œ

    ํ•„์š”ํ•œ ๋กœ์ง

     

    ์ž…๋ ฅ๋ฐ›๊ธฐ

    ์ด๋™๋กœ์ง

    ์ค‘๋‹จ์กฐ๊ฑด

     

    ์ด๋™ ๋กœ์ง 

    ์ง€๊ธˆ์€ ๋ฐฉํ–ฅ์ด 0, 1, 2, 3์ธ ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๋น„๊ตํ•˜๊ณ  ์žˆ๋‹ค. ์ด ๋ฐฉํ–ฅ ๋กœ์ง์„ ๋” ๊ฐ„์†Œํ™”ํ•  ์ˆ˜๋Š” ์—†์„๊นŒ?

     

    ์ฒ˜์Œ ์ฝ”๋“œ๋Š” ์ง€๊ธˆ ๋ฐฉํ–ฅ์ด 0 ,1 , 2, 3์ธ์ง€ ์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ๋‹ค ๋น„๊ต๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋Š” ๋ฐ˜๋ณต๋„ ๋งŽ์•˜๊ณ , ์–ธ์ œ ์–ด๋–ป๊ฒŒ ์ค‘๋‹จ์„ ํ•ด์ฃผ์–ด์•ผํ• ์ง€ ์ •ํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค. 

    if current_list[2] == 0:
        nx = current_list[0] + dx[left[0]]
        ny = current_list[1] +dy[left[0]]
        if nx>0 and ny>0 and entire_map[current_list[0]][current_list[1]] ==0:
          count +=1
          current_list[0]=nx
          current_list[1]=ny
          current_list[2]=left[0]
          continue
        else:
          current_list[2] = left[1]

     

     

    1๏ธโƒฃ์–ธ์ œ ์ค‘๋‹จํ•ด์•ผํ• ๊นŒ?

    ๋„ค ๋ฐฉํ–ฅ ๋ชจ๋‘ ๋‹ค ์กฐ์‚ฌ ํ›„ ๋’ค๋กœ ์ด๋™ํ–ˆ๋Š”๋ฐ ๋ฐ”๋‹ค์˜€์„ ๊ฒฝ์šฐ! 

     

    ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์€ ์œ ์ง€ํ•˜๊ณ  1์นธ ๋’ค๋กœ ์ด๋™

    -> ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ด๋™ํ•˜๊ธฐ 

    -> ๋ฐ”๋‹ค๋ผ๋ฉด ๋๋‚ด๊ธฐ

     

    ์ด๋ฅผ ์œ„ํ•ด ๋ช‡ ๋ฒˆ ํšŒ์ „ํ–ˆ๋Š”์ง€๋ฅผ ์„ธ๋Š” turn_time ๋ณ€์ˆ˜ ์ถ”๊ฐ€

     

    ์ค‘๋‹จํ•ด์•ผํ•  ๋•Œ๋Š”  While True:์™€ break๋ฅผ ํ™œ์šฉํ•ด๋ณด์ž 

     

    2๏ธโƒฃ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•˜๋Š” ๋กœ์ง์„ ํ•จ์ˆ˜๋กœ ๋นผ๊ธฐ

    ๋ฐฉํ–ฅ์ด 0์ผ ๋•Œ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•˜๋ฉด  -> 3

    ๋ฐฉํ–ฅ์ด 1์ผ ๋•Œ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•˜๋ฉด  -> 0

    ๋ฐฉํ–ฅ์ด 2์ผ ๋•Œ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•˜๋ฉด  -> 1

    ๋ฐฉํ–ฅ์ด 3์ผ ๋•Œ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•˜๋ฉด  -> 2

     

    ๊ทธ๋ž˜์„œ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ ์‹œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜„์žฌ ๋ฐฉํ–ฅ์—์„œ -1๋งŒํผ ๋œ๋‹ค.

    ๋งŒ์•ฝ ๋ฐฉํ–ฅ์ด -1์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ทธ ๋•Œ๋งŒ ๋ฐฉํ–ฅ์„ 3์œผ๋กœ ์ ์šฉํ•ด์ค€๋‹ค.

    n, m = map(int,input().split())
    x,y,direction = map(int,input().split())
    
    
    
    #์ „์ฒด ๋งต ์ •๋ณด
    entire_map = []
    for i in range(n):
      entire_map.append(list(map(int,input().split())))
    
    # ์ด๋™ ์‹œ ์ขŒํ‘œ ๋ณ€ํ™”
    dx = [-1,0,1,0]
    dy = [0,1,0,-1]
    
    # ๊ฒฝ์šฐ์˜ ์ˆ˜ 
    count = 1
    
    # ๋ช‡ ๋ฒˆ ํšŒ์ „ํ–ˆ๋Š”์ง€ 
    turn_time =0
    
    # ์™ผ์ชฝ์œผ๋กœ ๋ฐฉํ–ฅ ํšŒ์ „ ํ•จ์ˆ˜
    def turn_left():
      global direction
      direction-=1
      if direction == -1:
        direction = 3
    
    while True:
        turn_left()
        nx = x + dx[direction]
        ny = y + dy[direction]
        if nx>0 and ny>0 and entire_map[nx][ny] == 0:
          count += 1
          entire_map[nx][ny] = 1
          x =nx
          y = ny
          turn_time = 0
          continue
        else:
          turn_time +=1
    
        # ์ค‘๋‹จ ์กฐ๊ฑด 
        if turn_time ==4:
          nx = x - dx[direction]
          ny = y - dy[direction]
          if entire_map[nx][ny] ==0:
            x = nx
            y = ny
          else:
            break
          turn_time = 0
          
    print(count)

     

    ๐Ÿค์ƒˆ๋กœ ๋ฐฐ์šด ์ 

    1๏ธโƒฃ2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๋ฅผ ์‚ฌ์šฉํ•˜์ž!

     

    ํŠนํžˆ ํŠน์ • ํฌ๊ธฐ์˜ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ์—๋Š” ๋ฐ˜๋“œ์‹œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. 

    ์•„๋ž˜ ๋กœ์ง์„ ํ†ตํ•ด 3*4 ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

    n = 3
    m = 4
    
    array = [ [0] * m for _ in range(n) ]

     

     

    2๏ธโƒฃ์ค‘๋‹จ ์กฐ๊ฑด์ด ์žˆ์„ ๊ฒฝ์šฐ While True์™€ break๋ฌธ์„ ํ™œ์šฉํ•˜์ž

    3๏ธโƒฃํŒŒ์ด์ฌ์— ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” ์ด๋ฆ„์„ ์“ฐ์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•˜์ž

     

    ๋‚˜๋Š” ์ฒ˜์Œ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ list๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ด๋Š” ์ด๋ฏธ ํŒŒ์ด์ฌ์— ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” ํ•จ์ˆ˜๋ช…๊ณผ ๊ฐ™์•„์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค! 

     

     

    728x90