結果

問題 No.707 書道
ユーザー taotao54321taotao54321
提出日時 2018-09-28 21:18:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,331 bytes
コンパイル時間 2,159 ms
コンパイル使用メモリ 156,032 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 10:06:51
合計ジャッジ時間 1,410 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut tokens = $s.split_whitespace();
        input_inner! { tokens, $($r)* }
    };

    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut res = String::new();
            ::std::io::stdin().read_to_string(&mut res).unwrap();
            res
        };
        let mut tokens = s.split_whitespace();
        input_inner! { tokens, $($r)* }
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($tokens:expr)  => {};
    ($tokens:expr,) => {};

    ($tokens:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($tokens, $t);
        input_inner! { $tokens $($r)* }
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($tokens:expr, ( $($t:tt),* )) => {
        $(read_value!($tokens, $t)),*
    };

    ($tokens:expr, [ $t:tt; $len:expr ]) => {
        (0..$len).map(|_| read_value!($tokens, $t)).collect::<Vec<_>>()
    };

    ($tokens:expr, chars) => {
        read_value!($tokens, String).chars().collect::<Vec<_>>()
    };

    ($tokens:expr, usize1) => {
        read_value!($tokens, usize) - 1
    };

    ($tokens:expr, $t:ty) => {
        $tokens.next().unwrap().parse::<$t>().expect("parse error")
    };
}

use std::f64;

fn chmin<T>(xmin: &mut T, x: T) -> bool
    where T: PartialOrd + Copy
{
    if x < *xmin {
        *xmin = x;
        true
    }
    else {
        false
    }
}

fn dist(x1: i32, y1: i32, x2: i32, y2: i32) -> f64 {
    let x1 = f64::from(x1);
    let y1 = f64::from(y1);
    let x2 = f64::from(x2);
    let y2 = f64::from(y2);

    let dx = x1 - x2;
    let dy = y1 - y2;

    (dx*dx + dy*dy).sqrt()
}

fn main() {
    input! {
        H: i32,
        W: i32,
        M: [chars; H],
    }

    let it1 = (0..W).map(|x| ( x,-1));
    let it2 = (0..W).map(|x| ( x, H));
    let it3 = (0..H).map(|y| (-1, y));
    let it4 = (0..H).map(|y| ( W, y));
    let it = it1.chain(it2).chain(it3).chain(it4);

    let mut ans = f64::MAX;
    for (px,py) in it {
        let mut cur = 0.0;
        for y in 0..H { for x in 0..W {
            if M[y as usize][x as usize] == '1' {
                cur += dist(px,py,x,y);
            }
        }}
        chmin(&mut ans, cur);
    }

    println!("{}", ans);
}
0