#P14848. [ICPC 2022 Yokohama R] Remodeling the Dungeon
[ICPC 2022 Yokohama R] Remodeling the Dungeon
题目描述
The Queen of Icpca resides in a castle peacefully. One day, she heard that many secret agents had been active in other nations, and got worried about the security of the castle.
The castle has a rectangular dungeon consisting of rows of square rooms. Adjacent rooms are separated by a wall. Some walls have doorways making the separated rooms intercommunicate. The entrance and the exit of the dungeon are in the rooms located at two diagonal extreme corners. The dungeon rooms form a tree, that is, the exit room is reachable from every room in the dungeon through a unique route that passes all the rooms on the route only once.
In order to enhance the security of the castle, the Queen wants to remodel the dungeon so that the number of rooms on the route from the entrance room to the exit room is maximized. She likes the tree property of the current dungeon, so it must be preserved. Due to the cost limitation, what can be done in the remodeling is to block one doorway to make it unavailable and to construct one new doorway instead on a wall (possibly on the same wall).
Your mission is to find a way to remodel the dungeon as the Queen wants.
输入格式
The input consists of a single test case in the following format.
and represent the size of the dungeon, each of which is an integer between and , inclusive. The characters (, ) represent the configuration of the dungeon as follows:
- for and , which corresponds to the room -th from the north end and -th from the west end of the dungeon; such a room is called the room .
- or for and , which represents the wall between the rooms and ; the character means that the wall has a doorway and means it has no doorway.
- or for and , which represents the wall between the rooms and ; the character means that the wall has a doorway and means it has no doorway.
- () and (), which correspond to the outer walls of the dungeon.
- for and , which corresponds to an intersection of walls or outer walls.
The entrance and the exit of the dungeon are in the rooms and , respectively. The configuration satisfies the tree property stated above.
输出格式
Output the maximum length of the route from the entrance room to the exit room achievable by the remodeling, where the length of a route is the number of rooms passed including both the entrance and exit rooms.
2 3
+-+-+-+
|.....|
+.+.+.+
|.|.|.|
+-+-+-+
6
2 3
+-+-+-+
|...|.|
+.+.+.+
|.|...|
+-+-+-+
4
5 5
+-+-+-+-+-+
|...|...|.|
+-+.+.+.+.+
|...|.|.|.|
+.+.+.+-+.+
|.|...|.|.|
+.+.+-+.+.+
|.|.....|.|
+-+.+.+-+.+
|...|.....|
+-+-+-+-+-+
15
提示
In the first sample, if you block the doorway between the rooms and and construct a new doorway between the rooms and , then the unique route from to passes all of the 6 rooms, which is obviously the maximum.
In the second sample, any remodeling keeps the length of the unique route from to to be exactly 4.
In the third sample, one of the optimal ways is blocking the doorway between the rooms and and constructing a new doorway between the rooms and .
The configurations after the remodeling described above are as follows.
+-+-+-+ +-+-+-+ +-+-+-+-+-+
|.|...| |...|.| |...|...|.|
+.+.+.+ +.+.+.+ +-+.+.+.+.+
|...|.| |.|...| |...|.|.|.|
+-+-+-+ +-+-+-+ +.+.+.+.+.+
|.|...|.|.|
+.+.+-+.+.+
|.|.|...|.|
+-+.+.+-+.+
|...|.....|
+-+-+-+-+-+