#P14848. [ICPC 2022 Yokohama R] Remodeling the Dungeon

    ID: 16646 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>搜索2022广度优先搜索 BFSICPC横浜

[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 hh rows of ww 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.

h wh \ w c1,1c1,2c1,2w+1c_{1,1} c_{1,2} \cdots c_{1,2w+1} c2,1c2,2c2,2w+1c_{2,1} c_{2,2} \cdots c_{2,2w+1} \vdots c2h+1,1c2h+1,2c2h+1,2w+1c_{2h+1,1} c_{2h+1,2} \cdots c_{2h+1,2w+1}

hh and ww represent the size of the dungeon, each of which is an integer between 22 and 500500, inclusive. The characters ci,jc_{i,j} (1i2h+11 \leq i \leq 2h+1, 1j2w+11 \leq j \leq 2w+1) represent the configuration of the dungeon as follows:

  • c2i,2j=.c_{2i,2j} = \text{'}.\text{'} for 1ih1 \leq i \leq h and 1jw1 \leq j \leq w, which corresponds to the room ii-th from the north end and jj-th from the west end of the dungeon; such a room is called the room (i,j)(i,j).
  • c2i+1,2j=.c_{2i+1,2j} = \text{'}.\text{'} or \text{'}-\text{'} for 1ih11 \leq i \leq h-1 and 1jw1 \leq j \leq w, which represents the wall between the rooms (i,j)(i,j) and (i+1,j)(i+1,j); the character .\text{'}.\text{'} means that the wall has a doorway and \text{'}-\text{'} means it has no doorway.
  • c2i,2j+1=.c_{2i,2j+1} = \text{'}.\text{'} or \text{'}|\text{'} for 1ih1 \leq i \leq h and 1jw11 \leq j \leq w-1, which represents the wall between the rooms (i,j)(i,j) and (i,j+1)(i,j+1); the character .\text{'}.\text{'} means that the wall has a doorway and \text{'}|\text{'} means it has no doorway.
  • c1,2j=c2h+1,2j=c_{1,2j} = c_{2h+1,2j} = \text{'}-\text{'} (1jw1 \leq j \leq w) and c2i,1=c2i,2w+1=c_{2i,1} = c_{2i,2w+1} = \text{'}|\text{'} (1ih1 \leq i \leq h), which correspond to the outer walls of the dungeon.
  • c2i+1,2j+1=+c_{2i+1,2j+1} = \text{'}+\text{'} for 0ih0 \leq i \leq h and 0jw0 \leq j \leq w, which corresponds to an intersection of walls or outer walls.

The entrance and the exit of the dungeon are in the rooms (1,1)(1,1) and (h,w)(h,w), 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 (1,1)(1,1) and (1,2)(1,2) and construct a new doorway between the rooms (2,1)(2,1) and (2,2)(2,2), then the unique route from (1,1)(1,1) to (2,3)(2,3) 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 (1,1)(1,1) to (2,3)(2,3) to be exactly 4.

In the third sample, one of the optimal ways is blocking the doorway between the rooms (4,2)(4,2) and (4,3)(4,3) and constructing a new doorway between the rooms (2,4)(2,4) and (3,4)(3,4).

The configurations after the remodeling described above are as follows.

+-+-+-+ +-+-+-+ +-+-+-+-+-+
|.|...| |...|.| |...|...|.|
+.+.+.+ +.+.+.+ +-+.+.+.+.+
|...|.| |.|...| |...|.|.|.|
+-+-+-+ +-+-+-+ +.+.+.+.+.+
                |.|...|.|.|
                +.+.+-+.+.+
                |.|.|...|.|
                +-+.+.+-+.+
                |...|.....|
                +-+-+-+-+-+