hdu 1030

来源:互联网 发布:unity3d如何导入模型 编辑:程序博客网 时间:2024/05/21 17:33

本题我用的方法是模拟.

#include <iostream>#include <math.h>inline int GetLine(int m){double sqrtm = sqrt((long double)m);if ((sqrtm - (int)sqrtm) > 0){return (int)sqrtm + 1;}else{return (int)sqrtm;}}inline bool UpInLine(int m){// Get line number of mint line = GetLine(m);if (line % 2 != 0){// if line is odd, odd down, even upreturn (m % 2 == 0) ? true : false;}else{// if line is even, odd up, even downreturn (m % 2 == 0) ? false : true;}}inline bool SameLine(int m, int n){return GetLine(m) == GetLine(n);}// is m is left to ninline bool DirectLeft(int m, int n){// Move m to the line same as nint linem = GetLine(m);int linen = GetLine(n);int movem = m;while (linem < linen){movem += 2 * linem;linem++;}return (movem < n) ? true : false;}inline int MoveDown(int m){return m + 2 * GetLine(m);}inline int MoveLeft(int m){return m - 1;}inline int MoveRight(int m){return m + 1;}int main(){int m, n;while (std::cin >> m >> n){// let m < nif (m > n){int tmp = m;m = n;n = tmp;}int count = 0;// up to downwhile (m != n){if (GetLine(m) != GetLine(n)){// if m, n is not in the same lineif (UpInLine(m)){// if m is up in its line, move left or right, by it's direct to nif (DirectLeft(m, n)){// Move Rightm = MoveRight(m);}else{m = MoveLeft(m);}}else{// if m is down in its line, move downm = MoveDown(m);}}else{// if m, n is in the same line// direct move m left or right by it's direct to nif (DirectLeft(m, n)){m = MoveRight(m);}else{m = MoveLeft(m);}}count++;}printf("%d\n", count);}return 0;}