백준 15684 사다리 조작 ❌

CJB_ny·2023년 2월 21일
0

백준

목록 보기
82/104
post-thumbnail

사다리 조작

내풀이 (틀림)


문제의 상태값을 기반으로 visited[1][1] 이렇게 설정을 해주어야한다.

백트레킹을 하는데 모든 맵을 다 직접 이동할 필요없다. => dy, dx필요없고

현재 모든 사다리를 순회하면서

내 위치에서 ++, -- 를해서 다시 처음 출발했던 x좌표랑 같은지 보는 것이다.

같다면 true, 아니라면 false

이게 Check에서 확인해주는 부분이다.

핵심은 Check()함수였던거같다

현재위치를 어떻게 확인할 줄 몰랐었는데 이런식으로 확인을 하도록 하자.

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
using namespace std;
#define endl "\n"

const int INF = 987654321;
int n, m, h, a, b, ret = INF, visited[34][34];

bool Check()
{
    for (int i = 1; i <= n; ++i)
    {
        int start = i;
        for (int j = 1; j <= h; ++j)
        {
            if (visited[j][start]) ++start;
            else if (visited[j][start - 1]) --start;
        }
        if (start != i) return false;
    }
    return true;
}

void Go(int here, int cnt)
{
    if (cnt > 3 || cnt >= ret) return;
    if (Check())
    {
        ret = min(ret, cnt); return;
    }
    for (int i = here; i <= h; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            if (visited[i][j] || visited[i][j - 1] || visited[i][j + 1]) continue;
            visited[i][j] = 1;
            Go(i, cnt + 1);
            visited[i][j] = 0;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> m >> h;
    for (int i = 0; i < m; ++i)
    {
        cin >> a >> b;
        visited[a][b] = 1;
    }

    Go(1, 0);
    cout << ((ret == INF) ? -1 : ret) << endl;
    
    return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글