use proconio::{input, marker::Chars}; fn main() { input! { n: usize, // グリッドサイズ s: [Chars; n], // グリッドにある文字列 } // dfs let mut cnt = 0; let mut select = Vec::new(); dfs(n, &s, 0, 0, &mut select, &mut cnt); println!("{}", cnt); } fn dfs(n: usize, s: &Vec>, row: usize, col: usize, select: &mut Vec, cnt: &mut usize, ) { // 文字選択 select.push(s[row][col]); // すべて通った if row == n-1 && col == n-1 { // println!("{}", select.iter().join(" ")); // 回文判定 if iskaibun(&select) { // 回文なら個数をカウント *cnt += 1; } return; } // 右か下へ移動 if row != n-1 { dfs(n, s, row+1, col, select, cnt); // 戻す select.pop(); } if col != n-1 { dfs(n, s, row, col+1, select, cnt); // 戻す select.pop(); } } // 回文 fn iskaibun(s: &Vec) -> bool { let sz = s.len(); for i in 0..sz / 2 { if s[i] != s[sz - 1 - i] { return false; } } true }