搜索策略学习笔记(4):bfs最优问题求解实例

Case:Nightmare

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

My AC Code

As long as the case require the solution which has the shortest path,so we should give the priority to BFS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//Author:Call偶围城
#include <stdlib.h>
#define MAP_SIZE 10
#define QUEUE_LEN 100000
#define WALL 0
#define EMPTY 1
#define START 2
#define EXIT 3
#define RESET 4
#define MAX_LIFE 6
struct {
int i;
int j;
int life;
int step;
};
const int di[4] = {-1,1,0,0};
const int dj[4] = {0,0,-1,1};
struct queue[QUEUE_LEN];
struct x,y;
int head,tail;
int n,m;
int map[MAP_SIZE][MAP_SIZE];
int min;
//int cnt;
void append(struct node q) {
int i;
queue[tail] = q;
tail = (tail + 1) % QUEUE_LEN;
return;
}
struct node serve() {
struct q = queue[head];
head = (head + 1) % QUEUE_LEN;
return q;
}
int IsEmpty() {
if (head == tail) {
return 1;
}
else {
return 0;
}
}
void bfs() {
int s_i,s_j;
int i;
while (!IsEmpty()) {
//cnt++;
x = serve();
if (map[x.i][x.j] == EXIT) {
min = x.step;
return;
}
if (x.life <= 1) {
continue;
}
for (i = 0;i < 4;i++) {
s_i = x.i + di[i];
s_j = x.j + dj[i];
if ((1 <= s_i && s_i <= n)
&& (1 <= s_j && s_j <= m)
&& map[s_i][s_j] != WALL) {
y.i = s_i;
y.j = s_j;
if (map[s_i][s_j] == RESET) {
y.life = MAX_LIFE;
map[s_i][s_j] = EMPTY;
y.step = x.step + 1;
}
else {
y.life = x.life - 1;
y.step = x.step + 1;
}
append(y);
}
}
}
}
int main() {
int N;
int s_i,s_j;
int i,j;
scanf("%d",&N);
while (N--) {
head = tail = 0;
scanf("%d%d",&n,&m);
for (i = 1;i <= n;i++) {
for (j = 1;j <= m;j++) {
scanf("%d",&map[i][j]);
if (map[i][j] == START) {
x.i = i;
x.j = j;
x.life = MAX_LIFE;
x.step = 0;
append(x);
}
}
}
//cnt = 0;
min = -1;
bfs();
printf("%dn",min);
//printf("cnt=%dn",cnt);
}
return 0;
}

AC Code from others

This code use queue function in C++ library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Author:auto
# include <cstdio>
# include <cstring>
# include <iostream>
# include <queue>
using namespace std;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
struct
{
int tim,step,x,y;
};
queue<node> que;
int main()
{
int T,i,j,n,m,map[20][20],equ[20][20];
node ip,id;
cin>>T;
while(T--)
{
cin>>n>>m;
while(!que.empty()) que.pop();
memset(equ,0,sizeof(equ));
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]==2)
{
ip.x=i;
ip.y=j;
ip.step=0;
ip.tim=6;
que.push(ip);
}
if(map[i][j]==4) equ[i][j]=1;
}
while(!que.empty())
{
ip=que.front();
que.pop();
if(map[ip.x][ip.y]==3) break;
if(1==ip.tim) continue;
int xx,yy;
for(i=0;i<4;i++)
{
xx=ip.x+dx[i];
yy=ip.y+dy[i];
if(xx>0 && xx<=n && yy>0 && yy<=m && map[xx][yy] )
{
if(4==map[xx][yy])
if(!equ[xx][yy]) continue;
else equ[xx][yy]--;
id.x=xx;
id.y=yy;
id.step=ip.step+1;
id.tim=ip.tim-1;
if(map[xx][yy]==4) id.tim=6;
que.push(id);
}
}
}
if(que.empty()) cout<<"-1"<<endl;
else cout<<ip.step<<endl;
}
return 0;
}