Nyoj 483 Nightmare "bfs+ priority queue"

Source: Internet
Author: User

Nightmare time limit:MS | Memory limit:65535 KB Difficulty:4
Describe
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The Labyrinth has a exit, Ignatius should get out of the 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, which is to move from one area to the nearest are A (that's, 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) Tak Es 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 The Labyr Inth, if he could, output the minimum time that he had to use to find the exit of the The labyrinth, else output-1.

Here is some rules:
1. We can assume the Labyrinth is a 2 array.
2. Each minute, Ignatius could-only get-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 The Labyrinth.
4. If Ignatius get to the area which contains bomb-rest-equipment then the exploding time turns to 0, he can ' t use the equ Ipment to reset the bomb.
5. A Bomb-reset-equipment can used as many times as you wish, if it's needed, Ignatius can get to any areas in the lab Yrinth as many times as you wish.
6. The time to reset the exploding time can is ignore, in and words, if Ignatius get to a area which contain bomb-rest- Equipment, and the exploding time is larger than 0, the exploding time would being reset to 6.
Input
The input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case is starts with the 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 is five integers which indicate the different type of area in the Labyrinth:
0:the area was 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
23 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 3
Sample output
4-1
Test instructions is: The starting point for the 2 end is 3 bombs will explode in 6 minutes (when just out of the maze remaining time for 0 will also explode)
When encountering 4 o'clock time can be reset to 6 when walking to 4 o'clock the remaining time is 0 can not reset the time, if you can walk out of the maze within the specified time, the output of the number of steps
Otherwise output-1
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> #define MAX 10using namespace Std;int map[max][max];int vis[max][max];int n,m,t,sum;int x1,x2,y1,y2;struct node{int x,y;int step,time; friend bool Operator < (node A,node b) {return a.step>b.step;}}; void BFs () {int move[4][2]={0,1,0,-1,1,0,-1,0};int i,j;priority_queue<node>q;node beg,end;beg.x=x1;beg.y=y1; beg.step=0;beg.time=6;//is used to record how many steps Q.push (beg), Vis[x1][y1]=1;while (!q.empty ()) {end=q.top (), Q.pop (), before encountering a possible reset time (4);   ; if (end.x==x2&&end.y==y2) {printf ("%d\n", end.step);      return; }if (end.time==1)//Determine whether to reach the end point when one step is left (because there is no use when the 0 steps are left, even if it is possible to reset the time) continue;for (i=0;i<4;i++) {Beg.x=end.x+move I [0];beg.y=end.y+move[i][1];if (0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m& &map[beg.x][beg.y]!=0) {if (map[beg.x][beg.y]==4)//When encountering 4 {beg.time=6;//step time changed back to 6 map[beg.x][beg.y]=0;} elsebeg.time=end.time-1;//Otherwise walk one step time minus 1 Beg.step=end.step+1;q.push (beg);}}} printf (" -1\n");} int main () {int i,j,k,s;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m), for (i=0;i<n;i++) {for (j=0;j <m;j++) {scanf ("%d", &map[i][j]);}} for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (map[i][j]==2) {x1=i;y1=j;} if (map[i][j]==3) {x2=i;y2=j;}}} Sum=0;memset (vis,0,sizeof (Vis)); BFs ();}   return 0;}

  

Nyoj 483 Nightmare "bfs+ priority queue"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.