Programmers 60063 블록 이동하기 [2020 KAKAO BLIND RECRUITMENT]

문제

https://school.programmers.co.kr/learn/courses/30/lessons/60063

로봇개발자 "무지"​는 한 달 앞으로 다가온 "카카오배 로봇경진대회"에 출품할 로봇을 준비하고 있습니다.

준비 중인 로봇은 2 x 1 크기의 로봇으로 "무지"는 "0"​과 "1"​로 이루어진 N x N 크기의 지도에서 2 x 1 크기인 로봇을 움직여

(N, N) 위치까지 이동 할 수 있도록 프로그래밍을 하려고 합니다. 로봇이 이동하는 지도는 가장 왼쪽, 상단의 좌표를 (1, 1)​로 하며

지도 내에 표시된 숫자 "0"​은 빈칸을 "1"​은 벽을 나타냅니다. 로봇은 벽이 있는 칸 또는 지도 밖으로는 이동할 수 없습니다.

로봇은 처음에 아래 그림과 같이 좌표 (1, 1) 위치에서 가로방향으로 놓여있는 상태로 시작하며, 앞뒤 구분없이 움직일 수 있습니다.

로봇이 움직일 때는 현재 놓여있는 상태를 유지하면서 이동합니다.

예를 들어, 위 그림에서 오른쪽으로 한 칸 이동한다면 (1, 2), (1, 3) 두 칸을 차지하게 되며,

아래로 이동한다면 (2, 1), (2, 2) 두 칸을 차지하게 됩니다.

로봇이 차지하는 두 칸 중 어느 한 칸이라도 (N, N) 위치에 도착하면 됩니다.

로봇은 다음과 같이 조건에 따라 회전이 가능합니다.

위 그림과 같이 로봇은 90도씩 회전할 수 있습니다.

단, 로봇이 차지하는 두 칸 중, 어느 칸이든 축이 될 수 있지만,

회전하는 방향(축이 되는 칸으로부터 대각선 방향에 있는 칸)에는 벽이 없어야 합니다.

로봇이 한 칸 이동하거나 90도 회전하는 데는 걸리는 시간은 정확히 1초 입니다.

"0"​과 "1"​로 이루어진 지도인 board가 주어질 때,

로봇이 (N, N) 위치까지 이동하는데 필요한 최소 시간을 return 하도록 solution 함수를 완성해주세요.

제한 사항

board의 한 변의 길이는 5 이상 100 이하입니다.

board의 원소는 0 또는 1입니다.

로봇이 처음에 놓여 있는 칸 (1, 1), (1, 2)는 항상 0으로 주어집니다.

로봇이 항상 목적지에 도착할 수 있는 경우만 입력으로 주어집니다.

입출력 예

board result
[[0, 0, 0, 1, 1],[0, 0, 0, 1, 0],[0, 1, 0, 1, 1],[1, 1, 0, 0, 1],[0, 0, 0, 0, 0]] 7

입출력 예 설명

문제에 주어진 예시와 같습니다.

로봇이 오른쪽으로 한 칸 이동 후, (1, 3) 칸을 축으로 반시계 방향으로 90도 회전합니다.

다시, 아래쪽으로 3칸 이동하면 로봇은 (4, 3), (5, 3) 두 칸을 차지하게 됩니다.

이제 (5, 3)을 축으로 시계 방향으로 90도 회전 후, 오른쪽으로 한 칸 이동하면 (N, N)에 도착합니다.

따라서 목적지에 도달하기까지 최소 7초가 걸립니다.


통과된 코드

cpp
#include <string>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

// 탐색하는 방향 설정 =>  상, 하 ,좌 ,우
int dxdy[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

bool _MapArr[101][101][2];

// 로봇의 위치 및 현재 시간
tuple<int, int, int, int, int> _RobotPos;
// bfs 탐색을 위한 q
queue<tuple<int, int, int, int, int>> _RobotPosQ;

int solution(vector<vector<int>> board) {
    int answer = 0;
    int _MapMax = board.size();
    pair<int, int> _goal = make_pair(_MapMax - 1, _MapMax - 1); // 목표를 설정

    _RobotPos = make_tuple(0, 0, 0, 1, 0); // 로봇의 위치 초기값
    _RobotPosQ.push(_RobotPos); // 로봇의 시작를 큐에 넣기
    _MapArr[0][0][0] = true; // 방문 마킹
    _MapArr[0][1][0] = true;
    // board 는 0,0 에서 시작
    // 마킹은 1,1에서 시작


    // bfs 탐색 시작
    while (_RobotPosQ.size() > 0) {
        _RobotPos = _RobotPosQ.front();
        _RobotPosQ.pop();
        pair<int, int> _CntPosOne = make_pair(get<0>(_RobotPos), get<1>(_RobotPos)); // 로봇의 현재 x, y 위치 1
        pair<int, int> _CntPosTwo = make_pair(get<2>(_RobotPos), get<3>(_RobotPos)); // 로봇의 현재 x, y 위치 2
        int _cnttime = get<4>(_RobotPos);
        if (_CntPosOne == _goal || _CntPosTwo == _goal) {
            answer = _cnttime;
            break;
        }
        bool _robotstatus; // true 세로, false 가로
        // 상 하 좌 우를 탐색
        for (int i = 0; i < 4; i++) {

            pair<int, int> _TempPosOne, _TempPosTwo;
            _TempPosOne.first = _CntPosOne.first + dxdy[i][0];
            _TempPosOne.second = _CntPosOne.second + dxdy[i][1];
            _TempPosTwo.first = _CntPosTwo.first + dxdy[i][0];
            _TempPosTwo.second = _CntPosTwo.second + dxdy[i][1];

            // 가로 세로 비교
            if (_CntPosOne.first + 1 == _CntPosTwo.first || _CntPosOne.first - 1 == _CntPosTwo.first) _robotstatus = true; // 세로
            else _robotstatus = false;

            // 벽이라면 넘어간다.
            if (_TempPosOne.first < 0 || _TempPosOne.first >= _MapMax || _TempPosOne.second < 0 || _TempPosOne.second >= _MapMax) continue;
            if (_TempPosTwo.first < 0 || _TempPosTwo.first >= _MapMax || _TempPosTwo.second < 0 || _TempPosTwo.second >= _MapMax) continue;
            if (board[_TempPosOne.first][_TempPosOne.second] == 1 || board[_TempPosTwo.first][_TempPosTwo.second] == 1) continue;

            // 두 부분이 방문한 적이 있다면 넘어간다.
            if (_MapArr[_TempPosOne.first][_TempPosOne.second][_robotstatus] == true && _MapArr[_TempPosTwo.first][_TempPosTwo.second][_robotstatus] == true) continue;

            _RobotPos = make_tuple(_TempPosOne.first, _TempPosOne.second, _TempPosTwo.first, _TempPosTwo.second, _cnttime + 1);
            _RobotPosQ.push(_RobotPos); // 로봇의 시작를 큐에 넣기
            _MapArr[_TempPosOne.first][_TempPosOne.second][_robotstatus] = true; // 방문 마킹
            _MapArr[_TempPosTwo.first][_TempPosTwo.second][_robotstatus] = true;

        }

        // 방향 전환
        if (!_robotstatus) { // 가로
            if (_CntPosOne.first + 1 < _MapMax && board[_CntPosOne.first + 1][_CntPosOne.second] != 1 && board[_CntPosTwo.first + 1][_CntPosTwo.second] != 1) {
                if (_MapArr[_CntPosTwo.first + 1][_CntPosTwo.second][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosTwo.first + 1, _CntPosTwo.second, _CntPosTwo.first, _CntPosTwo.second, _cnttime + 1);
                    _MapArr[_CntPosTwo.first + 1][_CntPosTwo.second][!_robotstatus] = true;
                    _MapArr[_CntPosTwo.first][_CntPosTwo.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
            if (_CntPosOne.first - 1 >= 0 && board[_CntPosOne.first - 1][_CntPosOne.second] != 1 && board[_CntPosTwo.first - 1][_CntPosTwo.second] != 1) {
                if (_MapArr[_CntPosTwo.first - 1][_CntPosTwo.second][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosTwo.first - 1, _CntPosTwo.second, _CntPosTwo.first, _CntPosTwo.second, _cnttime + 1);
                    _MapArr[_CntPosTwo.first - 1][_CntPosTwo.second][!_robotstatus] = true;
                    _MapArr[_CntPosTwo.first][_CntPosTwo.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
            if (_CntPosTwo.first + 1 < _MapMax && board[_CntPosTwo.first + 1][_CntPosTwo.second] != 1 && board[_CntPosOne.first + 1][_CntPosOne.second] != 1) {
                if (_MapArr[_CntPosOne.first + 1][_CntPosOne.second][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosOne.first + 1, _CntPosOne.second, _CntPosOne.first, _CntPosOne.second, _cnttime + 1);
                    _MapArr[_CntPosOne.first + 1][_CntPosOne.second][!_robotstatus] = true;
                    _MapArr[_CntPosOne.first][_CntPosOne.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
            if (_CntPosTwo.first - 1 >= 0 && board[_CntPosTwo.first - 1][_CntPosTwo.second] != 1 && board[_CntPosOne.first - 1][_CntPosOne.second] != 1) {
                if (_MapArr[_CntPosOne.first - 1][_CntPosOne.second][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosOne.first - 1, _CntPosOne.second, _CntPosOne.first, _CntPosOne.second, _cnttime + 1);
                    _MapArr[_CntPosOne.first - 1][_CntPosOne.second][!_robotstatus] = true;
                    _MapArr[_CntPosOne.first][_CntPosOne.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
        }
        else {
            if (_CntPosOne.second + 1 < _MapMax && board[_CntPosOne.first][_CntPosOne.second + 1] != 1 && board[_CntPosTwo.first][_CntPosTwo.second + 1] != 1) {
                if (_MapArr[_CntPosTwo.first][_CntPosTwo.second + 1][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosTwo.first, _CntPosTwo.second + 1, _CntPosTwo.first, _CntPosTwo.second, _cnttime + 1);
                    _MapArr[_CntPosTwo.first][_CntPosTwo.second + 1][!_robotstatus] = true;
                    _MapArr[_CntPosTwo.first][_CntPosTwo.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
            if (_CntPosOne.second - 1 >= 0 && board[_CntPosOne.first][_CntPosOne.second - 1] != 1 && board[_CntPosTwo.first][_CntPosTwo.second - 1] != 1) {
                if (_MapArr[_CntPosTwo.first][_CntPosTwo.second - 1][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosTwo.first, _CntPosTwo.second - 1, _CntPosTwo.first, _CntPosTwo.second, _cnttime + 1);
                    _MapArr[_CntPosTwo.first][_CntPosTwo.second - 1][!_robotstatus] = true;
                    _MapArr[_CntPosTwo.first][_CntPosTwo.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
            if (_CntPosTwo.second + 1 < _MapMax && board[_CntPosTwo.first][_CntPosTwo.second + 1] != 1 && board[_CntPosOne.first][_CntPosOne.second + 1] != 1) {
                if (_MapArr[_CntPosOne.first][_CntPosOne.second + 1][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosOne.first, _CntPosOne.second + 1, _CntPosOne.first, _CntPosOne.second, _cnttime + 1);
                    _MapArr[_CntPosOne.first][_CntPosOne.second + 1][!_robotstatus] = true;
                    _MapArr[_CntPosOne.first][_CntPosOne.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }
            if (_CntPosTwo.second - 1 >= 0 && board[_CntPosTwo.first][_CntPosTwo.second - 1] != 1 && board[_CntPosOne.first][_CntPosOne.second - 1] != 1) {
                if (_MapArr[_CntPosOne.first][_CntPosOne.second - 1][!_robotstatus] == false) {
                    _RobotPos = make_tuple(_CntPosOne.first, _CntPosOne.second - 1, _CntPosOne.first, _CntPosOne.second, _cnttime + 1);
                    _MapArr[_CntPosOne.first][_CntPosOne.second - 1][!_robotstatus] = true;
                    _MapArr[_CntPosOne.first][_CntPosOne.second][!_robotstatus] = true;
                    _RobotPosQ.push(_RobotPos);
                }
            }

        }

    }

    return answer;
}