状压+bfs
https://loj.ac/problem/6121
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int maxn =12;
vector<int> key[maxn][maxn];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int door[maxn][maxn][maxn][maxn];
int vis[1<<15][maxn][maxn];
struct node{
int x,y,step,state;
};
int getans(int stx,int sty,int n,int m){
int ststate = 0;
for(int i=0;i<key[stx][sty].size();i++)
ststate |= (key[stx][sty][i]==0? 0: (1<<key[stx][sty][i]));
node tmp = (node){stx,sty,0,ststate};
queue<node> q;
q.push(tmp);
while (!q.empty()) {
node now = q.front();
q.pop();
if(now.x==n && now.y==m) return now.step;
for(int i=0;i<4;i++){
int nx = now.x+dir[i][0];
int ny = now.y+dir[i][1];
if(nx<1 || nx>n || ny<1 || ny>m) continue;
int nstate = now.state ;
for(int j=0;j<key[nx][ny].size();j++)
nstate |= (key[nx][ny][j]==0?0:(1<<key[nx][ny][j]));
if(door[now.x][now.y][nx][ny]!=-1){
int kk =door[now.x][now.y][nx][ny];
if((now.state&(1<<kk))==0) continue;
}
if(vis[nstate][nx][ny]==1) continue;
vis[nstate][nx][ny] = 1;
q.push((node){nx,ny,now.step+1,nstate});
}
}
return 0x3f3f3f3f;
}
int main(int argc, const char * argv[]) {
int n,m,p;
scanf("%d%d%d",&n,&m,&p);
int k;
scanf("%d",&k);
memset(door, -1, sizeof(door));
for(int i=0;i<k;i++){
int x1,y1,x2,y2,g;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);
door[x1][y1][x2][y2] = g;
door[x2][y2][x1][y1] = g;
}
int s;scanf("%d",&s);
for(int i=0;i<s;i++){
int x1,y1,g;
scanf("%d%d%d",&x1,&y1,&g);
key[x1][y1].push_back(g);
}
int diss = getans(1,1,n,m);
if(diss==0x3f3f3f3f){
puts("-1");
}else{
printf("%d\n",diss);
}
return 0;
}