#P15808. [JOI 2013 Final] 現代的な屋敷
[JOI 2013 Final] 現代的な屋敷
Problem Description
You got lost in a huge mansion. This mansion consists of square rooms arranged in a grid in the east-west and north-south directions: there are columns from west to east and rows from south to north, for a total of rooms. Let denote the room in the -th column from the west () and the -th row from the south ().
Any two adjacent rooms in the east, west, south, or north directions are connected by a door located at the center of the wall. Each door is either closed and impassable, or open and passable. When a door is open, moving between the centers of the two rooms takes 1 minute. Also, the center of some rooms has a switch; holding down a switch for 1 minute will toggle (switch) the open/closed state of all doors in the mansion.
At present, all doors connecting two east-west adjacent rooms are closed, and all doors connecting two north-south adjacent rooms are open. You are now at the center of room , and you want to reach the center of room in the shortest time.
Task
You are given the mansion size and the positions of the rooms with switches: . Starting from the state where all doors between east-west adjacent rooms are closed and all doors between north-south adjacent rooms are open, find the minimum time (in minutes) needed to move from the center of room to the center of room . If it is impossible to reach room , report that.
Input Format
Read the following data from standard input.
- The first line contains three space-separated integers . is the number of rooms in the east-west direction, is the number of rooms in the north-south direction, and is the number of rooms with switches.
- Each of the next lines, the -th line (), contains two space-separated integers . This means there is a switch at the center of room . The pairs are all distinct.
Output Format
Output one line to standard output containing a single integer: the minimum number of minutes required to move. If it is impossible to reach room , output .
3 2 1
1 2
4
3 2 1
2 1
-1
8 9 15
3 1
3 2
3 7
3 8
1 1
4 5
4 3
5 6
5 8
6 3
6 2
7 5
8 9
8 6
8 5
25
Hint
Sample Explanation 1
In this example, you can move from the center of room to the center of room in 4 minutes with the following actions, and this is the shortest time.
- Move to the center of room .
- Press the switch at the center of room .
- Move to the center of room .
- Move to the center of room .
The mansion state at that time is shown in the figure below. In the figure, right is east, up is north, × marks your position, and ○ marks a switch.
:::align{center}
:::
Sample Explanation 2
In this example, you cannot reach room .
Sample Explanation 3
In this example, the initial mansion state is shown in the figure below. Note that there may also be a switch at the center of room or room .
:::align{center}
:::
Constraints
Number of rooms in the east-west direction of the mansion
Number of rooms in the north-south direction of the mansion
Number of rooms with switches
East-west coordinate of a room with a switch
North-south coordinate of a room with a switch
Scoring
In the scoring testdata, the part worth 20% of the score satisfies and .
In the scoring testdata, the part worth 30% of the score satisfies .
In the scoring testdata, the part worth 50% of the score satisfies at least one of the above two conditions. Also, there is no scoring testdata that satisfies both conditions at the same time.
Translated by DeepSeek V3.2.
Input Format
Output Format
Hint
Translated by ChatGPT 5