103783: [Atcoder]ABC378 D - Count Simple Paths

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:15 Solved:0

Description

Score : $425$ points

Problem Statement

There is a grid of $H \times W$ cells. Let $(i, j)$ denote the cell at the $i$-th row from the top and the $j$-th column from the left.

Cell $(i, j)$ is empty if $S_{i,j}$ is ., and blocked if it is #.

Count the number of ways to start from an empty cell and make $K$ moves to adjacent cells (up, down, left, or right), without passing through blocked squares and not visiting the same cell more than once.

Specifically, count the number of sequences of length $K+1$, $((i_0, j_0), (i_1, j_1), \dots, (i_K, j_K))$, satisfying the following.

  • $1 \leq i_k \leq H$, $1 \leq j_k \leq W$, and $S_{i_k, j_k}$ is ., for each $0 \leq k \leq K$.
  • $|i_{k+1} - i_k| + |j_{k+1} - j_k| = 1$ for each $0 \leq k \leq K-1$.
  • $(i_k, j_k) \neq (i_l, j_l)$ for each $0 \leq k < l \leq K$.

Constraints

  • $1 \leq H, W \leq 10$
  • $1 \leq K \leq 11$
  • $H$, $W$, and $K$ are integers.
  • Each $S_{i,j}$ is . or #.
  • There is at least one empty cell.

Input

The input is given from Standard Input in the following format:

$H$ $W$ $K$
$S_{1,1}S_{1,2}\dots S_{1,W}$
$S_{2,1}S_{2,2}\dots S_{2,W}$
$\vdots$
$S_{H,1}S_{H,2}\dots S_{H,W}$

Output

Print the answer.


Sample Input 1

2 2 2
.#
..

Sample Output 1

2

Here are the two possible paths:

  • $(1,1) \rightarrow (2,1) \rightarrow (2,2)$
  • $(2,2) \rightarrow (2,1) \rightarrow (1,1)$

Sample Input 2

2 3 1
.#.
#.#

Sample Output 2

0

Sample Input 3

10 10 11
....#..#..
.#.....##.
..#...##..
...#......
......##..
..#......#
#........#
..##......
.###....#.
...#.....#

Sample Output 3

218070

Output

得分:425分

问题陈述

有一个$H \times W$单元格的网格。用$(i, j)$表示从顶部第$i$行和从左侧第$j$列的单元格。

如果$S_{i,j}$是.,则单元格$(i, j)$为空;如果是#,则为障碍。

计算从空单元格开始,进行$K$次移动到相邻单元格(上、下、左或右)的路径数量,不允许经过障碍方格,且每个单元格不得访问多次。

具体来说,计算满足以下条件的长度为$K+1$的序列数$((i_0, j_0), (i_1, j_1), \dots, (i_K, j_K))$。

  • 对于每个$0 \leq k \leq K$,有$1 \leq i_k \leq H$,$1 \leq j_k \leq W$,且$S_{i_k, j_k}$是.
  • 对于每个$0 \leq k \leq K-1$,有$|i_{k+1} - i_k| + |j_{k+1} - j_k| = 1$。
  • 对于每个$0 \leq k < l \leq K$,有$(i_k, j_k) \neq (i_l, j_l)$。

约束条件

  • $1 \leq H, W \leq 10$
  • $1 \leq K \leq 11$
  • $H$,$W$和$K$都是整数。
  • 每个$S_{i,j}$是.#
  • 至少有一个空单元格。

输入

输入从标准输入以下格式给出:

$H$ $W$ $K$
$S_{1,1}S_{1,2}\dots S_{1,W}$
$S_{2,1}S_{2,2}\dots S_{2,W}$
$\vdots$
$S_{H,1}S_{H,2}\dots S_{H,W}$

输出

打印答案。


示例输入1

2 2 2
.#
..

示例输出1

2

以下是两条可能的路径:

  • $(1,1) \rightarrow (2,1) \rightarrow (2,2)$
  • $(2,2) \rightarrow (2,1) \rightarrow (1,1)$

示例输入2

2 3 1
.#.
#.#

示例输出2

0

示例输入3

10 10 11
....#..#..
.#.....##.
..#...##..
...#......
......##..
..#......#
#........#
..##......
.###....#.
...#.....#

示例输出3

218070

加入题单

上一题 下一题 算法标签: