Test instructions
, numbering these squares according to the pattern in the diagram. The number of two squares is given, and the minimum number of steps from one lattice to another is obtained. (One step can only pass through a lattice with a common side)
Analysis:
According to high school mathematics knowledge, select any two non-collinear vector, it can represent all the points on the plane.
Set up a coordinate system like this, and calculate the coordinates of all points according to the rules in the diagram.
It's not hard to find the rules or the reasoning.
- The point in the first to third quadrant (x, y) is |x| from the origin. + |y|
- The point in the second to fourth quadrant (x, y) is max{|x|, |y|} from the origin.
The coordinates of the recurrence point is also more attention to detail, or it is easy to make mistakes.
1#include <cstdio>2#include <cmath>3#include <algorithm>4 using namespacestd;5 Const intMAXN =10000;6 7 struct Point8 {9 intx, y;TenPoint (intx=0,inty=0): X (x), Y (y) {} One}P[MAXN + the]; A - intDx[] = {-1, -1,0,1,1,0};//Six Directions - intDy[] = {0,1,1,0, -1, -1}; the intPos; - - voidCalintDirintL//recursively pushes the coordinates of L-grids in dir direction - { +pos++; - while(l--) + { AP[pos] = Point (p[pos-1].x+dx[dir], p[pos-1].y+Dy[dir]); atpos++; - } -pos--; - } - - voidInit () in { -p[2] = Point (1, -1); topos =2; + for(intL =1; L <= -; ++l) - { the for(intDIR =0; Dir <4; ++dir) * Cal (dir, l); $Cal4, L +1);Panax NotoginsengCal5, L); - } the } + A intMain () the { + Init (); - $ intN, M; $ while(SCANF ("%d%d", &n, &m) = =2&&N) - { - intx = p[n].x-p[m].x; the inty = p[n].y-p[m].y; - intans;Wuyi if((x<0&&y>0) || (x>0&&y<0)) ans =Max (ABS (x), ABS (y)); the ElseAns = ABS (x+y); - Wuprintf"The distance between cells%d and%d are%d.\n", N, M, ans); - } About $ return 0; -}
code June
UVa 808 (Build coordinate system, find law) Bee breeding