Source: http://poj.org/problem? Id = 3278
Question: How many steps are required from the start point to the end point for a start point and an end point. You can take one step to the left, one step to the right, or the coordinate is multiplied by 2.
Idea: a raw BFs, a water question.
Code:
# include
# include
# include
# include
using namespace STD; # define CLR (ARR, Val) memset (ARR, Val, sizeof (ARR) const int n = 100000; int flag [N]; struct point {int num, CNT;} PP [n + 5]; int BFS (INT begin, int end) {queue
QQ; CLR (flag, 0 ); QQ. push (BEGIN); flag [begin] = 1; PP [begin]. CNT = 0; while (1) {int x = QQ. front (); QQ. pop (); // printf ("x = % d \ n", x); If (x = END) break; If (X-1> = 0 & X-1 <= N &&! Flag [x-1]) {QQ. push (x-1); flag [x-1] = true; PP [x-1]. CNT = PP [X]. CNT + 1 ;}if (x + 1> = 0 & x + 1 <= N &&! Flag [x + 1]) {QQ. push (x + 1); flag [x + 1] = true; PP [x + 1]. CNT = PP [X]. CNT + 1 ;}if (2 * x> = 0 & 2 * x <= N &&! Flag [2 * x]) {QQ. push (2 * X); flag [2 * x] = true; PP [2 * X]. CNT = PP [X]. CNT + 1 ;}} return PP [end]. CNT;} int main () {int begin, end; while (scanf ("% d", & begin, & End )! = EOF) {int x = BFS (begin, end); printf ("% d \ n", x) ;}return 0 ;}