-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light-oj-1039.cpp
64 lines (61 loc) · 1.57 KB
/
Light-oj-1039.cpp
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
#include "bits/stdc++.h"
using namespace std;
string S,E;
int N;
map< string, int >D;
set<string>Forbidden;
int bfs(){
if(Forbidden.find(S)!=Forbidden.end() || Forbidden.find(E)!=Forbidden.end())return -1;
queue < string >Q;
Q.push(S);
D[S]=0;
while(!Q.empty()){
string U=Q.front();
Q.pop();
if(U==E)return D[U];
for(int i=0; i<3; i++){
string tmp=U;
tmp[i]++;
if(tmp[i]>'z')tmp[i]='a';
if(Forbidden.find(tmp)==Forbidden.end() && D[tmp]==0){
if(tmp==E)return D[U]+1;
D[tmp]=D[U]+1;
Q.push(tmp);
}
tmp=U;
tmp[i]--;
if(tmp[i]<'a')tmp[i]='z';
if(Forbidden.find(tmp)==Forbidden.end() && D[tmp]==0){
if(tmp==E)return D[U]+1;
D[tmp]=D[U]+1;
Q.push(tmp);
}
}
}
return -1;
}
int main(){
int t,cs=1;
scanf("%d",&t);
while(t--){
cin >> S >> E;
scanf("%d",&N);
while(N--){
string a,b,c,tmp="";
cin >> a >>b >>c;
for(int i=0; i<a.size(); i++){
for(int j=0; j<b.size(); j++){
for(int k=0; k<c.size(); k++){
tmp="abc";
tmp[0]=a[i];tmp[1]=b[j];tmp[2]=c[k];
Forbidden.insert(tmp);
}
}
}
}
printf("Case %d: %d\n",cs++,bfs());
Forbidden.clear();
D.clear();
}
return 0;
}