题意:
n*m大的迷宫 ,有p种钥匙。钥匙最多有10种。
然后一个k,然后k行表示 (x1,y1),(x2,y2)直接有门或者墙。 如果g==0 ,就是有墙, 如果g>0 表示有门,且门需要第g把钥匙才能开。
然后下来一个s,然后s行,表示(x,y)这个点有 第g把钥匙。
问从(1,1)到(n,m)最少几步。
输入:
4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1
输出:
14
题解:
如果没有钥匙?
现在有了钥匙,我们就记录每一步所拥有钥匙的状态
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<map>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<deque>
using namespace std;
typedef long long ll;
struct data
{
int x,y,step,zt;
data (int i,int j,int s,int z)
{
x=i,y=j,step=s,zt=z;
}
};
int tu[55][55][55][55],ys[55][55];
bool vis[55][55][1<<11];
int fx[4][2]= {{-1,0},{0,-1},{1,0},{0,1}};
int n,m,p;
int bfs()
{
memset(vis,0,sizeof(vis));
data tem(1,1,0,0);
queue<data>q;
q.push(tem);
while(!q.empty())
{
data tt=q.front();
//cout<<tt.x<<" "<<tt.y<<" "<<tt.step<<" ";
//int hhhhh=tt.zt;
//while(hhhhh)cout<<hhhhh%2,hhhhh/=2;cout<<endl;
q.pop();
if(tt.x==n&&tt.y==m)
return tt.step;
for(int i=0; i<4; i++)
{
int x=tt.x+fx[i][0],y=tt.y+fx[i][1];
if(x>=1&&x<=n&&y>=1&&y<=m&&tu[tt.x][tt.y][x][y]!=0)
{
if(tu[tt.x][tt.y][x][y]==-1)
{
if(!ys[x][y]&&!vis[x][y][tt.zt])
{
vis[x][y][tt.zt]=1;
if(x==n&&y==m)return tt.step+1;
data ttt(x,y,tt.step+1,tt.zt);
q.push(ttt);
}
else if(ys[x][y]&&!vis[x][y][tt.zt|ys[x][y]])
{
vis[x][y][tt.zt|ys[x][y]]=1;
if(x==n&&y==m)return tt.step+1;
data ttt(x,y,tt.step+1,tt.zt|ys[x][y]);
q.push(ttt);
}
}
else if(tu[tt.x][tt.y][x][y]!=-1)
{
int xi=(1<<(tu[tt.x][tt.y][x][y]-1));
if(xi&tt.zt)
{
if(!ys[x][y]&&!vis[x][y][tt.zt])
{
if(x==n&&y==m)return tt.step+1;
vis[x][y][tt.zt]=1;
data ttt(x,y,tt.step+1,tt.zt);
q.push(ttt);
}
else if(ys[x][y]&&!vis[x][y][tt.zt|ys[x][y]])
{
if(x==n&&y==m)return tt.step+1;
vis[x][y][tt.zt|ys[x][y]]=1;
data ttt(x,y,tt.step+1,tt.zt|ys[x][y]);
q.push(ttt);
}
}
}
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&p))
{
memset(tu,-1,sizeof(tu));
memset(ys,0,sizeof(ys));
int k;
scanf("%d",&k);
while(k--)
{
int x1,x2,y1,y2,type;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&type);
tu[x1][y1][x2][y2]=tu[x2][y2][x1][y1]=type;
}
int s;
scanf("%d",&s);
while(s--)
{
int xx,yy,type;
scanf("%d%d%d",&xx,&yy,&type);
ys[xx][yy]|=(1<<(type-1));
}
printf("%d\n",bfs());
}
return 0;
}