hdu5094-Maze

Maze

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


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
 
 
 这道题做的太曲折了,刚开始发现这个图不能设置vis数组,因为很有可能有些门本来打不开,然后在某处捡到钥匙再原路返回就可以了,然后我发现mle,我以为是题目卡的紧,四维的flag数组会爆,就一直在减代码的内存,尝试了几乎所有方法都没法把memory.exe减小到1000000k以下,最少也超了200k左右,后来我把数组换成了map,不mle了,t了,我以为是map查询lgn在bfs里次数太多导致的超时?最后发现 是因为这个题需要vis的数组,只是得加一维状态,刚开始没想到 还是太naive,果然刚开始mle的原因是因为没有vis跑的时间太长导致还没有到时限 队列里的node先把内存爆了 = =....下次一定要注意bfs vis是必须的 只不过每次的状态要考虑清楚
 1 #include<cstdio>
 2 
 3 #include<queue>
 4 
 5 #include<string.h>
 6 
 7 #include<algorithm>
 8 
 9 #define inf 0x3f3f3f3f
10 
11 const int maxn=50;
12 
13 using namespace std;
14 
15 typedef pair<int,int> P;
16 
17 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
18 
19 int type;
20 
21 int n,m,p;
22 
23 int k;
24 
25 int s;
26 
27 int a,b,c,e;
28 
29 bool vis[maxn+10][maxn+10][1<<11];
30 
31 int key[maxn+10][maxn+10];
32 
33 int flag[maxn+10][maxn+10][maxn+10][maxn+10];
34 
35 struct node{
36   P p;
37   int haskey;
38   int step;
39   node(int haskey=0,int step=0):haskey(haskey),step(step) {}
40 }newn;
41 
42 int bfs(){
43    queue<node> q;
44    node st;
45    st.p=P(1,1);
46    st.haskey|=key[1][1];
47    vis[1][1][st.haskey]=1;
48    q.push(st);
49    while(!q.empty()){
50         node v=q.front();q.pop();
51         if(v.p.first==n&&v.p.second==m) return v.step;
52         for(int i=0;i<4;i++){
53            int nx=v.p.first+dir[i][0],ny=v.p.second+dir[i][1];
54            if(!flag[nx][ny][v.p.first][v.p.second]||nx<=0||nx>n||ny<=0||ny>m||(vis[nx][ny][v.haskey|key[nx][ny]])) continue;
55            if(flag[nx][ny][v.p.first][v.p.second]!=-1){
56                 if(!((1<<(flag[nx][ny][v.p.first][v.p.second]))&v.haskey)) continue;
57            }
58            newn.p=P(nx,ny);
59            newn.step=v.step+1;
60            newn.haskey=v.haskey|key[nx][ny];
61            //printf("%d %d %d\n",nx,ny,newn.step);
62            vis[nx][ny][newn.haskey]=1;
63            q.push(newn);
64         }
65    }
66   return -1;
67 }
68 
69 int main()
70 {
71     while(scanf("%d%d%d",&n,&m,&p)!=EOF){
72         memset(key,0,sizeof(key));
73         memset(flag,-1,sizeof(flag));
74         memset(vis,false,sizeof(vis));
75         scanf("%d",&k);
76         for(int i=1;i<=k;i++){
77                 scanf("%d%d%d%d%d",&a,&b,&c,&e,&type);
78                 flag[a][b][c][e]=flag[c][e][a][b]=type;
79         }
80         scanf("%d",&s);
81         for(int i=1;i<=s;i++){
82                 scanf("%d%d%d",&a,&b,&type);
83                 key[a][b]|=(1<<(type));
84         }
85        int ans=bfs();
86        printf("%d\n",ans);
87     }
88     return 0;
89 }
View Code

 

posted @ 2016-08-04 15:33  GeniusYang  阅读(175)  评论(0编辑  收藏  举报