CF1753D The Beach

题目描述

## 题目大意 有一个 $n\times m$ 的网格图,场地上有许多不可移动的障碍,标记为 `#`,还有一些可以移动的障碍,如果他是横着的,定义它的一边为 `L`,另一边为 `R`;否则定义它的上边为 `U`,下边为 `D`,现在需要在这个网格图中空出一个 $1\times 2$ 的空位,可以移动的障碍移动如下: - 选择固定它的一边,将其旋转 $90^\degree$,花费 $p$ 单位,前提是旋转后不准与其他障碍重合。 - 将其平移一个单位,花费 $q$ 单位,前提是平移后不准与其他障碍重合。 现在要求出在这个网格图里空出 $1\times 2$ 的空位,至少需要花费多少单位,如果不可能,输出 `-1`。

输入格式

第一行两个整数 $1\leqslant n,m\leqslant 3\times 10^5,n\times m\leqslant 3\times 10^5$,表示这个网格的大小。 第二行两个整数 $1\leqslant p,q\leqslant 10^9$,如题意所述。 接下来 $n$ 行,每行 $m$ 个字符,每个字符为 `.`(表示空位)、`#`(表示不可移动障碍)、`L`、`R`、`U`、`D`(分别表示可移动的障碍是如何摆放的)中的一种。

输出格式

输出一个整数表示最小花费,如果不存在这样的方案,输出 `-1`。 ## 样例解释 第一个样例中可以把位于 $(1,2),(1,3)$ 的障碍往左移动一格,$(2,3),(2,4)$ 的障碍往右移动一格,花费 $2q=4$。 第二个样例中无论如何移动障碍都不可能空出 $1\times 2$ 的空位,输出 `-1`。

说明/提示

In the first example we can shift upper sunbed to the left and lower sunbed — to the right. Andrew will be able to put his sunbed vertically in the middle of the beach. We well cause $ 2 + 2 = 4 $ units of discomfort. It is easy to prove that it is an optimal answer. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1753D/068dcd3f383e5c13a23b270ba14b23f29dcfb57a.png)![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1753D/e96ec982370c391087a31ef1cfeb944b9fd4ac91.png)![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1753D/e2807d62f101bfa5c0609da1577d4e543cbce572.png)Optimal strategy in the first example (Andrew's sunbed is colored white). In the second example it is impossible to free a space for Andrew's sunbed. All possible states of the beach after any rotates and shifts are illustrated in the problem statement.