Rust で Dijkstra

こんな感じかなあ。負にならない値はできるだけ usize を使うと、Vec へのアクセスでキャストせずに済んでよさそう? 動くコード:https://ideone.com/3yRoBI

use std::collections::BinaryHeap;

type Cell = (usize, usize);

#[inline]
fn neighbors((x,y): Cell, h: usize, w: usize) -> Vec<Cell> {
    let mut res = vec![];
    if x + 1 < h {
        res.push((x+1, y));
    }
    if y + 1 < w {
        res.push((x, y+1));
    }
    if x > 0 {
        res.push((x-1, y));
    }
    if y > 0 {
        res.push((x, y-1));
    }
    res
}

type State = (usize, Cell);

fn main() {
    let maze = vec![
        ".#....",
        "...##.",
        "#.#...",
        "..#.#.",
        ".#...#",
        "...#.."
    ];
    let start = (0,0);
    let h = maze.len();
    let w = maze[0].len();
    let goal = (h-1, w-1);
    
    let mut queue = BinaryHeap::<State>::new();
    queue.push((0, start));
    let inf = 100;
    let mut steps = vec![vec![inf; w]; h];
    loop {
        match queue.pop() {
            None => break,
            Some((step, c)) => {
                // これが気になる
                if maze[c.0].chars().nth(c.1) == Some('#') || steps[c.0][c.1] < inf {
                    continue;
                }
                steps[c.0][c.1] = step;
                for nc in neighbors(c, h, w) {
                    queue.push((step + 1, nc));
                }
            }
        }
    }
    println!("{}", steps[goal.0][goal.1]);
}