HDU 1030 Delta-wave mathematical problem, hdudelta-wave
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 calculated according to the law.
My idea here is to perform a layer-by-layer search and use the layer-by-layer regular acceleration to achieve layer hops. When the layer or the same diagonal column is reached, the results can be directly calculated. The diagonal column is the column that can be directly directed to the target along the triangle edge.
The math calculates two differences between the layer and the layer. This rule can also be used to calculate the layer and column where N and M are located.
This is a little troublesome, but I have come up with 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.
He has a higher degree of abstraction, which makes it more difficult to summarize, 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 acm 3723 delta wave
Search for csnd