마이 플밍 블로그

백준 2206 벽 부수고 이동하기 본문

카테고리 없음

백준 2206 벽 부수고 이동하기

레옹 2021. 7. 30. 18:21
#include<cstdio>
#include<iostream>
#include<queue>
#include<stack>
#include <utility>;
#include <string>
using namespace std;

int M, N;
int Map[1001][1001];
int Dst[1001][1001][2];

int Dx[4] = { -1,0,1,0 };
int Dy[4] = { 0,-1,0,1 };
int Day = 0;
int NotRipeTomatoNum = 0;

int BreakWall = 1;
char arr[1001];
bool isInside(int x, int y) {
    if (0 <= x && x < M && 0 <= y && y < N) {
        return true;
    }
    return false;
}

int RipeTomatoes() {
    int RipeTomatoIndex = 0;
    vector<int> RipeTomatoXpos;
    vector<int> RipeTomatoYpos;

    queue<pair<pair<int, int>, int>> nodeQueue;
    nodeQueue.push(make_pair(make_pair(0, 0), 1));
    Dst[0][0][1] = 1;

    while (!nodeQueue.empty()) {
        pair<int, int> node = nodeQueue.front().first;
        int block = nodeQueue.front().second;
        nodeQueue.pop();
        int x = node.first;
        int y = node.second;

        if (y == N - 1 && x == M - 1) {
            return Dst[x][y][block];
        }

        for (int i = 0; i < 4; i++) {
            int searchX = x + Dx[i];
            int searchY = y + Dy[i];
            if (isInside(searchX, searchY)) {
                if (Map[searchX][searchY] == 1 && block) {
                    Dst[searchX][searchY][block - 1] = Dst[x][y][block] + 1;
                    nodeQueue.push(make_pair(make_pair(searchX, searchY), block - 1));
                }

                else if (Map[searchX][searchY] == 0 && Dst[searchX][searchY][block] == 0) {
                    Dst[searchX][searchY][block] = Dst[x][y][block] + 1;
                    nodeQueue.push(make_pair(make_pair(searchX, searchY), block));
                }
            }
        }
    }
    return -1;
}
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> N >> M;

    vector<int> RipeTomatoX;
    vector<int> RipeTomatoY;

    for (int y = 0; y < N; y++) {
        for (int i = 0; i < M; i++) {
            cin >> arr[i];
        }
        for (int x = 0; x < M; x++) {
            Map[x][y] = (int)arr[x] - 48;
        }
    }
    cout << RipeTomatoes();
    return 0;
}