Home > Back-end >  Do a BFS method of input data is always stop working, and have a big help to see what's going o
Do a BFS method of input data is always stop working, and have a big help to see what's going o

Time:09-29

You receive a notification was admitted to central south university, happy to come to changsha, soon you will come to YueLuNa road, known to your location is N, the position of central south university is K, in order to go to central south university, you have two mobile way: walk or take the bus,

Walk: you can start your position moves to X + 1 X or X - 1

By bus: you can start your position move to the 2 X X

The time required to walk or take the bus every time is 1 minutes, central south university you want to reach as soon as possible, what is the time required?

Enter
For each set of data, the input line, the N and K (1 & lt;=N, K
=100000)
O
For each set of data, the output of a line, the time needed for

The sample input
5 17
Sample output
4

My code is as follows: when the input sample is larger than 20000 K program will stop the response, what is wrong with==
#include
using namespace std;
Const int maxn=100020;
Int MPT [maxn];
Int vis [maxn];

Int main (void) {
Int m, n;
While (cin> M> N) {
Queue Q;
Memset (MPT, 0, sizeof (MPT));
Memset (vis, 0, sizeof (vis));
for(int i=0; iMPT [I]=0;
Vis [I]=0;
}
while(! Q.e mpty ()) {
Q.p op ();
}
Q.p ush (m);
Vis [m]=1;
int temp;
while(! Q.e mpty ()) {
Int ans=q.f ront ();
Q.p op ();
If (ans==n) {
Coutbreak;
}
Temp=ans + 1;
If (vis/temp==0 & amp; & Temp<=100000 & amp; & Temp>=0) {
Vis [\]=1;
MPT [\]=MPT (ans) + 1;
Q.p ush (temp);
}
Temp=ans - 1;
If (vis/temp==0 & amp; & Temp<=100000 & amp; & Temp>=0) {
Vis [\]=1;
MPT [\]=MPT (ans) + 1;
Q.p ush (temp);
}
Temp=ans * 2;
If (vis/temp==0 & amp; & Temp<=100000 & amp; & Temp>=0) {
Vis [\]=1;
MPT [\]=MPT (ans) + 1;
Q.p ush (temp);
}
}
}
return 0;
}
  • Related