티스토리 뷰

BOJ :: 백준 :: 2174 :: 로봇 시뮬레이션


출처 : https://www.acmicpc.net/problem/2174


#include <iostream>

#include <algorithm>

using namespace std;


struct R {

int i, j, d;

int r, c, n;

};

int N, M, n1, n2, map[101][101];

int dd[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };

R ro[101], co[101];


int main() {

std::ios::sync_with_stdio(false); cin.tie(0);


cin >> M >> N; cin >> n1 >> n2; char tmp;

for (int i = 1; i <= n1; i++) {

cin >> ro[i].j >> ro[i].i >> tmp;

if (tmp == 'N') ro[i].d = 0;

else if (tmp == 'E') ro[i].d = 1;

else if (tmp == 'S') ro[i].d = 2;

else ro[i].d = 3;

map[ro[i].i][ro[i].j] = i;

}

for (int i = 1; i <= n2; i++) {

cin >> co[i].r >> tmp >> co[i].n;

if (tmp == 'L') co[i].c = 0;

else if (tmp == 'R') co[i].c = 1;

else co[i].c = 2;

}


for (int command = 1; command <= n2; command++) {

if (co[command].c == 0) {

int f = co[command].n;

while (f--) {

if (ro[co[command].r].d == 0) ro[co[command].r].d = 4;

ro[co[command].r].d -= 1;

}

}

else if (co[command].c == 1) {

int f = co[command].n;

while (f--) {

if (ro[co[command].r].d == 3) ro[co[command].r].d = -1;

ro[co[command].r].d += 1;

}

}

else {

int f = co[command].n; int dir = ro[co[command].r].d;

int ni = ro[co[command].r].i + (dd[dir][0]*f); 

int nj = ro[co[command].r].j + (dd[dir][1]*f);

if (ni > 0 && nj > 0 && ni <= N && nj <= M && map[ni][nj] < 1) {

map[ro[co[command].r].i][ro[co[command].r].j] = 0;

map[ni][nj] = co[command].r; ro[co[command].r].i = ni; ro[co[command].r].j = nj;

}

else if (ni < 1 || nj < 1 || ni>N || nj>M) {

cout << "Robot " << co[command].r << " crashes into the wall" << endl; return 0;

}

else if (map[ni][nj] > 0) {

cout << "Robot " << co[command].r << " crashes into robot " << map[ni][nj] << endl; return 0;

}

}

}


cout << "OK" << endl;


return 0;

}


'알고리즘 > BOJ' 카테고리의 다른 글

[BOJ 2468] 안전 영역  (0) 2018.09.18
[BOJ 2589] 보물섬  (0) 2018.09.18
[BOJ 1103] 게임  (0) 2018.08.26
[BOJ 1937] 욕심쟁이 판다  (0) 2018.08.26
[BOJ 15683] 감시  (0) 2018.08.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함