#![allow(unused_imports)] #![allow(non_snake_case)] use std::collections::HashMap; use std::collections::HashSet; #[allow(unused_macros)] macro_rules! read { ([$t:ty] ; $n:expr) => ((0..$n).map(|_| read!([$t])).collect::>()); ($($t:ty),+ ; $n:expr) => ((0..$n).map(|_| read!($($t),+)).collect::>()); ([$t:ty]) => (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::>()); ($t:ty) => (rl().parse::<$t>().unwrap()); ($($t:ty),*) => {{ let buf = rl(); let mut w = buf.split_whitespace(); ($(w.next().unwrap().parse::<$t>().unwrap()),*) }}; } #[allow(dead_code)] fn rl() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().to_owned() } fn main() { let dist = read!(i32, i32); let obst = read!(i32, i32); let ops = [(-1i32, -1i32), (-1, 0), (-1, 1), (0, 1), (0, -1), (1, -1), (1, 0), (1, 1)]; let mut open = vec![((0i32, 0i32), 0, obst.0.max(obst.1))]; let mut close = HashSet::new(); close.insert((0i32, 0i32)); while let Some((coord, g, _)) = open.pop() { if coord.0 == dist.0 && coord.1 == dist.1 { println!("{}", g); return; } for op in ops.iter() { let new_coord = (op.0 + coord.0, op.1 + coord.1); if new_coord.0 == obst.0 && new_coord.1 == obst.1 { continue; } if close.insert(new_coord) { open.push((new_coord, g + 1, (new_coord.0 - dist.0).abs().max((new_coord.1-dist.1).abs()))); } } open.sort_by_key(|(_, g, h)| std::cmp::Reverse(g+h)); } }