-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1194.cpp
More file actions
103 lines (78 loc) · 1.96 KB
/
1194.cpp
File metadata and controls
103 lines (78 loc) · 1.96 KB
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
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include <string>
using namespace std;
struct P
{
int x, y, move, key;
};
char arr[51][51];
int N, M;
int visited[51][51][1 << 6];
int movepos[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
const int INF = 123456789;
int minV = INF;
bool canmove(int x, int y) {
if (x < 0 || x >= M)return false;
if (y < 0 || y >= N)return false;
if (arr[y][x] == '#')return false;
return true;
}
int main(void)
{
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> N >> M;
P start;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
if (arr[i][j] == '0') {
start = { j,i,0,0 };
arr[i][j] = '.';
}
}
queue<P> q;
visited[start.y][start.x][start.key] = 1;
q.push(start);
while (!q.empty()) {
P curr = q.front(); q.pop();
//'1'로바꿔야함
if (arr[curr.y][curr.x] == '1') {
if (minV > curr.move)minV = curr.move;
break;
}
for (int i = 0; i < 4; i++) {
int gox, goy;
gox = curr.x + movepos[i][0];
goy = curr.y + movepos[i][1];
if (canmove(gox, goy)) {
if (!visited[goy][gox][curr.key]) {
if (arr[goy][gox] >= 'a' && arr[goy][gox] <= 'f') {
visited[goy][gox][curr.key] = 1;
int idx = arr[goy][gox] - 'a';
int getKey = curr.key | (1 << idx);
visited[goy][gox][getKey] = 1;
q.push(P{ gox,goy,curr.move + 1,getKey });
}
else if (arr[goy][gox] >= 'A' && arr[goy][gox] <= 'F') {
int idx = arr[goy][gox] - 'A';
if (curr.key & (1 << idx)) {
visited[goy][gox][curr.key] = 1;
q.push(P{ gox,goy,curr.move + 1,curr.key });
}
}
// . 이거나 1 이거나
else {
visited[goy][gox][curr.key] = 1;
q.push(P{ gox,goy,curr.move + 1,curr.key });
}
}
}
}
}
if (minV == INF)cout << "-1";
else cout << minV;
}