[BOJ 2174] 로봇 시뮬레이션
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;
}