[C++] 백준 30036 INK

semin·2023년 10월 18일

https://www.acmicpc.net/problem/30036
제목 : INK
solved.ac 난이도 : Silver I

풀이

위 사진은 문제에서 주어지는 이동 커멘드에 대한 설명이다.
커멘드는 총 j, J, U, D, R, L 6가지가 있다.

벽의 좌표를 따로 저장하여 플레이어의 좌표와 계산해서 나온 거리가 잉크의 단계보다 작거나 같으면 벽의 위치에
(점프횟수 % 잉크 종류개수) 의 위치에 있는 잉크를 덮어 씌우는 식으로 풀었다.

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

void moveplayer(vector<string> &map, int& y, int& x, char dir, int N) {
    if (dir == 'U') {
        if (y - 1 >= 0 && map[y - 1][x] == '.') y -= 1;
    }
    if (dir == 'D') {
        if (y + 1 < N && map[y + 1][x] == '.') y += 1;
    }
    if (dir == 'L') {
        if (x - 1 >= 0 && map[y][x - 1] == '.') x -= 1;
    }
    if (dir == 'R') {
        if (x + 1 < N && map[y][x + 1] == '.') x += 1;
    }
}

void jump(vector<pair<int, int>>& wall, vector<string>& map, int& jumpcount, int& inklevel, string ink, int y, int x) {
    for (int i = 0; i < wall.size(); i++) {
        int wy = wall[i].first, wx = wall[i].second;
        //auto [wy, wx] = wall[i];
        int dis = abs(y - wy) + abs(x - wx);
        if (dis <= inklevel) {
            map[wy][wx] = ink[jumpcount];
        }
    }
    inklevel = 0;
    jumpcount++;
}

void charge(int& inklevel) {
    inklevel++;
}



int main()
{
    int I, N, K, inklevel = 0, jumpcount = 0, y, x;
    string ink, order, line;
    vector<pair<int, int>> wall;
    vector<string> map;
    cin >> I >> N >> K;
    cin >> ink;
    for (int i = 0; i < N; i++) {
        cin >> line;
        map.push_back(line);
        for (int j = 0; j < N; j++) {
            if (line[j] == '@') {
                y = i;
                x = j;
            }
            if (line[j] == '#') {
                wall.push_back({ i, j });
            }
        }
    }
    cin >> order;
    for (int i = 0; i < order.length(); i++) {
        if (order[i] == 'j') charge(inklevel);
        else if (order[i] == 'J') {
            jump(wall, map, jumpcount, inklevel, ink, y, x);
            jumpcount %= I;
        }
        else {
            map[y][x] = '.';
            moveplayer(map, y, x, order[i], N);
            map[y][x] = '@';
        }
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << map[i][j];
        }
        cout << "\n";
    }
}

profile
게임 프로그래밍 공부

0개의 댓글