結果

問題 No.3806 Crossword Key with Cropping (Easy)
コンテスト
ユーザー surumeika1987
提出日時 2026-11-24 10:21:33
言語 Rust
(1.99.1 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
WA  
実行時間 -
コード長 4,042 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,083 ms
コンパイル使用メモリ 167,764 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-11-24 10:22:22
合計ジャッジ時間 47,994 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge6_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 3 WA * 45 TLE * 5
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `h`
  --> src/main.rs:89:23
   |
89 | fn get_key_last_pos_h(h: u32, w: u32, v: &Vec<bool>, y: u32, x: u32) -> Option<(u32, u32)> {
   |                       ^ help: if this is intentional, prefix it with an underscore: `_h`
   |
   = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

ソースコード

diff #
raw source code

use std::io::stdin;

fn main() {
    let (h, w) = input_size();
    let board = input_board(h, w);
    
    let mut keys: Vec<(u32, u32, u32, u32)> = Vec::new();
    for i in 0..h {
        for j in 0..w {
            let (hk, vk) = get_key_last_pos(h, w, &board, i, j);
            
            if let Some((y, x)) = hk {
                keys.push((i, j, y, x))
            }

            if let Some((y, x)) = vk {
                keys.push((i, j, y, x))
            }
        }
    }

    
    let q = input_num();
    for _ in 0..q {
        let mut is_key = vec![false; (w*h) as usize];
        let (y1, x1, y2, x2) = input_query();
        for (y3, x3, y4, x4) in &keys {
            let (y3, x3, y4, x4) = (*y3, *x3, *y4, *x4);
            let is_h = y3 == y4;
            if x1 <= x3 && x3 <= x2 && y1 <= y3 && y3 <= y2 {
                is_key[(y3 * w + x3) as usize] = true;
            } else if x1 <= x4 && x4 <= x2 && y1 <= y4 && y4 <= y2 {
                if is_h {
                    is_key[(y3 * w + x1) as usize] = true;
                } else {
                    is_key[(y1 * w + x3) as usize] = true;
                }
            } else if (x3 <= x1 && x1 <= x4 && y3 <= y1 && y1 <= y4) || (x3 <= x2 && x2 <= x4 && y3 <= y2 && y2 <= y4) {
                if is_h {
                    is_key[(y3 * w + x1) as usize] = true;
                } else {
                    is_key[(y1 * w + x3) as usize] = true;
                }
            }
        }
        println!("{}", is_key.into_iter().filter(|v| *v).count());
    }
}

fn input_size() -> (u32, u32) {
    let mut a = String::new();
    stdin().read_line(&mut a).unwrap();
    let v: Vec<u32> = a.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect();
    (v[0], v[1])
}

fn input_board(h: u32, w: u32) -> Vec<bool> {
    let mut board = vec![false; (w*h) as usize];
    for i in 0..h {
        let mut a = String::new();
        stdin().read_line(&mut a).unwrap();
        let c: Vec<char> = a.chars().collect();
        for j in 0..w {
            if c[j as usize] == '.' {
                board[(i * w + j) as usize] = true;
            }
        }
    }
    board
}

fn input_num() -> u32 {
    let mut a = String::new();
    stdin().read_line(&mut a).unwrap();
    a.trim().parse().ok().unwrap()
}

fn input_query() -> (u32, u32, u32, u32) {
    let mut a = String::new();
    stdin().read_line(&mut a).unwrap();
    let v: Vec<u32> = a.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect();
    (v[0] - 1, v[1] - 1, v[2] - 1, v[3] - 1)
}

fn get_key_last_pos(h: u32, w: u32, v: &Vec<bool>, y: u32, x: u32) -> (Option<(u32, u32)>, Option<(u32, u32)>) {
    return (get_key_last_pos_h(h, w, v, y, x), get_key_last_pos_v(h, w, v, y, x));
}

fn get_key_last_pos_h(h: u32, w: u32, v: &Vec<bool>, y: u32, x: u32) -> Option<(u32, u32)> {
    // 横
    // 対象マスが黒か
    if v[(y * w + x) as usize] == false {
        return None;
    }
    // 対象マスの左が白か
    if x != 0  && v[(y * w + (x - 1)) as usize] == true {
        return None;
    }
    // 対象マスの右が盤面外又は黒か
    if w <= x + 1 || v[(y * w + (x + 1)) as usize] == false {
        return None;
    }
    let mut vx = 0;
    while x + vx < w {
        if v[(y * w + (x + vx)) as usize] == false {
            break;
        }
        vx += 1;
    }
    return Some((y, x + vx - 1));
}

fn get_key_last_pos_v(h: u32, w: u32, v: &Vec<bool>, y: u32, x: u32) -> Option<(u32, u32)> {
    // 縦
    // 対象マスが黒か
    if v[(y * w + x) as usize] == false {
        return None;
    }
    // 対象マスの上が白か
    if y != 0 && v[((y - 1) * w + x) as usize] == true {
        return None;
    }
    // 対象マスの下が盤面外又は黒か
    if h <= y + 1 || v[((y + 1) * w + x) as usize] == false {
        return None;
    }
    let mut vy = 0;
    while y + vy < h {
        if v[((y + vy) * w + x) as usize] == false {
            break;
        }
        vy += 1;
    }
    return Some((y + vy - 1, x));
}
0