No.3679 Some Huge Bug Returns
タグ : / 解いたユーザー数 144
作問者 :
ゆにたりー卿
drken1215
lp_ql
yimiya(いみや)
よには
👑
Andrew8128
leaf_1415
triangle_coder
👑 Problem Statement
The floor plan of Iwai Seijin's room is represented by a grid with $H$ rows and $W$ columns.
Below, the cell in the $i$-th row from the top and the $j$-th column from the left of this grid is denoted by cell $(i,j)$.
Initially, Iwai Seijin is recording a video at cell $(A,B)$, and some huge bug occupies the rectangular region whose upper-left cell is cell $(R_1,C_1)$ and whose lower-right cell is cell $(R_2,C_2)$.
Iwai Seijin will now exterminate this huge bug by the following procedure.
- Approach: repeat moves until reaching any of the cells occupied by the huge bug
- Neutralization: finish off the huge bug and collect its carcass
- Disposal: repeat moves until reaching the trash can at cell $(P,Q)$, then throw away the carcass of the huge bug
- Return: repeat moves until reaching cell $(A,B)$
Iwai Seijin is worried about how the video will turn out and wants to exterminate this huge bug as efficiently as possible.
What is the minimum number of moves with which Iwai Seijin can exterminate this huge bug?
When at cell $(i,j)$, Iwai Seijin can, by performing $1$ move, reach any of cell $(i-1,j),\ $ cell $(i+1,j),\ $ cell $(i,j-1),\ $ cell $(i,j+1)$ (if the cell exists).
Constraints
- $1 \leq H \leq 200$
- $1 \leq W \leq 200$
- $1 \leq A \leq H$
- $1 \leq B \leq W$
- $1 \leq R_1 \leq R_2 \leq H$
- $1 \leq C_1 \leq C_2 \leq W$
- $1 \leq P \leq H$
- $1 \leq Q \leq W$
- All input values are integers.
Input
The input is given from Standard Input in the following format.$H$ $W$ $A$ $B$ $R_1$ $C_1$ $R_2$ $C_2$ $P$ $Q$
Output
Print the answer.
Sample
Sample 1
Input
5 8 1 3 4 5 5 7 5 1
Output
16
The animation above visualizes the following movements.
- Initially, Iwai Seijin is recording a video at cell $(1,3)$.
- Move downward $4$ times to reach cell $(5,3)$.
- Move rightward $2$ times to reach cell $(5,5)$.
- Cell $(5,5)$ is part of the region occupied by the huge bug, so perform "Neutralization" here.
- Move leftward $4$ times to reach cell $(5,1)$.
- Cell $(5,1)$ is the cell with the trash can, so throw away the remains of the huge bug here.
- Move rightward $2$ times to reach cell $(5,3)$.
- Move upward $4$ times to reach cell $(1,3)$.
- Cell $(1,3)$ is the cell where the video was originally being recorded, so this completes the extermination of the huge bug.
By moving as in this animation, the huge bug can be efficiently exterminated with $16$ moves.
It is impossible to exterminate the huge bug with $15$ moves or fewer, so the answer is $16$.
Sample 2
Input
200 200 1 1 1 2 200 200 200 1
Output
400
The huge bug may occupy $99.5\%$ of Iwai Seijin's room.
Sample 3
Input
1 1 1 1 1 1 1 1 1 1
Output
0
Note that cell $(A,B)$ where Iwai Seijin is recording the video, the rectangular region occupied by the huge bug, and cell $(P,Q)$ with the trash can may overlap.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。