HDU 5094 Maze(BFS、状态压缩)
来源:互联网 发布:数据接口类型 编辑:程序博客网 时间:2024/05/18 15:04
Maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 308 Accepted Submission(s): 112
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上海全国邀请赛——题目重现(感谢上海大学提供题目)
注意钥匙拿到就一直拿到了,门只需开一次,同一个位置可能有多把钥匙!!
#include <iostream>#include <cstring>#include <queue>using namespace std;const int MAXN = 16, dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};int n, m, p, k, s, go[MAXN][MAXN][MAXN][MAXN], key[MAXN][MAXN], ans;bool visited[MAXN][MAXN][1<<10];struct State { int x, y, key, step; State(int _x, int _y, int _key, int _step) : x(_x), y(_y), key(_key), step(_step) {}};void init() { memset(go, -1, sizeof(go)); memset(key, 0, sizeof(key)); memset(visited, false, sizeof(visited)); ans = -1;}void input() { cin >> k; for (int i = 0; i < k; i++) { int x1, y1, x2, y2, g; cin >> x1 >> y1 >> x2 >> y2 >> g; go[x1][y1][x2][y2] = go[x2][y2][x1][y1] = g; } cin >> s; for (int i = 0; i < s; i++) { int x, y, q; cin >> x >> y >> q; key[x][y] |= 1 << (q-1); }}void work() { queue<State> q; q.push(State(1, 1, 0, 0)); while (!q.empty()) { State cur = q.front(); q.pop(); if (cur.x == n && cur.y == m) { ans = cur.step; return; } cur.key |= key[cur.x][cur.y]; for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i], ny = cur.y + dy[i]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) { int gate = go[cur.x][cur.y][nx][ny]; if ((gate > 0 && (cur.key & (1 << (gate-1)))) || (gate == -1)) { if (!visited[nx][ny][cur.key]) { visited[nx][ny][cur.key] = true; q.push(State(nx, ny, cur.key, cur.step+1)); } } } } }}void output() { cout << ans << endl;}int main() { ios::sync_with_stdio(false); while (cin >> n >> m >> p) { init(); input(); work(); output(); } return 0;}
0 0
- hdu 5094 Maze (bfs+状态压缩)
- [HDU]5094Maze(状态压缩BFS)
- HDU 5094 Maze(BFS、状态压缩)
- hdu 5094 Maze bfs+状态压缩
- HDU 5094 --Maze【BFS && 状态压缩】
- hdu 5091 Maze (bfs+状态压缩)
- HDOJ 5094 Maze 【状态压缩+BFS】
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
- Maze zoj3652(bfs+记忆化+状态压缩)
- zoj 3652 ZOJ 3652 MAZE(BFS+状态压缩)
- CodeForces 197D Infinite Maze (BFS+状态压缩)
- hdu 5094 Maze bfs
- hdu 5094 Maze(BFS)
- Hdu 5094 Maze(状压dp+bfs)
- hdu 1429 (bfs+状态压缩)
- HDU 1429 (BFS+状态压缩)
- hdu 1429 BFS+状态压缩
- hdu 2209 BFS + 状态压缩
- PHP中判断字符串是否含有中文
- gridView点击Item有黄色背景??
- hdu 4465 Candy(log压缩大数)
- 【DP】 HDOJ 2829 Lawrence
- Combinations
- HDU 5094 Maze(BFS、状态压缩)
- DBHelper连接oracle数据库类
- 1212万能盛典--全民预售报名开启
- 面向对象葵花宝典:思想、技巧与实践(36) - 设计原则如何用?
- 1212万能盛典--无线会场招商
- [水煮网商]成为有价值的网商
- 【LeetCode】Find Minimum in Rotated Sorted Array
- 卖家开店需留意
- 第11周 项目6-3 素数