2014上海全国邀请赛Maze

Maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 517    Accepted Submission(s): 187
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 xi1, yi1, xi2, yi2, gi; when gi >=1, represents there is a gate of type gi between location (xi1, yi1) and (xi2, yi2); when gi = 0, represents there is a wall between location (xi1, yi1) and (xi2, yi2), ( | xi1 - xi2 | + | yi1 - yi2 |=1, 0<= gi <=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 xi1, yi1 and qi respectively. That means the key type of qi locates on location (xi1, yi1), (1<= qi<=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
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5094
题目大意:在一个n*m的矩阵里,在相邻的两个格子间可能有门或者墙或者都要,而门上有锁,锁需要对应的钥匙才能开,在一些格子里会有钥匙。求从[1][1]到[n][m]需要走的最小的步数,走不到输出-1。
题目分析:两个格子间的门可能有多个锁,但是也可能有墙,有墙则一定不能通过;一个格子可能有多把钥匙;钥匙的有无可以用二进制的状态压缩储存。存好后就只是一个简单的宽搜了。
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=(1<<31)-10;
struct node {
    int x,y,z,w;
};
queue<node>q;
int yk[16],ma[51][51],f[51][51][51][51],bo[51][51][2100];
int main()
{
    int a,b,c,d,e,i,j,k,n,m,p,s;
    node xx,hh;
    yk[0]=1;for (i=1;i<=15;i++) yk[i]=yk[i-1]*2;
    while (~scanf("%d%d%d", &n, &m, &p)) {
        scanf("%d", &k);
        memset(f,0,sizeof(f));
        memset(ma,0,sizeof(ma));
        memset(bo,0,sizeof(bo));
        for (i=0;i<k;i++) {
            scanf("%d%d%d%d%d", &a, &b, &c ,&d, &e);
            f[a][b][c][d]=f[a][b][c][d]|yk[e];
            f[c][d][a][b]=f[c][d][a][b]|yk[e];
        }
        scanf("%d", &s);
        for (i=0;i<s;i++) {
            scanf("%d%d%d", &a, &b, &k);
            ma[a][b]=ma[a][b]|yk[k];
        }
        while (!q.empty()) q.pop();
        bo[1][1][ma[1][1]]=1;
        xx.x=1;xx.y=1;xx.z=0;xx.w=ma[1][1];q.push(xx);
        while (!q.empty()) {
            xx=q.front();
            if (xx.x==n&&xx.y==m) {
                printf("%d\n", xx.z);break;
            }
            if (xx.x>1&&(!(f[xx.x][xx.y][xx.x-1][xx.y]&yk[0])||f[xx.x][xx.y][xx.x-1][xx.y]==0)&&!bo[xx.x-1][xx.y][xx.w|ma[xx.x-1][xx.y]]&&((xx.w&f[xx.x][xx.y][xx.x-1][xx.y])==f[xx.x][xx.y][xx.x-1][xx.y])) {
                bo[xx.x-1][xx.y][xx.w|ma[xx.x-1][xx.y]]=1;
                hh.x=xx.x-1;hh.y=xx.y;hh.z=xx.z+1;hh.w=xx.w|ma[xx.x-1][xx.y];q.push(hh);
            }
            if (xx.x<n&&(!(f[xx.x][xx.y][xx.x+1][xx.y]&yk[0])||f[xx.x][xx.y][xx.x+1][xx.y]==0)&&!bo[xx.x+1][xx.y][xx.w|ma[xx.x+1][xx.y]]&&((xx.w&f[xx.x][xx.y][xx.x+1][xx.y])==f[xx.x][xx.y][xx.x+1][xx.y])) {
                bo[xx.x+1][xx.y][xx.w|ma[xx.x+1][xx.y]]=1;
                hh.x=xx.x+1;hh.y=xx.y;hh.z=xx.z+1;hh.w=xx.w|ma[xx.x+1][xx.y];q.push(hh);
            }
            if (xx.y>1&&(!(f[xx.x][xx.y][xx.x][xx.y-1]&yk[0])||f[xx.x][xx.y][xx.x][xx.y-1]==0)&&!bo[xx.x][xx.y-1][xx.w|ma[xx.x][xx.y-1]]&&((xx.w&f[xx.x][xx.y][xx.x][xx.y-1])==f[xx.x][xx.y][xx.x][xx.y-1])) {
                bo[xx.x][xx.y-1][xx.w|ma[xx.x][xx.y-1]]=1;
                hh.x=xx.x;hh.y=xx.y-1;hh.z=xx.z+1;hh.w=xx.w|ma[xx.x][xx.y-1];q.push(hh);
            }
            if (xx.y<m&&(!(f[xx.x][xx.y][xx.x][xx.y+1]&yk[0])||f[xx.x][xx.y][xx.x][xx.y+1]==0)&&!bo[xx.x][xx.y+1][xx.w|ma[xx.x][xx.y+1]]&&((xx.w&f[xx.x][xx.y][xx.x][xx.y+1])==f[xx.x][xx.y][xx.x][xx.y+1])) {
                bo[xx.x][xx.y+1][xx.w|ma[xx.x][xx.y+1]]=1;
                hh.x=xx.x;hh.y=xx.y+1;hh.z=xx.z+1;hh.w=xx.w|ma[xx.x][xx.y+1];q.push(hh);
            }
            q.pop();
        }
        if (q.empty()) printf("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值