https://www.acmicpc.net/problem/8972
1. 풀이
시뮬레이션문제
로봇이 이동할 때, 해당 턴에 로봇이 2개 이상 이동한 자리면 터뜨리는 것만 잘 구현하면 될듯
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
struct pos{
int y, x;
};
int R, C;
char map[100][100];
int cntR[100][100];
pos cPos;
queue<pos> rPos;
string tmp = "";
pos dir[9] = { { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, -1 }, { 0, 0 }, { 0, 1 }, { -1, -1 }, { -1, 0 }, { -1, 1 } };
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> R >> C;
for (int i = 0; i < R; i++){
cin >> tmp;
for (int j = 0; j < C; j++){
map[i][j] = tmp[j];
if (tmp[j] == 'I')
cPos = { i, j };
else if (tmp[j] == 'R')
rPos.push({ i, j });
}
}
cin >> tmp; // 움직이는 방향
for (int i = 0; i < tmp.length(); i++){
// 종수 이동
for (int j = 0; j < R; j++)
for (int k = 0; k < C; k++)
cntR[j][k] = 0;
int cDir = (int)(tmp[i] - '0') - 1;
int ny = cPos.y + dir[cDir].y, nx = cPos.x + dir[cDir].x;
if (map[ny][nx] == 'R'){
cout << "kraj " << i + 1;
return 0;
}
map[cPos.y][cPos.x] = '.';
cPos = { ny, nx };
map[ny][nx] = 'I';
// 미친아두이노 이동
int qSize = rPos.size();
for (int j = 0; j < qSize; j++){
int cry = rPos.front().y, crx = rPos.front().x;
map[cry][crx] = '.';
rPos.pop();
int nry = cry, nrx = crx;
if ((cPos.y - cry) != 0)
nry += ((cPos.y - cry) / abs((cPos.y - cry)));
if ((cPos.x - crx) != 0)
nrx += ((cPos.x - crx) / abs((cPos.x - crx)));
if (map[nry][nrx] == 'I'){
cout << "kraj " << i + 1;
return 0;
}
cntR[nry][nrx]++; // 해당 턴 해당 자리 이동한 미친아두이노 수++
rPos.push({ nry, nrx });
}
qSize = rPos.size();
for (int j = 0; j < qSize; j++){
if (cntR[rPos.front().y][rPos.front().x] < 2){ // 2미만 일때만 정상이동
map[rPos.front().y][rPos.front().x] = 'R';
rPos.push(rPos.front());
}
rPos.pop();
}
}
for (int i = 0; i < R; i++){
for (int j = 0; j < C; j++){
cout << map[i][j];
}
cout << "\n";
}
return 0;
}
'알고리즘 > 알고리즘' 카테고리의 다른 글
[C++] 백준 1800번 : 인터넷설치 (0) | 2021.09.10 |
---|---|
[C++]백준 1197번 : 최소 스패닝 트리 (0) | 2021.09.09 |
[C++]백준 2234번 : 성곽 (0) | 2021.09.03 |
[C++]백준 17182번 : 우주탐사선 (0) | 2021.08.29 |
[C++]백준 14466번 : 소가 길을 건너간 이유 (0) | 2021.08.28 |