题库练习 The Beach
← 上一题 下一题 →

A15533 | The Beach

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Andrew loves the sea. That's why, at the height of the summer season, he decided to go to the beach, taking a sunbed with him to sunbathe.

The beach is a rectangular field with $n$ rows and $m$ columns. Some cells of the beach are free, some have roads, stones, shops and other non-movable objects. Some of two adjacent along the side cells can have sunbeds located either horizontally or vertically.

Andrew hopes to put his sunbed somewhere, but that's a bad luck, there may no longer be free places for him! That's why Andrew asked you to help him to find a free place for his sunbed. Andrew's sunbed also should be places on two adjacent cells.

If there are no two adjacent free cells, then in order to free some place for a sunbed, you will have to disturb other tourists. You can do the following actions:

- Come to some sunbed and, after causing $p$ units of discomfort to its owner, lift the sunbed by one of its sides and rotate it by $90$ degrees. One half of the sunbed must remain in the same cell and another half of the sunbed must move to the free cell. At the same time, anything could be on the way of a sunbed during the rotation . ![](/uploads/luogu/CF1753D/09cf95acda528113fcf87f6bc31ab7d9580f9b23_67a22dccd9e0.png)![](/uploads/luogu/CF1753D/e96ec982370c391087a31ef1cfeb944b9fd4ac91_9fc2bec3db36.png)![](/uploads/acgo/image/4ffc7631600fc4b2_ed027c26c755.jpeg)Rotation of the sunbed by $90$ degrees around cell $(1, 2)$ .
- Come to some sunbed and, after causing $q$ units of discomfort to its owner, shift the sunbed along its long side by one cell. One half of the sunbed must move to the place of another, and another — to the free cell. ![](/uploads/luogu/CF1753D/09cf95acda528113fcf87f6bc31ab7d9580f9b23_67a22dccd9e0.png)![](/uploads/luogu/CF1753D/e96ec982370c391087a31ef1cfeb944b9fd4ac91_9fc2bec3db36.png)![](/uploads/acgo/image/76711722fcf4bdff_dab81e0b75d3.jpeg)Shift of the sunbed by one cell to the right.

In any moment each sunbed occupies two adjacent free cells. You cannot move more than one sunbed at a time.

Help Andrew to free a space for his sunbed, causing the minimum possible number of units of discomfort to other tourists, or detect that it is impossible.

输入格式

The first line contains two integers $n$ and $m$ ( $1 \le n, m \le 300\,000$ , $1 \le n \cdot m \le 300\,000$ ) — the number of rows and columns in rectangle.

The second line contains two integers $p$ and $q$ ( $1 \le p, q \le 10^9$ ) — the number of units of discomfort caused by rotation and shift of a sunbed, respectively.

Each of the following $n$ lines contains $m$ characters, describing cells of the rectangle. Each lines consists of characters "L", "R", "D", "U", "." and "\#", denoting the type of the cell. Characters "L", "R", "D" and "U" denote a half of a sunbed placed in the cell — left, right, bottom and top half, respectively. Character "." denotes a free cell and character "\#" — a cell, occupied by some non-movable object.

输出格式

Print one integer — the minimum possible number of units of discomfort, caused to other tourists, to free a space for a sunbed. If it is impossible to free a space for a sunbed, print $-1$ .

输入输出样例

输入 #1
2 5
5 2
.LR##
##LR.
输出 #1
4
输入 #2
2 3
4 5
LR.
#.#
输出 #2
-1
输入 #3
4 3
10 10
.LR
###
UU#
DD.
输出 #3
-1
输入 #4
3 6
10 7
.U##.#
#DLR##
.##LR.
输出 #4
24
C++ 编辑器
输入
输出