hdu 5094 Maze

题意:

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值