# 描述

Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room’s floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below… Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety… but there’s a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.

## Input

The input file will begin with a line containing a single integer representing the number of test cases, which is at most $25$. Each test case will begin with a line containing a single positive integer $n$ representing the number of rows in the map, followed by a single non-negative integer $d$ representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit ($0-3$) in each position representing the number of jumps the pillar in that position will sustain before collapsing ($0$ means there is no pillar there). The second map will follow, with an L for every position where a lizard is on the pillar and a . for every empty pillar. There will never be a lizard on a position where there is no pillar. Each input map is guaranteed to be a rectangle of size $n \times m$, where $1 ≤ n ≤ 20$ and $1 ≤ m ≤ 20$. The leaping distance is always $1 ≤ d ≤ 3$.

## Output

For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.

# 思路

• 题意：每一个人都可以最长跳$d$的距离，对于每一个柱子有一个限定条件：只能从这里跳出去多少次。问有几个人不能跳到图的外面。
• 最大流建图，难点在于如何建图。将图的外面作为汇点，如果可以直接从柱子跳出去，就直接连到汇点。否则我们将它连到能够跳到的柱子。但是这样建图存在一个问题：如果这个柱子可以跳到旁边很多的点上，每一个边的容量都是这个限定次数的话，等于限定的次数被放大了。这样跑出的最大流显然是错误的。
• 考虑拆点，那么就考虑拆点，将每个点拆成两个，$1→2$连一条边容量为限定的次数，如果能跳到别的点，就把$2$连到那个点的$1$，容量为$+\infty$。
• 这样就可以限定每个点跳出去的次数，跑最大流即可。
• PS：点的总数最大会到$800$，数组开小贡献一次RE。

0%