Give a number Tower, then walk along the edge between the numbers, give two numbers, and ask the shortest length of the path.
It seems like a search question, but anyone who has some experience in question-making knows that this is not a search question, and direct search will definitely time out.
This is a mathematical question based on regular calculation.
My idea here is to perform a layer-by-layer search and use the layer-by-layer regular acceleration to achieve layer hops. The results can be directly calculated at the same layer or on the same diagonal column. The diagonal column is the column that can be directly directed to the target along the triangle edge.
Math calculates two differences between the layer and the layer, and can also use this rule to calculate the layer and column where N and m are located.
This is a bit of trouble, but I have figured it out myself. It's a good idea. O (∩) O Haha ~
I checked the problem of others, this blog uses the method of fixed coordinates: http://gisyhy.blog.163.com/blog/static/129390343201032311220935/ is very clever.
It is more difficult to summarize the abstract, but the code is simpler.
# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h >#include <stack >#include <queue >#include <set> # include <map> using namespace STD; int main () {int n, m; while (scanf ("% d", & N, & M )! = EOF) {If (n> m) Swap (n, m); long T = 1; int I = 1; // number of cells in the row where the bucket is located while (T <n) {I + = 2; t = (long (I + 1)> 1ll) * (long (I + 1)> 1ll);} long d = 1; Int J = 1; while (d <m) {J + = 2; D = (long (J + 1)> 1ll) * (long (J + 1)> 1ll);} // The first line, then take the high round int linen = int (long) N-(t-(long) I )); // The number of int linem = int (long) m-(D-(long) J) in the row); // int step = 0 at the upper and lower layers of each layer; while (linen! = Linem & I-linen! = J-linem & I! = J) {step ++; If (linen & 1) // The base layer goes down to an even number {linen ++; I + = 2 ;} else // even number, move a single digit {If (linem <linen) linen --; else linen ++ ;}} int HI = (I + 1) >>> 1; int HJ = (J + 1)> 1; // if (I = J) Step + = ABS (linem-linen ); else step + = (HJ-Hi) <1); printf ("% d \ n", step);} return 0 ;}
HDU 1030 Delta-wave mathematical problem