[코드트리] 전투 로봇

rhkr9080·2023년 7월 27일
0

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

💻 문제 풀이 : C++

😂 1차 시도 실패 (38%)
#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;
};

bool operator<(Unit a, Unit b)
{
    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];
int visited[MAP_SIZE][MAP_SIZE];
Unit robot;
vector<Unit> monster;

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

void CLEAR()
{
    n = 0;
    memset(MAP, 0, sizeof(MAP));
    memset(visited, 0, sizeof(visited));
    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};
            }
            else if (MAP[i][j] != 0)
            {
                monster.push_back({i, j, MAP[i][j], 0});
            }
        }
    }
}

int getMonster()
{
    memset(visited, 0, sizeof(visited));
    // Unit tmpRobot = robot;
    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};
            if (next.row < 0 || next.col < 0 || next.row >= n || next.col >=n ) 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});
                
            }
            if (MAP[next.row][next.col] > now.level) continue;
            if (visited[next.row][next.col] != 0) continue;
            visited[next.row][next.col] = visited[now.row][now.col] + 1;
            nowQ.push(next);
       }
       if (!tmpTarget.empty())
       {
            Unit next = tmpTarget.top();
            robot.row = next.row;
            robot.col = next.col;
            robot.exp++;
            if (robot.exp >= robot.level)
            {

                robot.exp = 0;
                robot.level += 1;
                // cout << "LEVEL UP!" << endl;
            }
            MAP[next.row][next.col] = 0;
            // cout << "**********" << endl;
            // while(!tmpTarget.empty())
            // {
            //     cout << tmpTarget.top().row << " " << tmpTarget.top().col << endl;
            //     tmpTarget.pop();
            // }
            
            cout << next.row << " " << next.col << " " << visited[now.row][now.col] << endl;
            return visited[now.row][now.col];
       }
    }
    return 0;
}

void SOLVE()
{
    int time = 0 ;

    while(time < 1000)
    {
        int tmpTime = getMonster();
        if(tmpTime == 0)
        {
            break;
        }
        else
        {
            cout << "**" << time << "\n";
            time += tmpTime;
        }
    }

    cout << time << endl;
}

int main() {

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

    return 0;
}

📌 memo 😊

profile
공부방

1개의 댓글

comment-user-thumbnail
2023년 7월 27일

많은 도움이 되었습니다, 감사합니다.

답글 달기