반갑습니다!

[백준] 2174 로봇 시뮬레이션 본문

알고리즘 문제 풀이

[백준] 2174 로봇 시뮬레이션

김덜덜이 2020. 4. 30. 15:45

풀이

문제의 조건에 맞춰서 구현하면 된다. 방향 전환을 쉽게 구현하기 위해 숫자로 저장하였다.

코드

#include <iostream>
#include <map>
using namespace std;

struct robot {
    int x, y, dir;
};

int a, b, n, m;
robot r[101];
int dx[] = { 0, 1, 0, -1 }, dy[] = { 1, 0, -1, 0 };

bool inRange(int x, int y) {
    return (1 <= x && x <= a) && (1 <= y && y <= b);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> a >> b;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        int tmp1, tmp2;
        char tmp3;
        cin >> tmp1 >> tmp2 >> tmp3;
        r[i].x = tmp1;
        r[i].y = tmp2;
        if (tmp3 == 'N') r[i].dir = 0;
        else if (tmp3 == 'E') r[i].dir = 1;
        else if (tmp3 == 'S') r[i].dir = 2;
        else r[i].dir = 3;
    }

    for (int i = 0; i < m; i++) {
        int num, repeat;
        char cmd;
        cin >> num >> cmd >> repeat;
        for (int j = 0; j < repeat; j++) {
            if (cmd == 'F') {
                r[num].x += dx[r[num].dir];
                r[num].y += dy[r[num].dir];
                if (!inRange(r[num].x, r[num].y)) {
                    cout << "Robot " << num << " crashes into the wall\n";
                    exit(0);
                }
                for (int l = 1; l <= n; l++) {
                    if (l == num) continue;
                    if (r[num].x == r[l].x && r[num].y == r[l].y) {
                        cout << "Robot " << num << " crashes into robot " << l << '\n';
                        exit(0);
                    }
                }
            }
            else if (cmd == 'R') r[num].dir = (r[num].dir + 1) % 4;
            else r[num].dir = (r[num].dir - 1 + 4) % 4;
        }
    }
    cout << "OK\n";
    return 0;
}

'알고리즘 문제 풀이' 카테고리의 다른 글

[백준] 11404 플로이드  (0) 2020.05.01
[백준] 14502 연구소  (0) 2020.04.30
[백준] 3987 보이저 1호  (0) 2020.04.30
[백준] 9655 돌 게임  (0) 2020.04.29
[백준] 16197 두 동전  (0) 2020.04.29