[코드트리] 전투로봇

rhkr9080·2023년 8월 30일
0

코드트리

목록 보기
15/29

문제링크 : https://www.codetree.ai/training-field/frequent-problems/problems/fighting-robot/description?page=1&pageSize=20

💻 문제 풀이 : C++

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>

#define MAP_SIZE 25

using namespace std;

struct Unit
{
	int row;
	int col;
	int level;
	int exp;
	int dir;
};

bool operator<(Unit a, Unit b)
{
	if (a.dir > b.dir) return true;
	if (a.dir < b.dir) return false;
	if (a.row > b.row) return true;
	if (a.row < b.row) return false;
	if (a.col > b.col) return true;
	if (a.col < b.col) return false;
	return false;
}

int n;
int MAP[MAP_SIZE][MAP_SIZE];
Unit robot;
vector<Unit> monster;

// 위, 왼쪽, 오른쪽, 아래 ??
int dr[] = { -1, 0, 0, 1 };
int dc[] = { 0, -1, 1, 0 };

void CLEAR()
{
	n = 0;
	memset(MAP, 0, sizeof(MAP));
	monster.clear();
}

void INPUT()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> MAP[i][j];
			if (MAP[i][j] == 9)
			{
				robot = { i, j, 2, 0, 0 };
				MAP[i][j] = 0;
			}
			else if (MAP[i][j] != 0)
			{
				monster.push_back({ i, j, MAP[i][j], 0, 0 });
			}
		}
	}
}

int getMonster()
{
	int visited[25][25] = { 0 };
	queue<Unit> nowQ;
	priority_queue<Unit> tmpTarget;
	nowQ.push(robot);
	visited[robot.row][robot.col] = 1;
	while (!nowQ.empty())
	{
		Unit now = nowQ.front();
		nowQ.pop();
		for (int i = 0; i < 4; i++)
		{
			Unit next = { now.row + dr[i], now.col + dc[i], now.level, now.exp, now.dir };
			if (next.row < 0 || next.col < 0 || next.row >= n || next.col >= n) continue;
			if (MAP[next.row][next.col] > now.level) continue;
			if (visited[next.row][next.col] != 0) continue;
			if (MAP[next.row][next.col] != 0 && MAP[next.row][next.col] < robot.level)
			{
				tmpTarget.push({ next.row, next.col, next.level, next.exp, visited[now.row][now.col] });
			}
			visited[next.row][next.col] = visited[now.row][now.col] + 1;
			nowQ.push(next);
		}
	}
	// 몬스터 없애기
	if (!tmpTarget.empty()) {
		// 로봇 이동
		robot.row = tmpTarget.top().row;
		robot.col = tmpTarget.top().col;
		// 해당 칸은 빈칸이 됩니다
		MAP[tmpTarget.top().row][tmpTarget.top().col] = 0;
		// 경험치 상승
		robot.exp += 1;
		// 레벨 상승
		if (robot.exp == robot.level) {
			robot.level += 1;
			robot.exp = 0;
		}
		return tmpTarget.top().dir;
	}
	return 0;
}

void SOLVE()
{
	int time = 0;

	while (time < 10000)
	{
		int tmpTime = getMonster();
		// 없앨 수 있는 몬스터가 없다면 종료
		if (tmpTime == 0) break;
		// 몬스터를 없애는 시간은 없음
		else time += tmpTime;
	}

	cout << time << endl;
}

int main() {

	CLEAR();
	INPUT();
	SOLVE();

	return 0;
}

📌 memo 😊

profile
공부방

0개의 댓글