杭电1072
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4360 Accepted Submission(s): 2177
Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent
the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area
in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.
Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.
Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.
Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
Sample Input
3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
Sample Output
4 -1 13
本题的关键在于怎么解决回路问题,这里我们通过values[8][8]来记录上次搜索摸一个位置是的“生命数值”,如果以后需要再次搜索该点的时候,如果它的生命数值没有之前的大,那么没有价值继续加入这个节点。因为,已经有一个更优的节点在优先队列中或者已经被访问(当然了,可能已经被排除)。下面是通过AC的程序代码:
//1072 #include <iostream> #include <fstream> #include <queue> using namespace std; typedef struct node { int i; int j; int time;//到达这个点的时候的时间 int lifetime;//此时的生命时间值 friend bool operator < (node n1, node n2) { return n1.time > n2.time; } }Node; typedef struct { int i; int j; }Point; int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; int valbles[8][8];//表示上次插入的时候的剩余时间 char map[8][8]; int n, m; Point start;//开始点 Point dest;//出口 int bfs() { if (start.i == dest.i && start.j == dest.j) return 0; priority_queue<Node> Q; Node temp1; temp1.i = start.i; temp1.j = start.j; temp1.time = 0; temp1.lifetime = 6; Q.push(temp1); //used[start.i][start.j] = true;//已经走过 valbles[start.i][start.j] = 6; while(!Q.empty()) { Node temp = Q.top(); Q.pop(); if (temp.lifetime == 1) continue;//已经没有生命数值 if (temp.time >= n*m) return -1; for (int i = 0; i < 4; ++ i)//扩展四个方向的路径 { int ni = temp.i + dir[i][0]; int nj = temp.j + dir[i][1]; if (ni >= 0 && ni < n && nj >= 0 && nj < m && map[ni][nj] != '0') { if (map[ni][nj] == '3')//已经达到出口 return temp.time + 1; Node extNode; extNode.i = ni; extNode.j = nj; extNode.time = temp.time + 1; if (map[ni][nj] == '1')//直接走,生命数值减去一 extNode.lifetime = temp.lifetime - 1; else if (map[ni][nj] == '4')//回复生命数值到6 extNode.lifetime = 6; //used[ni][nj] = true; //已经走过 if (extNode.lifetime <= valbles[ni][nj])continue;//没有继续插入的价值 valbles[ni][nj] = extNode.lifetime; Q.push(extNode); } } } return -1; } int main() { //ifstream cin("in.txt"); int num ; cin >> num ; for (int i =0; i < num; ++ i) { cin >> n >> m; for (int j = 0; j < n; ++ j) for (int k = 0; k < m ; ++ k) { cin >> map[j][k]; valbles[j][k] = -1; if (map[j][k] == '2') { start.i = j; start.j = k; } else if (map[j][k] == '3') { dest.i = j; dest.j = k; } } int result = bfs(); cout << result << endl; } //cin.close(); return 0; }