[bfs] Red Red Goes to Xiaozhai (bfs Seeking the shortest path + thinking)

Title description

Honghong wanted to go to the city center for dinner at noon today, so he decided to go there by bus. But when going to the bus, Honghong decided to practice space magic and travel through time and space.

Now Honghong's road to the bus is a straight line, and then he can move in three ways:

  1. Walk forward one position

  2. Go back one position

  3. Room to move, his position from xmoving to2 * x

All three modes of movement are required 10 s

But Honghong hopes to minimize physical exertion, so he hopes to reach the bus station in the shortest time.

enter

There are multiple test samples

Each set comprising a test sample nandk ( 0 < n, k<= 100000)

n Indicates the current position of Hong Hong

k Indicates the location of the bus stop

Output

Output the minimum time required by the bus station, output x:x(indicating minutes and seconds)

Sample input

5 17
7 4
5 10000

Sample output

0:40
0:30
2:10

prompt

N May be greater than K


Analysis and code

Most topics are previously encountered *2,+1 / -1, +1, -1two operations do not occur simultaneously, using backstepping way we can solve this problem. But this problem can not reverse the lack of push, because knowing the odd case can not be determined by +1or -1an even number of cases.

The smaller the number of steps in this question, the better, and each step of the operation can be regarded as the same weight. You can only choose one of the three options, so it can be carried out bfsand the minimum number of steps for the conversion operation can be obtained.

Using simulation codes queue array, distthe array has three states, is initialized to -1 indicates that the number can not be reached, the starting point is 0, each step conversion update distarray can. That is, the optimization of the weight-judgment array distand the completion of the weight-judgment + recording conversion step function are also common techniques.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
const int N = 100005;
 
int q[N];
int n, k;
int res;
int dist[N];
 
void bfs(int x) {
    
    
    memset(dist, -1, sizeof dist);
     
    int hh = 0, tt = 0;
     
    dist[x] = 0;
     
    q[tt ++ ] = x;
     
    while (hh <= tt) {
    
    
        int t = q[hh ++ ];
         
         // 三种操作依次进行,操作不论先后,先搜到的数一定是最小的转化步骤
        int u;
        for (int i = 0; i < 3; i ++ ) {
    
    		
            if (i == 0) u = t + 1;
            else if (i == 1) u = t - 1;
            else if (i == 2) u = t * 2;
         
            if (u < 0 || u > N) continue;
            if (dist[u] == -1) {
    
    
                dist[u] = dist[t] + 1;
                q[tt ++ ] = u;
            }
            if (u == k) {
    
    
                res = dist[u];
                return ;
            }
        }
    }
}
 
int main(){
    
    
    while (cin >> n >> k) {
    
    
        if (n >= k) {
    
    
            int t = (n - k) * 10;
            printf("%d:%d\n", t / 60, t % 60);
        } else {
    
    
            bfs(n);
            int t = res * 10;
            printf("%d:%d\n", t / 60, t % 60);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/115062756