본문 바로가기
알고리즘/알고리즘

[C++]백준 8972번 : 미친 아두이노

by 두둠칫 2021. 9. 4.

https://www.acmicpc.net/problem/8972

 

8972번: 미친 아두이노

요즘 종수는 아두이노를 이용해 "Robots"이라는 게임을 만들었다. 종수는 아두이노 한대를 조정하며, 미친 아두이노를 피해다녀야 한다. 미친 아두이노는 종수의 아두이노를 향해 점점 다가온다.

www.acmicpc.net

 

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;
}