HDOJ 题目5094 Maze(BFS+状压)
来源:互联网 发布:编程软件下载 编辑:程序博客网 时间:2024/05/22 08:18
Maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 895 Accepted Submission(s): 313
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.
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).
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.
If there is no possible plan, output -1.
Sample Input
4 4 991 2 1 3 21 2 2 2 02 1 2 2 02 1 3 1 02 3 3 3 02 4 3 4 13 2 3 3 03 3 4 3 04 3 4 4 022 1 24 2 1
Sample Output
14
Source
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)
Recommend
hujie | We have carefully selected several similar problems for you: 5379 5378 5377 5376 5375
题目大意:从1 1走到n m,第一行输入n m p,p表示p个要是,在输入一个k,后边k行,每行两个坐标,一个g,g是0说明两个坐标之间有墙,否则有g号门
再输入个s,后边s行,每行一个左边,一个q,表示q号钥匙在这个坐标上
这个题好坑啊,,,比赛错了7次,,起点可能是钥匙,,,而且每个点可能有多把钥匙,,,
ac代码
Problem : 5094 ( Maze ) Judge Status : AcceptedRunId : 14490146 Language : C++ Author : lwj1994Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;int map[55][55][55][55],key[55][55][55],keynum[55][55];int vis[1<<11][55][55],v[55][55][55];int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};int n,m,q;struct s{ int x,y,step,cnt; friend bool operator < (s a,s b) { return a.step>b.step; }}a,temp;int jud(struct s a,struct s b){ if(b.x<=0||b.x>n||b.y<=0||b.y>m) return 0; if(map[a.x][a.y][b.x][b.y]==0) return 0; //if(vis[b.cnt][b.x][b.y]) // return 0; return 1;}int bfs(){ a.x=1; a.y=1; a.step=0; a.cnt=0; int i,j; priority_queue<struct s>q; memset(vis,0,sizeof(vis)); if(key[1][1][0]) { for(i=0;i<keynum[1][1];i++) { int cc=1<<key[1][1][i]; a.cnt=a.cnt|cc; } } q.push(a); vis[a.cnt][1][1]=1; while(!q.empty()) { a=q.top(); q.pop(); for(i=0;i<4;i++) { temp.x=a.x+dx[i]; temp.y=a.y+dy[i]; temp.step=a.step+1; temp.cnt=a.cnt; if(!jud(a,temp)) continue; if(map[a.x][a.y][temp.x][temp.y]>0) { int cc=temp.cnt; if(((cc>>map[a.x][a.y][temp.x][temp.y])&1)==0) { continue; } } if(key[temp.x][temp.y][0]) { for(j=0;j<keynum[temp.x][temp.y];j++) { int cc=1<<(key[temp.x][temp.y][j]); temp.cnt=temp.cnt|cc; } } if(!vis[temp.cnt][temp.x][temp.y]) { if(temp.x==n&&temp.y==m) return temp.step; vis[temp.cnt][temp.x][temp.y]=1; q.push(temp); } } } return -1;}int main(){ while(scanf("%d%d%d",&n,&m,&q)!=EOF) { memset(map,-1,sizeof(map)); int i; int nm; scanf("%d",&nm); for(i=0;i<nm;i++) { int x1,x2,y1,y2,k; scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k); map[x1][y1][x2][y2]=k; map[x2][y2][x1][y1]=k; } int num; scanf("%d",&num); memset(key,0,sizeof(key)); memset(keynum,0,sizeof(keynum)); memset(v,0,sizeof(v)); while(num--) { int x,y,k; scanf("%d%d%d",&x,&y,&k); if(v[x][y][k]) continue; v[x][y][k]=1; key[x][y][keynum[x][y]++]=k; } if(n==1&&m==1) { printf("0\n"); continue; } printf("%d\n",bfs()); }}
0 0
- HDOJ 题目5094 Maze(BFS+状压)
- HDOJ 5094 Maze 【状态压缩+BFS】
- Hdu 5094 Maze(状压dp+bfs)
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
- HDOJ 题目4568 Hunter(BFS+状压DP,TSP)
- hdu 5094 Maze (bfs+状态压缩)
- [HDU]5094Maze(状态压缩BFS)
- HDU 5094 Maze(BFS、状态压缩)
- hdu 5094 Maze bfs
- hdu 5094 Maze(BFS)
- HDOJ 题目1226 超级密码(BFS)
- HDOJ 题目1728 逃离迷宫(BFS)
- HDOJ 题目1495 非常可乐(BFS)
- HDOJ 题目1372 Knight Moves(BFS)
- HDOJ 题目2102 A计划(BFS)
- HDOJ 题目1242 Rescue(BFS)
- HDOJ 题目1401 Solitaire(双向BFS)
- zoj3652 Maze(bfs)
- 在Linux下怎么看网络流量
- 在需求分析中就可以避免的那些错误2
- 取石子
- iframe的滚动条问题:显示/隐藏滚动条
- maven学习笔记(一)常用插件配置详解
- HDOJ 题目5094 Maze(BFS+状压)
- LoadRunner用户行为模拟器 《第三篇》
- python 随便写的数据库差异化比较,并自动生成差异化脚本
- JVM学习笔记--虚拟机性能监控与故障处理工具
- el表达式中 ==null 和empty 区别
- const限定符和constexpr初探
- 《序列》游戏攻略【18~24】
- IOS开发之----NSDictionary,JSON和XML互相转换
- sdut oj3117图的基本存储的基本方式二