結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
_yoshih_
|
| 提出日時 | 2018-10-01 00:53:57 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 5,000 ms |
| コード長 | 1,233 bytes |
| コンパイル時間 | 12,255 ms |
| コンパイル使用メモリ | 394,220 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 09:08:25 |
| 合計ジャッジ時間 | 13,482 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
use std::collections::VecDeque;
use std::cmp::min;
use std::io::*;
use std::str::*;
fn read<T: FromStr>(sl: &mut StdinLock) -> Option<T> {
let s = sl.by_ref().bytes().map(|c| c.unwrap() as char)
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect::<String>();
s.parse::<T>().ok()
}
fn run(sl: &mut StdinLock) {
let n = read::<i32>(sl).unwrap();
let bits = (0..=n).map(|i| i.count_ones() as i32).collect::<Vec<_>>();
let mut done = vec![false; bits.len()];
let mut q = VecDeque::new();
q.push_back((1, 1));
const CANNOT_REACH: i32 = 10001;
let mut result = CANNOT_REACH;
while !q.is_empty() {
let (pos, count) = q.pop_front().unwrap();
if pos == n {
result = min(result, count);
continue;
}
if pos < 1 || pos > n || done[pos as usize] {
continue;
}
done[pos as usize] = true;
q.push_back((pos + bits[pos as usize], count + 1));
q.push_back((pos - bits[pos as usize], count + 1));
}
println!("{}", if result == CANNOT_REACH {-1} else {result});
}
fn main() {
let s = stdin();
let mut sl = s.lock();
run(&mut sl);
}
_yoshih_