미로 만들기 - 1347

2025. 2. 12. 15:46PS/백준

문제

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

느낀점

방향과 위치 이동을 모두 고려해주어야했기 때문에 상당히 어려웠다. 그리고 처음에는 방향을 반대로 생각해서 예시를 이해못했는데, 그것 때문에 처음에 문제를 잘못 풀었다. 문제를 먼저 제대로 이해하자

풀이

#include <iostream>
#include <vector>
#include <string>

int changeDirection(int direction, char cmd)
{
    if (cmd == 'R')
    {
        if (direction == 0)
        {
            return 3;
        }
        else if (direction == 1)
        {
            return 0;
        }
        else if (direction == 2)
        {
            return 1;
        }
        else if (direction == 3)
        {
            return 2;
        }
    }
    else
    {
        if (direction == 0)
        {
            return 1;
        }
        else if (direction == 1)
        {
            return 2;
        }
        else if (direction == 2)
        {
            return 3;
        }
        else if (direction == 3)
        {
            return 0;
        }
    }
}

int main()
{
    int N, temp;
    std::cin >> N;
    std::string maze;
    std::vector<std::pair<int, int>> coor;
    std::vector<std::pair<int, int>> direction = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // south, east, north, west
    std::vector<std::vector<char>> mazeMap(101, std::vector<char>(101, '#'));
    std::pair<int, int> baseCoor = std::make_pair(50, 50);
    mazeMap[50][50] = '.';
    int xMax = 50, xMin = 50, yMax = 50, yMin = 50, baseDirection = 0;
    std::cin >> maze;

    for (int i = 0; i < N; i++)
    {
        if (maze[i] == 'F')
        {
            int x = baseCoor.first + direction[baseDirection].first;
            int y = baseCoor.second + direction[baseDirection].second;
            if (x <= 50)
            {
                if (xMin >= x)
                {
                    xMin = x;
                }
            }
            else if (x > 50)
            {
                if (xMax < x)
                {
                    xMax = x;
                }
            }
            if (y <= 50)
            {
                if (yMin >= y)
                {
                    yMin = y;
                }
            }
            else if (y > 50)
            {
                if (yMax < y)
                {
                    yMax = y;
                }
            }

            baseCoor.first = x;
            baseCoor.second = y;
            mazeMap[x][y] = '.';
        }
        else
        {
            baseDirection = changeDirection(baseDirection, maze[i]);
        }
    }
    for (int i = yMax; i >= yMin; i--)
    {
        for (int j = xMin; j <= xMax; j++)
        {
            std::cout << mazeMap[j][i];
        }
        std::cout << "\n";
    }

    return 0;
}

'PS > 백준' 카테고리의 다른 글

주식 - 11501  (0) 2025.02.13
한 줄로 서기 - 1138  (0) 2025.02.12
창고 다각형 - 2304  (0) 2025.02.12
참외밭 - 2477  (0) 2025.02.12
결혼식 - 5567  (0) 2025.02.12