프로그래머스 <공원산책>

마스터피스·2024년 2월 1일

코딩테스트 문제

목록 보기
12/21
post-thumbnail
#include <string>
#include <vector>
#include <map>

using namespace std;

typedef struct {
int x, y;
}box;

// 상 하 좌 우
box moveD[4] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
map<char, int> mapping = { { 'N', 0 }, { 'S', 1 }, { 'W', 2 }, { 'E', 3 } }; // 1

vector<int> solution(vector<string> park, vector<string> routes) {
    pair<int, int> loc;

    int H = park.size();
    int W = park[0].size();

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (park[i][j] == 'S') {
                loc = { i, j }; // 2
                break;
            }
        }
    }

        // 3
    for (const auto &route: routes) {
        char op = mapping[route[0]];
        int n = route[2] - 48;

        int nx = loc.first;
        int ny = loc.second;

        while (n--) {
            nx += moveD[op].x;
            ny += moveD[op].y;

            if ((nx < 0 || H <= nx || ny < 0 || W <= ny) || park[nx][ny] == 'X') 
                        break;
        }

        if (n < 0) loc = { nx, ny }; // 4
    }

    return { loc.first, loc.second };
}
profile
코딩 일지

0개의 댓글