ouc shanghairegion#1E

E - Maze

Time Limit:1000MS     Memory Limit:100000KB     64bit IO Format:%I64d & %I64u

Submit Status

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

orz陶神,代码简短有力,学习了。学长是用状态压缩来存钥匙的,值得学习,上大神代码,无限YM

/*
*Author: Ouc_Sky
*Problom: E(Shanghai2014)
*Algorithm: zhuangya dp
*Time: 2014/11/20
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define debug 0

using namespace std;

const int MAXN = 52;

struct node
{
    int x,y,key,step;

};

int n,m,p,k,s;
int ans;
int gate[MAXN][MAXN][MAXN][MAXN];
int key[MAXN][MAXN];
bool vis[MAXN][MAXN][1<<11];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};    //right left down up

int main()
{
    while(~scanf("%d%d%d",&n,&m,&p))
    {
            memset(gate,-1,sizeof(gate));
            memset(key,0,sizeof(key));
            memset(vis,false,sizeof(vis));
            ans = -1;
            scanf("%d",&k);
            for(int i=0;i<k;i++)
            {
                int x1,y1,x2,y2,gi;
                scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&gi);
                gate[x1][y1][x2][y2] = gi;
                gate[x2][y2][x1][y1] = gi;
            }
            if (debug) printf("input2 Yes\n");
            scanf("%d",&s);
            for(int i=0;i<s;i++)
            {
                int x,y,q;
                scanf("%d%d%d",&x,&y,&q);
                key[x][y] = (1<<(q-1)) | key[x][y];
            }
            queue<node> que;
            struct node next = {1,1,0,0};
            que.push(next);

            while(!que.empty())
            {
                struct node now = que.front();
                que.pop();

                if(now.x == n && now.y == m)
                {
                    ans = now.step;
                    break;
                }

                now.key  = key[now.x][now.y] | now.key;

                for(int i=0;i<4;i++)
                {
                    next.x = now.x+dx[i];
                    next.y = now.y+dy[i];
                    if(now.x >= 1 && now.x <= n && now.y >= 1 && now.y <= m)
                    {
                        int thisGate = gate[now.x][now.y][next.x][next.y];
                        if( thisGate > 0 && (now.key & (1<<(thisGate-1) ))
                           || (thisGate == -1))
                        {
                            if (!vis[next.x][next.y][now.key])
                            {
                                vis[next.x][next.y][now.key] = true;
                                next.key = now.key;
                                next.step = now.step +1;
                                que.push(next);
                            }
                        }
                    }
                }
            }
        printf("%d\n",ans);
    }//<end of while(!scanf)>
    return 0;
}

  

时间: 2024-10-12 10:25:00

ouc shanghairegion#1E的相关文章

ouc shanghairegion#1A

A - Game with Pearls Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Tom and Jerry are playing a game with tubes and pearls. The rule of the game is: 1) Tom and Jerry come up together with a numb

OUC&amp;&amp;我的ACM之路(三)

OUC && 我的ACM之路(三) 时间匆匆,转眼间,省赛我都已经参加过三届了.前面两篇日志:OUC && 我的ACM之路(一)  OUC && 我的ACM之路(二)首先吐槽一下这次省赛题目实在太简单了,也许HIT认为我们就这个水平吧.SD加油,早日冲出final.  rank在这里 其实我也没啥好总结的,只是感概,时间过得匆匆,很多话,前面两篇里面已经说了.最后说一下,实际比赛的时候,ABG三个数学题目,ouc_abc写的,Orz,要当时的状态,我应该是推

ouc 1059

1059: 树判断 时间限制: 1 Sec  内存限制: 64 MB提交: 77  解决: 16[提交][状态][讨论版] 题目描述 树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构,很象自然界中的树那样.树(tree)是包含n(0<=n<=1000)个结点的有穷集合,其中: (1)每个元素称为结点(node):           (2)有一个特定的结点被称为根结点或树根(root):          (3)除根结点之外的其余数据元素被分为m(

ouc 1062

1062: 编辑距离 时间限制: 1 Sec  内存限制: 64 MB提交: 66  解决: 27[提交][状态][讨论版] 题目描述 俄罗斯科学家Vladimir Levenshtein在1965年提出了编辑距离概念. 编辑距离,又称Levenshtein距离,是指两个字符串之间,由一个转成另一个所需的最少编辑操作次数.许可的三种编辑操作包括插入一个字符.删除一个字符.将一个字符替换成另一个字符. 至今,编辑距离一直在相似句子检索的领域中发挥着不可忽视的作用. 输入 输入数据的第一行是一个正整

ouc 1087

1087: 飞行棋 时间限制: 1 Sec  内存限制: 64 MB提交: 7  解决: 3[提交][状态][讨论版] 题目描述 飞行棋是在一个长度为n的棋盘上走动棋子的游戏.游戏开始时有一个棋子在棋盘的开始,位置是1.然后每一步玩家掷一次骰子,并将棋子往前跳骰子正面大小个格子. 当棋子跳出飞行棋的棋盘时游戏结束.问游戏结束时玩游戏的人掷骰子次数的期望. 输入 第一行输入一个数T代表测试用例组数(T<=200),接下来T组测试用例,每组测试数据为棋盘大小 输出 对于每个棋盘,输出玩家要掷骰子次数

ouc 1095

1095: C.波动序列 时间限制: 2 Sec  内存限制: 64 MB提交: 103  解决: 41[提交][状态][讨论版] 题目描述 有一个长度为N的整数序列,序列里面的数是两两不同的,现在要在里面找一个波动序列,这个序列越长越好. 比如有波动序列{a0,a1,a2…an},则a0 > a1 < a2 > a3 < … 输入 第一行输入一个数T,代表有T个任务,T不大于50. 对于每个任务,输入格式为 N a0 a1 a2 … aN 其中N<=30000,测试数据保证

ouc 1057

1057: M的整数倍 时间限制: 1 Sec  内存限制: 64 MB提交: 102  解决: 29[提交][状态][讨论版] 题目描述 给定N个数,选出任意多的数(每个数只能选一次),使其和为M的整数倍.问最少需要选几个数. 输入 第一行输入一个数T代表测试用例组数(T<=10),接下来T组测试用例,每组测试用例第一行为整数M, N(1<=M, N<=1000):接下来N行每行一个数,分别代表N个数之一. 输出 对于每组测试用例,输出满足条件最少需要选几个数,若没有解输出-1.每行输

ouc 1066

1066: 青蛙过河 时间限制: 1 Sec  内存限制: 64 MB提交: 51  解决: 12[提交][状态][讨论版] 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上. 由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上的一串整数: 0,1,……,L(其中L是桥的长度). 坐标为0的点表示桥的起点,坐标为L的点表示桥的终点. 青蛙从桥的起点开始,不停的向终点方向跳跃.一次跳跃的距离是S

hbmy周赛1--E

E - Combination Lock Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the t