HDU 1072 Nightmare

Source: Internet
Author: User


Problem Descriptionignatius 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.

Inputthe 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.

Outputfor 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

Sample Output
4-113

Authorignatius.l

Even English slag does not affect the reading of the question. Sweep through the case, sweep the keywords.

I probably know test instructions.

Or a map, 0,1,2,3,4, composed of. The bomb exploded six minutes later.

0 is the wall cannot walk, 1 can walk, 2 is the starting point, 3 is the end point, 4 is the place which resets the bomb time. After 4, it was 6 minutes before the bomb exploded.

Up and down about four directions, each step one minute.

The question is whether we can escape. (Before the explosion)

Understanding the above is a simple wide search and some pruning just fine.

On the Code

#include <stdio.h> #include <algorithm> #include <queue>using namespace Std;int dir[4][2]={{0,1},{0, -1},{1,0},{-1,0}}; #define INF 0x6fffffint map[10][10];int vis[10][10];int n,m,x2,y2;struct node{int x,y,time,step;};  void BFs (int x,int y) {int i;node st,ed;queue<node>q;st.x=x;st.y=y;st.time=6;  Initial time st.step=0; Initial steps Q.push (ST), while (!q.empty ()) {St=q.front (); Q.pop (); if (st.x==x2 &&st.y==y2)//reach 3 This position {printf ("%d\n", St.step); return;}  for (i=0;i<4;i++) {ed.x=st.x+dir[i][0];ed.y=st.y+dir[i][1];ed.time=st.time-1; Time -1.ed.step=st.step+1;//steps plus one if (ed.x>=0 &&ed.y>=0 &&ed.x<n&&ed.y<m & &map[ed.x][ed.y]!=0 &&ed.time>0 &&vis[ed.x][ed.y]<st.time)//Last pruning key, can represent the next step can go, And the bomb hasn't exploded yet. (Time pruning) because it is possible to return after 4, as in case three.  {if (map[ed.x][ed.y]==4)//After 4, reset time ed.time=6; Vis[ed.x][ed.y]=ed.time;q.push (ed);}}} printf (" -1\n");}    int main () {int t,i,j; scanf ("%d", &t), int x1,y1;while (t--) {int i,k;scanf ("%d%d", &n,&m); foR (i=0;i<n;i++) for (j=0;j<m;j++) {scanf ("%d", &map[i][j]), if (map[i][j]==2) {x1=i;y1=j;//record}if (map[i][j]== 3) {x2=i,y2=j;//record}}memset (vis,0,sizeof (Vis)); BFs (x1,y1);//Run the BFS to}return 0;}


HDU 1072 Nightmare

Related Keywords:

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.