题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
And there are many different types of trees in HUST, each of which has a number represent its type. The doctors of biology in HUST find 4 different ways to change the tree’s type x into a new type y:
1. y=x+1
2. y=x-1
3. y=x+f(x)
4. y=x-f(x)
The function f(x) is defined as the number of 1 in x in binary representation. For example, f(1)=1, f(2)=1, f(3)=2, f(10)=2.
Now the doctors are given a tree of the type A. The doctors want to change its type into B. Because each step will cost a huge amount of money, you need to help them figure out the minimum steps to change the type of the tree into B.
Remember the type number should always be a natural number (0 included).
输入描述:
One line with two integers A and B, the init type and the target type.
输出描述:
You need to print a integer representing the minimum steps.
示例1
输入
5 12
输出
3
说明
The minimum steps they should take: 5->7->10->12. Thus the answer is 3. 大写的门
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
And there are many different types of trees in HUST, each of which has a number represent its type. The doctors of biology in HUST find 4 different ways to change the tree’s type x into a new type y:
1. y=x+1
2. y=x-1
3. y=x+f(x)
4. y=x-f(x)
The function f(x) is defined as the number of 1 in x in binary representation. For example, f(1)=1, f(2)=1, f(3)=2, f(10)=2.
Now the doctors are given a tree of the type A. The doctors want to change its type into B. Because each step will cost a huge amount of money, you need to help them figure out the minimum steps to change the type of the tree into B.
Remember the type number should always be a natural number (0 included).
输入描述:
One line with two integers A and B, the init type and the target type.
输出描述:
You need to print a integer representing the minimum steps.
示例1
输入
5 12
输出
3
说明
The minimum steps they should take: 5->7->10->12. Thus the answer is 3. 大写的门
#include<bits/stdc++.h> using namespace std; const int MAX = 1e6 + 7; struct node { int x, s; } op, uv; int vis[MAX]; int f(int x) { int ans = 0; int xxx = 1; while(xxx <= x) { if(x & xxx) ans++; xxx <<= 1; } return ans; } bool judge(int x) { if(x >= 0 && x <= 1e7) return 1; return 0; } void bfs(int s, int t) { queue <node> q; memset(vis, 0, sizeof vis); op.x = s; op.s = 0; vis[s] = 1; q.push(op); while(!q.empty()) { op = q.front(); if(op.x == t) { printf("%d\n", op.s); return; } q.pop(); for(int i = 0; i < 4; i++) { if(i == 0) { uv.x = op.x + 1; if(!vis[uv.x] && judge(uv.x)) { vis[uv.x] = 1; uv.s = op.s + 1; q.push(uv); } } if(i == 1) { uv.x = op.x - 1; if(!vis[uv.x] && judge(uv.x)) { vis[uv.x] = 1; uv.s = op.s + 1; q.push(uv); } } if(i == 2) { int x = f(op.x); uv.x = op.x + x; if(!vis[uv.x] && judge(uv.x)) { vis[uv.x] = 1; uv.s = op.s + 1; q.push(uv); } } if(i == 3) { int x = f(op.x); uv.x = op.x - x; if(!vis[uv.x] && judge(uv.x)) { vis[uv.x] = 1; uv.s = op.s + 1; q.push(uv); } } } } } int main() { int n, m; scanf("%d%d", &n, &m); bfs(n, m); return 0; }