๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 20436 - ZOAC 3

by YWTechIT 2021. 6. 1.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 20436 - ZOAC 3

๋ฐฑ์ค€ 20436 - ZOAC 3


โšก๏ธ ๋‚˜์˜ ํ’€์ด

์—†๋Š” ๊ฒƒ์„ ์งœ๋‚ผ๋ ค๋‹ˆ๊นŒ(?) ์กฐ๊ธˆ ํž˜๋“ค์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.

 

์ฒ˜์Œ์— 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋กœ ๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ๋Š์–ด์„œ keyboard = [['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'], ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'], ['z', 'x', 'c', 'v', 'b', 'n', 'm']]๋กœ ์„ ์–ธํ–ˆ๋Š”๋ฐ, ์ด๊ฒƒ๋ณด๋‹ค ํ•˜๋‹จ์— ์žˆ๋Š” ์ฝ”๋“œ์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ์‹ธ๊ณ  ๋ฌธ์ž์—ด์— 'str'์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋ฉด ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜์ง€๋งŒ ์ฝ”๋“œ๋Š” ๋”์šฑ ๊ฐ„๊ฒฐํ•ด์ง€๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์ด ๋ฌธ์ œ์—์„œ ํ•ต์‹ฌ์ ์œผ๋กœ ํŒ๋‹จํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1. ๋ฌธ์ž๋ฅผ ํ‚ค๋ณด๋“œ์˜ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

  - check_coordinateํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ํ•ด๋‹น key์˜ column๊ณผ index์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค.

 

2. ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ํ‚ค๋ณด๋“œ์˜ ํ•œ๊ธ€ ์ž์Œ์ธ์ง€ ํ•œ๊ธ€ ๋ชจ์Œ์ธ์ง€ ๊ตฌ๋ถ„ํ•œ๋‹ค. ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด ๋‹ค์Œ ์‚ฌ์ง„์„ ๋ณด์ž.

 

 

3. ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ด ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.

  - ๋ฌธ์ž์—ด์˜ ์ „์ฒด ๊ธธ์ด(๊ฐ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„) + ์ด์ „ key์—์„œ ๋‹ค์Œ key๋กœ ๋„˜์–ด๊ฐ€๋Š” ์‹œ๊ฐ„์„ ํƒ์‹œ ๊ฑฐ๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‹ค์Œ key๋กœ ๋„˜์–ด๊ฐ€๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ–ˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ์ž…๋ ฅ์€ ์ •ํ™•ํžˆ 2๊ธ€์ž๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

 

import sys
keyboard = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']    # keyboard position

def check_coordinate(key):    # find coordinate, index key
    for i in range(3):
        if key in keyboard[i]:
            return i, keyboard[i].index(key)

sl, sr = input().split()
st = sys.stdin.readline().strip()
time, distance = len(st), 0    # ๋ฌธ์ž์—ด์˜ ์ „์ฒด ๊ธธ์ด

cur_sl_column, cur_sl_row = check_coordinate(sl)    # ์ฒ˜์Œ ์ฃผ์–ด์ง€๋Š” sl์˜ ์ขŒํ‘œ
cur_sr_column, cur_sr_row = check_coordinate(sr)    # ์ฒ˜์Œ ์ฃผ์–ด์ง€๋Š” sr์˜ ์ขŒํ‘œ

for i in st:
    column, row = check_coordinate(i)
    if (column <= 1 and row <= 4) or (column == 2 and row <= 3):    # ์ž์Œ ์žํŒ์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ
        distance += abs(cur_sl_column - column) + abs(cur_sl_row - row)    # ํƒ์‹œ ๊ฑฐ๋ฆฌ
        cur_sl_column = column    # cur_column ๊ฐ’ ๊ฐฑ์‹ 
        cur_sl_row = row    # cur_row ๊ฐ’ ๊ฐฑ์‹ 
    else:    # ๋ชจ์Œ ์žํŒ์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ
        distance += abs(cur_sr_column - column) + abs(cur_sr_row - row)    
        cur_sr_column = column
        cur_sr_row = row

time += distance    # ๋ฌธ์ž์—ด์˜ ์ „์ฒด ๊ธธ์ด + `key`๊ฐ€ ์›€์ง์ธ ์ด ๊ฑฐ๋ฆฌ
print(time)
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€