Good questions about bfs!
I started to think of using the priority queue, but I had to wait a while, but it took more than ms to get down ~ Cup priority
Later, I thought that bfs could be used directly. unexpectedly, the TE was not displayed, but the AC was used. It took more than ms to get speechless !!!!
Cry out for the first time ......
The bfs practices are as follows:
1. Start to stack
2. the start point can directly reach all vertices (which do not take time) into the stack.
3. Two steps
The starting point of 3.1 cannot be directly reached (Time required ).
3.2 add all the points that can be reached directly (no time spent) to the stack
3.3 jump to 3
4 jump to 1
Note: the start point is the first point of the current outbound stack.
If you do not understand this process, you can check the code (although the code is a bit ......, It can be further simplified. In the future, we can't always think about priorities. Who gives priority to who has a cup? Of course, it doesn't mean that we have a large number of female compatriots ......)
1 # include <iostream>
2 # include <queue>
3 using namespace std;
4 struct node
5 {
6 int x, y, time;
7/* friend bool operator <(node a, node B)
8 {
9 return a. time> B. time;
10 }*/
11 };
12 int n, m;
13 int xi [8] = {-1,-1, 0, 1, 1, 1, 0,-1 };
14 int yi [8] = {0, 1, 1, 1, 0,-1,-1,-1 };
15 char map [2, 1005] [2, 1005];
16 bool vist [1005] [1005];
17 bool check (int dx, int dy)
18 {
19 if (dx> = 0 & dy> = 0 & dx <n & dy <m) return true;
20 return false;
21}
22 queue <node> q;
23 int bfs (node now, node end)
24 {
25 while (! Q. empty () q. pop ();
26 now. time = 0;
27 q. push (now );
28
29 for (int I = 0; I <n; I ++)
30 for (int j = 0; j <m; j ++)
31 vist [I] [j] = false;
32 vist [now. x] [now. y] = true;
33 node next, tmp;
34 bool flag = false;
35 while (! Q. empty ())
36 {
37 now = q. front ();
38 tmp = now;
39 if (now. x = end. x & now. y = end. y) return now. time;
40 q. pop ();
41 flag = false;
42 while (1)
43 {
44 next. x = tmp. x + xi [map [tmp. x] [tmp. y]-'0'];
45 next. y = tmp. y + yi [map [tmp. x] [tmp. y]-'0'];
46 if (check (next. x, next. y )&&! Vist [next. x] [next. y])
47 {
48 if (next. x = end. x & next. y = end. y) return tmp. time;
49 next. time = tmp. time;
50 vist [next. x] [next. y] = true;
51 q. push (next );
52 tmp = next;
53} else break;
54}
55 for (int I = 0; I <8; I ++)
56 {
57 next. x = now. x + xi [I];
58 next. y = now. y + yi [I];
59 if (check (next. x, next. y )&&! Vist [next. x] [next. y])
60 {
61 if (map [now. x] [now. y]-'0' = I) next. time = now. time;
62 else next. time = now. time + 1;
63 if (next. x = end. x & next. y = end. y) return next. time;
64 vist [next. x] [next. y] = true;
65 q. push (next );
66 tmp = next;
67 while (1)
68 {
69 next. x = tmp. x + xi [map [tmp. x] [tmp. y]-'0'];
70 next. y = tmp. y + yi [map [tmp. x] [tmp. y]-'0'];
71 if (check (next. x, next. y )&&! Vist [next. x] [next. y])
72 {
73 if (next. x = end. x & next. y = end. y) return tmp. time;
74 next. time = tmp. time;
75 vist [next. x] [next. y] = true;
76 q. push (next );
77 tmp = next;
78} else break;
79}
80}
81}
82}
83 return 0;
84}
85 int main ()
86 {
87 while (scanf ("% d", & n, & m )! = EOF)
88 {
89 int I, j;
90 for (I = 0; I <n; I ++)
91 scanf ("% s", map [I]);
92 int T;
93 scanf ("% d", & T );
94 node now, end;
95 for (I = 0; I <T; I ++)
96 {
97 scanf ("% d", & now. x, & now. y, & end. x, & end. y );
98 now. time = 0;
99 now. x --;
100 now. y --;
101 end. x --;
102 end. y --;
103 if (now. x = end. x & now. y = end. y)
104 {
105 printf ("0 \ n ");
106} else printf ("% d \ n", bfs (now, end ));
107}
108}
109 return 0;
110}
111
From road repair