Hdu 5094 Maze【状压搜索】

Maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 1349    Accepted Submission(s): 477


Problem Description
This story happened on the background of Star Trek.

Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS.

The captain of Enterprise, James T. Kirk, had to fly to Qo’noS to rescue his deputy. Fortunately, he stole a map of the maze where Spock was put in exactly.

The maze is a rectangle, which has n rows vertically and m columns horizontally, in another words, that it is divided into n*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from current location to next costs 1 second. And he is able to move to next location if and only if:

Next location is adjacent to current Kirk’s location on up or down or left or right(4 directions)
Open door is passable, but locked door is not.
Kirk cannot pass a wall

There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time.

Initial location of Kirk was (1, 1) while Spock was on location of (n, m). Your task is to help Kirk find Spock as soon as possible.
 

Input
The input contains many test cases.

Each test case consists of several lines. Three integers are in the first line, which represent n, m and p respectively (1<= n, m <=50, 0<= p <=10). 
Only one integer k is listed in the second line, means the sum number of gates and walls, (0<= k <=500).

There are 5 integers in the following k lines, represents x i1, y i1, x i2, y i2, g i; when g i >=1, represents there is a gate of type gi between location (x i1, y i1) and (x i2, y i2); when g i = 0, represents there is a wall between location (x i1, y i1) and (x i2, y i2), ( | x i1 - x i2 | + | y i1 - y i2 |=1, 0<= g i <=p )

Following line is an integer S, represent the total number of keys in maze. (0<= S <=50).

There are three integers in the following S lines, represents x i1, y i1 and q i respectively. That means the key type of q i locates on location (x i1, y i1), (1<= q i<=p).
 

Output
Output the possible minimal second that Kirk could reach Spock. 

If there is no possible plan, output -1. 
 

Sample Input
  
  
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
 

Sample Output
  
  
14

题目大意:


现在给你N*M的一个迷宫,其中一共有P种钥匙和门。

现在已知有K个障碍物,障碍物可能是墙也可能是门,如果是墙那么一定不能通过,如果是门,那么如果有钥匙就能通过。

现在有S个地方有钥匙。


问从左上角走到右下角需要的最短时间。


思路:


Bfs+状压水题。


设定vis【x】【y】【tmp】表示到位子(x,y),拥有钥匙的状态为tmp的情况是否走过了。


那么过程维护一下就行。


问题在于障碍物是一条边,连接两个相邻的点,那么我们扩大这个地图,将两个点之间多添加一个点即可。


那么原来的(x,y)现在就是(x*2-1,y*2-1);


扩大了地图之后,要注意(x%2==0,y%2==0)的点是不存在的,即不能走。


注意细节,和位运算。


Ac代码:


#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct node
{
    int x,y,tmp,step;
}now,nex;
int ned[102][102];
int no[102][102];
int have[102][102];
int vis[102][102][(1<<11)];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
int n,m,P;
void Bfs(int x,int y)
{
    int realn=n*2-1;
    int realm=m*2-1;
    memset(vis,0,sizeof(vis));
    now.step=0;
    now.x=x;
    now.y=y;
    now.tmp=have[1][1];
    queue<node>s;
    s.push(now);
    vis[now.x][now.y][now.tmp]=1;
    while(!s.empty())
    {
        now=s.front();
        if(now.x==realn&&now.y==realm)
        {
            printf("%d\n",now.step/2);
            return ;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=1&&nex.x<=realn&&nex.y>=1&&nex.y<=realm&&no[nex.x][nex.y]==0)
            {
                if(nex.x%2==0&&nex.y%2==0)continue;
                nex.step=now.step+1;
                nex.tmp=(now.tmp|have[nex.x][nex.y]);
                if((nex.tmp&ned[nex.x][nex.y])==ned[nex.x][nex.y])
                {
                    if(vis[nex.x][nex.y][nex.tmp]==0)
                    {
                        vis[nex.x][nex.y][nex.tmp]=1;
                        s.push(nex);
                    }
                }
            }
        }
    }
    printf("-1\n");
    return ;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&P))
    {
        memset(vis,0,sizeof(vis));
        memset(no,0,sizeof(no));
        memset(ned,0,sizeof(ned));
        memset(have,0,sizeof(have));
        int realn=n+n-1;
        int realm=m+m-1;
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int x,y,x2,y2,zhonglei;
            scanf("%d%d%d%d%d",&x,&y,&x2,&y2,&zhonglei);
            x=x*2-1;
            y=y*2-1;
            x2=x2*2-1;
            y2=y2*2-1;
            for(int i=x;i<=x2;i++)
            {
                for(int j=y;j<=y2;j++)
                {
                    if(i==x&&j==y)continue;
                    if(i==x2&&j==y2)continue;
                    if(zhonglei==0)
                    {
                        no[i][j]=1;
                        continue;
                    }
                    ned[i][j]|=(1<<zhonglei);
                }
            }
        }
        scanf("%d",&q);
        while(q--)
        {
            int x,y,zhonglei;
            scanf("%d%d%d",&x,&y,&zhonglei);
            x=x*2-1;
            y=y*2-1;
            have[x][y]|=(1<<zhonglei);
        }
        Bfs(1,1);
    }
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值