HDU 1072 BFS

Source: Internet
Author: User
Nightmare

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 7083 accepted submission (s): 3409


Problem descriptionignatius had a nightmare last night. he found himself in a labyrinth with a time bomb on him. the labyrinth has an exit, Ignatius shocould 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 cocould 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 cocould reset the exploding time to 6 minutes.

Given the layout of the labyrinth and Ignatius 'start position, please tell Ignatius whether he cocould get out of the labyrinth, if he cocould, 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 cocould only get to one of the nearest area, and he shocould not walk out of the border, of course he cocould 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 frequently times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as frequently 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 wocould be reset to 6.

 

Inputthe 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 shocould 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 shocould output the minimum time he needs, else you shoshould just output-1.

 

Sample input33 32 1 11 1 01 1 34 82 1 1 0 1 1 01 0 4 1 1 1 0 4 11 0 0 0 0 0 0 11 1 1 1 4 1 1 1 35 81 2 1 1 1 1 1 4 1 0 0 0 1 0 1 1 1 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 1 1 1 1 1 1 1 1 1 1

 

Sample Output4-113 meaning: Give a n * m diagram, initially a person in map [I] [J] = 2, can only move up, down, left, right 1 lattice, each step is reduced by 1 and the initialization time is 6. If the time is 1 and the exit 3 is not found, 1 is output; otherwise, the minimum number of steps is output, and 0 is the wall, 1 is the open space, 2 is the initial location, 3 is the target, and 4 can reset the time to 6. Idea: the optimal Maze problem is generally BFs. Just simulate it and determine whether the next step can be taken. If it can be taken and the time after the next step is not 0, take this step, then, determine whether it is the target, whether it is the number of returned steps, whether it is 4, and whether it is the update time. In my code, a visited [] [] array is added for optimization, because each location can be up to four times, if there are more things, you can skip the BFS function. Code:
1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 # include <vector> 5 # include <queue> 6 # include <iostream> 7 using namespace std; 8 9 struct node {10 int X, Y, W, step; 11}; 12 13 int n, m; 14 int map [10] [10]; 15 int visited [10] [10]; 16 int XX [] = {1,-1, 0}; 17 int YY [] = {0, 0, 1,-1 }; 18 19 int judge (int x, int y) {20 if (Map [x] [Y] = 0 | x <0 | x> = n | Y <0 | Y> = m) return 0; 21 return 1; 22} 23 24 int B FS (int x, int y) {25 int I, j; 26 node p, q; 27 p. X = x; p. y = y; p. W = 6; p. step = 0; 28 queue <node> q; 29 Q. push (p); 30 While (! Q. empty () {31 P = Q. front (); 32 Q. pop (); 33 for (I = 0; I <4; I ++) {34 Q. X = P. X + XX [I]; 35 Q. y = P. Y + YY [I]; 36 Q. W = P. w-1; 37 Q. step = P. step + 1; 38 If (Judge (Q. x, Q. y) {39 if (Q. w> 0 & visited [q. x] [q. y] <4 & map [q. x] [q. y]! = 2) {// because it starts from 2, then returns to 2, which is 40 if (Map [q. x] [q. y] = 3) return Q. step; 41 else if (Map [q. x] [q. y] = 4) {42 Q. W = 6; 43} 44 visited [q. x] [q. y] ++; 45 Q. push (Q); 46} 47} 48} 49} 50 return 0; 51} 52 main () 53 {54 int I, j, t, x, y; 55 CIN> T; 56 while (t --) {57 memset (visited, 0, sizeof (visited); 58 scanf ("% d", & N, & M); 59 for (I = 0; I <n; I ++) {60 for (j = 0; j <m; j ++) {61 scanf ("% d", & map [I] [J]); 62 if (Map [I] [J] = 2) {63 x = I; y = J; 64} 65} 66} 6 7 int ans = BFS (x, y); 68 if (! Ans) printf ("-1 \ n"); 69 else printf ("% d \ n", ANS); 70} 71}

 

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.