hdu5094(bfs,状态压缩)

154 篇文章 0 订阅
46 篇文章 0 订阅

Maze

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


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

题意:走迷宫从(1,1)走到(n,m),有些墙走不了,有些需要钥匙,钥匙在某些格子里,走到了才能取出来。

思路:刚开始总是没想清楚怎么解决bfs的重复路径问题,一个vis[x][y]根本解决不了问题,后来看完题解顿悟(简直是豁然开朗一下子完全没难度)…后想想以前写过一个这样的类似的拿钥匙的,果然没经过思考看题解仍然是不会,现在终于彻底懂了…不过后来写了wa了,看看别人写的结果发现一个点可能有多把钥匙= =,加上就过了……无语。

//#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值