본문 바로가기

백준

2206 벽 부수고 이동하기

전에 미로찾기 문제에서 벽을 부쉈는데 이 문제도 벽을 부순다..

 

미로찾기 문제에서 북님 코드 함 따라 쳐봐야지 싶었는데 이 문제 풀 때 코드 봤음

 

 

 

그런데 auto [x, y, t] = q.front(); 이 부분에서 에러가 떴다.

 

알고보니 c++17 문법이었다!

 

저게 어떻게 돌아가냐면 큐에서 하나 꺼낸 다음 변수에 줄줄 풀어준다

 

map이나 pair vector도 가능!

 

for (auto [key, value] : mp)

for (auto [x, y] : v)

 

요즘 애들 참 좋은 세상에 산다

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;

int dist[1005][1005][2];
int wall[1005][1005];

int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };

int     main()
{
    int n, m;
    cin >> n >> m;

    memset(dist, -1, sizeof(dist));

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%1d", &wall[i][j]);

    queue<iii> q;
    q.emplace(1, 1, 0);
    dist[1][1][0] = 1;

    while (!q.empty())
    {
        auto [x, y, t] = q.front();
        q.pop();

        if (x == n && y == m)
        {
            printf("%d\n", dist[x][y][t]);
            return 0;
        }

        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];

            if (nx < 1 || nx > n || ny < 1 || ny > m)
                continue;

            int nt = t;
            if (wall[nx][ny] == 1)
                nt++;

            if (nt >= 2 || dist[nx][ny][nt] != -1)
                continue;

            dist[nx][ny][nt] = dist[x][y][t] + 1;
            q.emplace(nx, ny, nt);
        }
    }

    printf("-1\n");

    return 0;
}

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

2358 평행선  (0) 2020.09.25
5060 무글 맵스 [미완]  (0) 2020.09.25
14699 관악산 등산  (0) 2020.09.24
2961 도영이가 만든 맛있는 음식  (0) 2020.09.24
16113 시그널  (0) 2020.09.24