Files
CodeTest/baekjoon/직사각형에서_탈출/solution_18509701.rs
2024-08-29 16:22:36 +09:00

68 lines
1.5 KiB
Rust

use std::io;
struct Rectangle{
width: u32,
height: u32,
}
struct Position{
x: u32,
y: u32,
}
impl Position{
fn init(x: u32, y: u32) -> Position{
Position{
x,
y,
}
}
}
impl Rectangle{
fn init(width: u32, height: u32) -> Rectangle{
Rectangle{
width,
height,
}
}
fn shortest_path(self, pos: Position) -> u32{
let mut dx : [u32; 4] = [0 as u32; 4];
dx[0] = pos.x;
dx[1] = pos.y;
dx[2] = self.width - pos.x;
dx[3] = self.height - pos.y;
let mut min = 1000;
for i in 0..4{
if min > dx[i]{
min = dx[i];
}
}
min
}
}
fn main(){
let mut str_iter = String::new();
io::stdin().read_line(&mut str_iter)
.expect("Failed to read line");
let mut str_iter = str_iter.trim().split(" ");
let x = str_iter.next().expect("Not enough inputs")
.parse::<u32>().expect("It's not an integer");
let y = str_iter.next().expect("Not enough inputs")
.parse::<u32>().expect("It's not an integer");
let w = str_iter.next().expect("Not enough inputs")
.parse::<u32>().expect("It's not an integer");
let h = str_iter.next().expect("Not enough inputs")
.parse::<u32>().expect("It's not an integer");
let pos = Position::init(x, y);
let rect = Rectangle::init(w, h);
println!("{}", rect.shortest_path(pos));
}