#define _USE_MATH_DEFINES #include #include using namespace std; bool safe(int, int, int, int); struct Point { int x; int y; }; Point now = { 0, 0 }; Point goal; Point obst; int main(int argc, char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> goal.x >> goal.y >> obst.x >> obst.y; int dirx; int diry; int flag; int cnt = 0; while (1) { dirx = goal.x - now.x; //dirx>0 で右に進むべき diry = goal.y - now.y; //diry>0 で左に進むべき flag = (abs(dirx) > abs(diry)); if (dirx > 0) { if (diry > 0) { // 右上 if (safe(now.x, now.y, dirx, diry)) { now.x++; now.y++; } else { now.x += flag; now.y += !flag; } } else if (diry < 0) { // 右下 if (safe(now.x, now.y, dirx, diry)) { now.x++; now.y--; } else { now.x += flag; now.y += !flag; } } else if (diry == 0) { // 右 now.x++; } } else if (dirx < 0) { if (diry > 0) { // 左上 if (safe(now.x, now.y, dirx, diry)) { now.x--; now.y++; } else { now.x += flag; now.y += !flag; } } else if (diry < 0) { // 左下 if (safe(now.x, now.y, dirx, diry)) { now.x--; now.y--; } else { now.x += flag; now.y += !flag; } } else if (diry == 0) { // 左 now.x--; } } else if (dirx == 0) { if (diry > 0) { // 上 now.y++; } else if (diry < 0) { // 下 now.y--; } else if (diry == 0) { // 到着 break; } } cnt++; } printf("%d\n", cnt); return 0; } bool safe(int nowx, int nowy, int dirx, int diry) { bool toRight = (dirx > 0); bool toLeft = !toRight; bool toUp = (diry > 0); bool toBottom = !toUp; bool safe; bool goalNext; if (toRight && toUp) { safe = !((nowx + 2 == obst.x) && (nowy + 2 == obst.y)); goalNext = (now.x + 1 == goal.x) && (nowy + 1 == goal.y); } else if (toRight && toBottom) { safe = !((nowx + 2 == obst.x) && (nowy - 2 == obst.y)); goalNext = (now.x + 1 == goal.x) && (nowy -1 == goal.y); } else if (toLeft && toUp) { safe = !((nowx - 2 == obst.x) && (nowy + 2 == obst.y)); goalNext = (nowx - 1 == goal.x) && (nowy + 1 == goal.y); } else { safe = !((nowx - 2 == obst.x) && (nowy - 2 == obst.y)); goalNext = (nowx - 1 == goal.x) && (nowy - 1 == goal.y); } return safe || goalNext; }