杭电1072 迈不过友情╰ 2022-06-04 07:22 161阅读 0赞 # 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. 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. 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; }
相关 杭电1061 Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J 布满荆棘的人生/ 2022年09月17日 05:27/ 0 赞/ 259 阅读
相关 杭电acm 1072 Nightmare 杭电acm 1072 Nightmare 题目链接: > [http://acm.hdu.edu.cn/showproblem.php?pid=1072][http_a 本是古典 何须时尚/ 2022年08月04日 10:55/ 0 赞/ 141 阅读
相关 杭电1039 Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 一时失言乱红尘/ 2022年06月05日 12:48/ 0 赞/ 264 阅读
相关 杭电1072 Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot 迈不过友情╰/ 2022年06月04日 07:22/ 0 赞/ 162 阅读
相关 杭电1026 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 快来打我*/ 2022年06月04日 05:53/ 0 赞/ 282 阅读
相关 杭电oj Problem Title 1 Pro. ID 1000 A+B Problem include<stdio.h> int main() { £神魔★判官ぃ/ 2022年05月15日 16:14/ 0 赞/ 315 阅读
相关 杭电1060 此题是一道数学题,也是一道技巧题,也是不能直接算的,否则会超时的!!! 此题思路: 设n^n=d.xxxx\10^(k-1),其中k表示n^n的位数; d.xxxx 痛定思痛。/ 2021年12月01日 22:40/ 0 赞/ 305 阅读
相关 杭电2075 此题真的是简单的再不能简单了!呵呵!我一直纠结,出这样的题是什么意思呢?不懂!哎,不说那些废话了,直接 ac吧!呵呵! \include<iostream> using 今天药忘吃喽~/ 2021年12月01日 22:38/ 0 赞/ 285 阅读
相关 杭电2078 说实话,此题是一道有严重bug的问题,对于xhd没晚能复习的科目数m根本就没用上!!!哎不管那么些了,反正ac了!呵呵!此题这样想xhd得复习效率是前一课程和后一课程复习效率差 ╰+攻爆jí腚メ/ 2021年12月01日 22:38/ 0 赞/ 335 阅读
相关 杭电2090 此题就是一道令人无法琢磨的题!哎!!我简直就无语了!!呵呵!竟然能出这题。。。。 废话少说,直接ac!!! \\\ 此题要想输出结果,还需要注意一下! 在linux 约定不等于承诺〃/ 2021年12月01日 21:12/ 0 赞/ 343 阅读
还没有评论,来说两句吧...