Total passenger garlic garlic Jun home (conditional BFS)

 

Garlic king to go home, but his house keys in the hands of his friend coconut flower girl, he must first get a key to go home from the hands of broccoli sister. Broccoli sister told him: "You are my house keys copied a lot of, respectively, in different places."

Garlic Jun hoping to return home as soon as possible, he needs to first obtain a key to any, would you please help him calculate the shortest journey home need.

Jun garlic city life can be seen as a n × m grid, including road barriers, and key local home where can be seen as a road, you can. Jun garlic can be moved up and down along the four directions in the city, take a step to move a grid counted.

Input Format

The first line has two integers n, m. City maps are n rows and m columns. (1≤n, m≤2000)

The next n lines of m characters, representing a map of the city. '' Represents the road, '#' represents an obstacle, 'S' represents the location where the garlic king, 'T' represents the position of the garlic family, 'P' represents the position of the key. In addition to obstacles, other places can. (Jun garlic title ensure there is at least one path can successfully get the key and go home)

Output Format

Garlic output of the minimum number of steps to go home, per line.

Sample input

8 10
P.####.#P#
..#..#...#
..#T##.#.#
..........
..##.#####
..........
#####...##
###....S##

 

Sample Output

21

 

 

bfs time to open a multi-dimensional array of tags indicating whether the state has made the key. If you reach the end of the state and get the keys are labeled, the end of the bfs.

So we need to open a three-dimensional array vis third dimension to mark whether to get the key, that is, the same point can actually go twice, the first time is not the time to get the key, the second is to get the key when

 

 

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 int n,m;
 7 char mat[2010][2010];
 8 bool vis[2010][2010][2];
 9 int dir[4][2]={1,0,-1,0,0,-1,0,1};
10 
11 struct node{
12     int x,y,step,op;
13     node(){}
14     node(int _x,int _y,int _step,int _op):x(_x),y(_y),step(_step),op(_op){} 
15 }st,key;
16 
17 void bfs(node u) {
18     queue<node> q;
19     q.push(u);
20     vis[u.x][u.y][0]=1;
21     while(!q.empty()) {
22         node f=q.front();
23         q.pop();
24         printf("x=%d y=%d step=%d op=%d\n",f.x,f.y,f.step,f.op);
25         if(mat[f.x][f.y]=='T'&&f.op) {
26             printf("%d",f.step);
27             return ;
28         }
29         for(int i=0;i<4;i++) {
30             int nx=f.x+dir[i][0];
31             int ny=f.y+dir[i][1];
32              if (vis [nx] [] [f.op] || nx < 0 || nx> = n || < 0 ||> = m || mat [nx] [the] == ' # ' ) continue ;
33              if (mat [nx] [the] == ' P ' ) {
 34                  q.push (node (nx, f.step + 1 , 1 ));
35                  vis [nx] [a] [ 1 ] = 1 ;
36              }
 37              else 
38              {
 39                  q.push (node (nx, f.step + 1 , f.op));
40                 vis[nx][ny][f.op]=1;
41             }
42         }
43     }
44 }
45 
46 int main() {
47     scanf("%d%d",&n,&m);
48     for(int i=0;i<n;i++) {
49         scanf("%s",mat[i]);
50         for(int j=0;j<m;j++) {
51             if(mat[i][j]=='S') {
52                 st.x=i;
53                 st.y=j;
54                 st.step=0;
55                 st.op=0;
56             }
57         }
58     }
59     bfs(st);
60     return 0;
61 }

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12170840.html