不用搞得很麻烦,直接加一维当前手里的钥匙就好了
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int,int>;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int n,q,m;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int g[12][12][12][12];
int key[12][12];
int dist[12][12][1<<11];
void bfs()
{
memset(dist,0x3f,sizeof dist);
queue<array<int,3>>q;
q.push({1,1,key[1][1]});
dist[1][1][key[1][1]] = 0;
while(q.size()){
auto t = q.front();
q.pop();
int x = t[0],y = t[1],keys = t[2];
//cout<<x<<" "<<y<<"??\n";
if(x==n&&y==m){
cout<<dist[x][y][keys];return;
}
for(int i=0;i<4;i++){
int temx = dx[i]+x,temy = dy[i]+y;
if(temx<1||temx>n||temy<1||temy>m)continue;
if(!g[x][y][temx][temy])continue;
int state = 0;
if(g[x][y][temx][temy]!=-1)state = (1<<g[x][y][temx][temy]);
if((keys|state)!=keys)continue;
state = (keys|key[temx][temy]);
if(dist[temx][temy][state]>dist[x][y][keys]+1){
dist[temx][temy][state] = dist[x][y][keys]+1;
q.push({temx,temy,state});
}
}
}
cout<<-1;return;
}
void solve()
{
cin>>n>>m>>q;
cin>>q;
memset(g,-1,sizeof g);
while(q--){
int a,b,c,d,e;cin>>a>>b>>c>>d>>e;
g[a][b][c][d] = e;
g[c][d][a][b] = e;
}
cin>>q;
while(q--){
int a,b,c;cin>>a>>b>>c;
key[a][b]|=(1<<c);
}
bfs();
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
//cin>>_;
_ = 1;
while(_--)solve();
return 0;
}