๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 20436๋ฒˆ - ZOAC 3

by ์•ˆ์ฃผํ˜• 2022. 9. 15.

๋ฌธ์ œ

20436๋ฒˆ: ZOAC 3

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋‘ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž sL, sR์ด ์ฃผ์–ด์ง„๋‹ค. sL, sR์€ ๊ฐ๊ฐ ์™ผ์† ๊ฒ€์ง€์†๊ฐ€๋ฝ, ์˜ค๋ฅธ์† ๊ฒ€์ง€์†๊ฐ€๋ฝ์˜ ์ฒ˜์Œ ์œ„์น˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜

www.acmicpc.net

์ฝ”๋“œ

#include <iostream>
using namespace std;

int map[3][10];

string top = "qwertyuiop";
string middle = "asdfghjkl";
string bottom = "zxcvbnm";

struct Info{
    int r;
    int c;
    char hand;
};

Info l, r;

Info find_coor(char key){
    int _r = 0, _c = 0;
    char hand;
    for(int i=0; i<top.length(); i++){
        if(top[i] == key){
            _r = 0, _c = i;
            if(i<=4) hand = 'l';
            else hand = 'r';
        }
    }
    for(int i=0; i<middle.length(); i++){
        if(middle[i] == key){
            _r = 1, _c = i;
            if(i <= 4) hand = 'l';
            else hand = 'r';
        }
    }
    for(int i=0; i<bottom.length(); i++){
        if(bottom[i] == key){
            _r = 2, _c = i;
            if(i <= 3) hand = 'l';
            else hand = 'r';
        }
    }
    return {_r, _c, hand};
}

int find_dist(char key){
    Info key_coor = find_coor(key);
    int dist = 0;
    if(key_coor.hand == 'l'){
        dist = abs(l.r - key_coor.r) + abs(l.c - key_coor.c);
        l.r = key_coor.r;
        l.c = key_coor.c;
    }
    if(key_coor.hand == 'r'){
        dist = abs(r.r - key_coor.r) + abs(r.c - key_coor.c);
        r.r = key_coor.r;
        r.c = key_coor.c;
    }
    return dist;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    char left, right; cin>>left>>right;
    string s; cin>>s;
    
    l = find_coor(left);
    r = find_coor(right);
    int ans = 0;
    for(char key: s) ans += find_dist(key);
    
    cout<<ans+s.length();

}

ํ’€์ด

์ฒ˜์Œ์— ์„ฑ์šฐ๋Š” ํ•œ๊ธ€ ์ž์Œ ์ชฝ ์žํŒ์€ ์™ผ์† ๊ฒ€์ง€ ์†๊ฐ€๋ฝ์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ , ํ•œ๊ธ€ ๋ชจ์Œ ์ชฝ ์žํŒ์€ ์˜ค๋ฅธ์† ๊ฒ€์ง€ ์†๊ฐ€๋ฝ์œผ๋กœ ์ž…๋ ฅํ•œ๋‹ค. ๋ผ๋Š” ์กฐ๊ฑด์„ ์ œ๋Œ€๋กœ ์ฝ์ง€ ๋ชปํ•ด์„œ ์™ผ์†๊ณผ ์˜ค๋ฅธ์† ๋‘˜ ๋‹ค ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋‘˜ ์ค‘ ๊ฐ€๊นŒ์šด ์†๊ฐ€๋ฝ์„ ๋ˆ„๋ฅด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ˜ผ์ž ํ•œ๋ฒˆ ๋” ๊ผฌ์•„์„œ ์ž˜๋ชป ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹..
ํ‚ค๋ณด๋“œ์˜ ์ ค์œ„, ์ค‘๊ฐ„, ์ ค ํ•˜๋‹จ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์„ ์–ธํ•˜๊ณ  ๊ฐ๊ฐ์˜ ๋ฌธ์ž๊ฐ€ ํ•ด๋‹น ๋ฌธ์ž์—ด์— ํฌํ•จํ•  ๋•Œ ๋ฐฐ์—ด์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€