본문 바로가기

BOJ/C++

달이 차오른다, 가자

 

*현재 가지고 있는 열쇠의 상태를 비트형태로 보존이 핵심 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <vector>
#include <queue>
#define INF 987654321
using namespace std;
int n, m, ans = INF;
char map[51][51];
typedef pair<intint> pp;
typedef pair<pp, pp> pppp;
queue<pppp> q;
int dx[4= {-1100}, dy[4= {00-11};
bool visited[51][51][1 << 6];
//해당 열쇠를 가지고 들렀는지 아닌지 확인
bool check(int x, int y)
{
    if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '#')
        return false;
    return true;
}
void bfs()
{
    while (!q.empty())
    {
        int size = q.size();
        while (size--)
        {
            int x = q.front().second.first;
            int y = q.front().second.second;
            int state = q.front().first.first;
            int cnt = q.front().first.second;
            q.pop();
 
           
            if (map[x][y] == '1')
            {
                ans = min(ans, cnt);
                continue;
            }
 
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
 
                if (check(nx, ny))
                {
                    //지금 방문한 곳이 문이면
                    if (!visited[nx][ny][state])
                    {
                        if ('A' <= map[nx][ny] && map[nx][ny] <= 'F')
                        {
                            //열쇠가 있으면 통과
                            int tmp = map[nx][ny] - 'A';
                            
                            if ((state & (1<<tmp))) {
                                //지나갈 수 있다. 
                                visited[nx][ny][state] = true;
                                q.push(pppp(pp(state, cnt+1), pp(nx, ny)));
                            }
                        }
                        //지금 방문한 곳이 열쇠이면
                        //열쇠 겟
                        else if ('a' <= map[nx][ny] && map[nx][ny] <= 'f')
                        {
                            int tmp = map[nx][ny] - 'a';
 
                            visited[nx][ny][state | (1<<tmp)] = true;
                            q.push(pppp(pp(state | (1<<tmp), cnt+1), pp(nx, ny)));
                        }
                        else {
                            visited[nx][ny][state] = true;
                            q.push(pppp(pp(state, cnt+1), pp(nx, ny)));
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> map[i][j];
 
            if (map[i][j] == '0')
            { //민식이의 위치
                map[i][j] = '.';
                visited[i][j][0= true;
                q.push(pppp(pp(00), pp(i, j)));
            }
        }
    }
 
    bfs();
 
    if (ans == INF)
        cout << -1 << '\n';
    else
        cout << ans << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'BOJ > C++' 카테고리의 다른 글

공주님을 구해라!  (0) 2020.02.28
알고스팟  (0) 2020.02.28
Two Dots  (0) 2020.02.28
로봇  (0) 2020.02.28
점프 점프  (0) 2020.02.27