Skip to content

Commit 75822aa

Browse files
committed
add abc420d.rs
1 parent 60fcd0d commit 75822aa

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

src/abc/abc420d.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/** THIS IS AN OUTPUT FILE. NOT EDIT THIS FILE DIRECTLY. **/
2+
use proconio::input;
3+
use proconio::marker::*;
4+
use std::marker::PhantomData;
5+
use std::cmp::*;
6+
use std::collections::*;
7+
8+
fn check(rows: &Vec<Vec<char>>, ni:usize, nj:usize, p:usize) -> usize {
9+
let c = rows[ni][nj];
10+
if c == '#' {
11+
0
12+
} else {
13+
if c == 'o' && p % 2 == 1 {
14+
0
15+
} else if c == 'x' && p % 2 == 0 {
16+
0
17+
} else if c == '?' {
18+
1
19+
} else {
20+
2
21+
}
22+
}
23+
}
24+
25+
fn main() {
26+
input! {
27+
h:usize,
28+
w:usize,
29+
rows:[Chars;h]
30+
}
31+
32+
let inf = 1_000_000_000usize;
33+
let mut memo = vec![vec![vec![inf;w];h];2];
34+
35+
let mut stack = vec![];
36+
let mut goal = (0,0);
37+
for i in 0..h {
38+
for j in 0..w {
39+
if rows[i][j] == 'S' {
40+
stack.push((i,j,0,0));
41+
memo[0][i][j] = 0;
42+
}
43+
if rows[i][j] == 'G' {
44+
goal = (i,j);
45+
}
46+
}
47+
}
48+
49+
while !stack.is_empty() {
50+
let mut new_stack = vec![];
51+
while let Some((ci,cj,p,cv)) = stack.pop() {
52+
let nv = cv + 1;
53+
if 0 < ci {
54+
let a = check(&rows, ci-1,cj, p);
55+
let np = (p + a) % 2;
56+
if memo[np][ci-1][cj] > nv && a > 0 {
57+
memo[np][ci-1][cj] = nv;
58+
new_stack.push((ci-1,cj,np,nv));
59+
}
60+
}
61+
62+
if ci < h-1 {
63+
let a = check(&rows, ci+1, cj, p);
64+
let np = (p+a) % 2;
65+
if memo[np][ci+1][cj] > nv && a > 0 {
66+
memo[np][ci+1][cj] = nv;
67+
new_stack.push((ci+1,cj,np,nv));
68+
}
69+
}
70+
71+
if 0 < cj {
72+
let a = check(&rows, ci, cj-1, p);
73+
let np = (p+a) % 2;
74+
if memo[np][ci][cj-1] > nv && a > 0 {
75+
memo[np][ci][cj-1] = nv;
76+
new_stack.push((ci,cj-1,np,nv));
77+
}
78+
}
79+
80+
if cj < w-1 {
81+
let a = check(&rows, ci, cj+1, p);
82+
let np = (p+a) % 2;
83+
if memo[np][ci][cj+1] > nv && a > 0 {
84+
memo[np][ci][cj+1] = nv;
85+
new_stack.push((ci,cj+1,np,nv));
86+
}
87+
}
88+
}
89+
stack = new_stack;
90+
}
91+
92+
if memo[0][goal.0][goal.1] < inf || memo[1][goal.0][goal.1] < inf {
93+
println!("{}", min(memo[0][goal.0][goal.1], memo[1][goal.0][goal.1]));
94+
} else {
95+
println!("-1");
96+
}
97+
}

0 commit comments

Comments
 (0)