Last active
June 22, 2021 19:28
-
-
Save tokugh/0db242aefd61ad64b47eddfb86e3c806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::Cell; | |
use proconio::{input, marker::Chars}; | |
#[derive(Debug)] | |
struct GridGraph { | |
h: usize, | |
w: usize, | |
c: Vec<Vec<char>>, | |
} | |
impl GridGraph { | |
fn neighbors(&self, (i, j): (usize, usize)) -> Vec<(usize, usize)> { | |
let mut res = vec![]; | |
if i > 0 && self.c[i-1][j] == '.' { res.push((i-1, j)); } | |
if i < self.h-1 && self.c[i+1][j] == '.' { res.push((i+1, j)); } | |
if j > 0 && self.c[i][j-1] == '.' { res.push((i, j-1)); } | |
if j < self.w-1 && self.c[i][j+1] == '.' { res.push((i, j+1)); } | |
res | |
} | |
} | |
struct Dfs { | |
vm: Vec<Vec<Cell<bool>>>, | |
steps: Cell<usize>, | |
maxsteps: Cell<usize>, | |
} | |
impl Dfs { | |
fn new(h: usize, w: usize) -> Self { | |
Self { vm: vec![vec![Cell::new(false); w]; h], steps: Cell::new(0), maxsteps: Cell::new(0), } | |
} | |
fn dfs(&self, g: &GridGraph, (i, j): (usize, usize), (si, sj): (usize, usize)) { | |
self.steps.set(self.steps.get() + 1); | |
self.vm[i][j].set(true); | |
for (x, y) in g.neighbors( (i, j) ) { | |
if x == si && y == sj { | |
self.maxsteps.set( self.maxsteps.get().max( self.steps.get() ) ); | |
continue; | |
} | |
if self.vm[x][y].get() { continue; } | |
self.dfs(g, (x, y), (si, sj)); | |
} | |
self.steps.set(self.steps.get() - 1); | |
self.vm[i][j].set(false); | |
} | |
} | |
fn main() { | |
input! { h: usize, w: usize, c: [Chars; h], }; | |
let g = GridGraph { h, w, c, }; | |
let mut ans = 0; | |
for si in 0..h { | |
for sj in 0..w { | |
if g.c[si][sj] == '#' { continue; } | |
let dfs = Dfs::new(h, w); | |
dfs.dfs(&g, (si, sj), (si, sj)); | |
ans = ans.max( dfs.maxsteps.get() ); | |
} | |
} | |
if ans <= 2 { | |
println!("-1"); | |
} else { | |
println!("{}", ans); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use proconio::{input, marker::Chars}; | |
static mut vm: [[bool; 16]; 16] = [[false; 16]; 16]; | |
static mut steps: usize = 0usize; | |
static mut maxsteps: usize = 0usize; | |
#[derive(Debug)] | |
struct GridGraph { | |
h: usize, | |
w: usize, | |
c: Vec<Vec<char>>, | |
} | |
impl GridGraph { | |
fn neighbors(&self, (i, j): (usize, usize)) -> Vec<(usize, usize)> { | |
let mut res = vec![]; | |
if i > 0 && self.c[i-1][j] == '.' { res.push((i-1, j)); } | |
if i < self.h-1 && self.c[i+1][j] == '.' { res.push((i+1, j)); } | |
if j > 0 && self.c[i][j-1] == '.' { res.push((i, j-1)); } | |
if j < self.w-1 && self.c[i][j+1] == '.' { res.push((i, j+1)); } | |
res | |
} | |
} | |
fn dfs(g: &GridGraph, (i, j): (usize, usize), (si, sj): (usize, usize)) { | |
unsafe { steps += 1; vm[i][j] = true; } | |
for (x, y) in g.neighbors( (i, j) ) { | |
if x == si && y == sj { | |
unsafe { maxsteps = maxsteps.max(steps); } | |
continue; | |
} | |
if unsafe{vm[x][y]} { continue; } | |
dfs(g, (x, y), (si, sj)); | |
} | |
unsafe { steps -= 1; vm[i][j] = false; } | |
} | |
fn main() { | |
input! { h: usize, w: usize, c: [Chars; h], }; | |
let g = GridGraph { h, w, c, }; | |
let mut ans = 0; | |
for si in 0..h { | |
for sj in 0..w { | |
if g.c[si][sj] == '#' { continue; } | |
unsafe { steps = 0; maxsteps = 0; } | |
dfs(&g, (si, sj), (si, sj)); | |
ans = ans.max(unsafe{maxsteps}); | |
} | |
} | |
if ans <= 2 { | |
println!("-1"); | |
} else { | |
println!("{}", ans); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment