-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1194.cpp
More file actions
70 lines (69 loc) · 1.86 KB
/
1194.cpp
File metadata and controls
70 lines (69 loc) · 1.86 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
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
char d[51][51];
int v[51][51][1<<6];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,sr,sc;
cin>>n>>m;
for(int i=0;i<n;i++){
string str;
cin>>str;
for(int j=0;j<m;j++){
d[i][j]=str[j];
if(d[i][j]=='0'){
sr=i;
sc=j;
}
}
}
memset(v,-1,sizeof(v));
queue<pair<pair<int,int>,int>> q;
v[sr][sc][0]=0;
q.push({{sr,sc},0});
while(!q.empty()){
int cr = q.front().first.first;
int cc = q.front().first.second;
int key = q.front().second;
int cost = v[cr][cc][key];
if(d[cr][cc]=='1'){
cout<<cost;
return 0;
}
q.pop();
if(d[cr][cc]>='a'&&d[cr][cc]<='f'){
// cout<<"!!";
key=key|(1<<(d[cr][cc]-'a'));
v[cr][cc][key]=cost;
}
// printf("cr : %d cc : %d key : %d cost : %d \n",cr,cc,key,cost);
for(int i=0;i<4;i++){
int nr = cr+dx[i];
int nc = cc+dy[i];
if(nr>=0&&nr<n&&nc>=0&&nc<m&&d[nr][nc]!='#'){
if(d[nr][nc]>='A'&&d[nr][nc]<='F'){
if(key&(1<<(d[nr][nc]-'A')))
{
if(v[nr][nc][key]==-1||v[nr][nc][key]>v[cr][cc][key]+1){
v[nr][nc][key]=cost+1;
q.push({{nr,nc},key});
}
}
}
else if(v[nr][nc][key]==-1||v[nr][nc][key]>v[cr][cc][key]+1){
v[nr][nc][key]=cost+1;
q.push({{nr,nc},key});
}
}
}
}
cout<<-1;
return 0;
}