問題一覧 > 通常問題

No.3679 Some Huge Bug Returns

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 1024 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 144
作問者 : elphe / テスター : ゆにたりー卿 drken1215 Mikan04y lp_ql yimiya(いみや) よには 👑 loop0919 Andrew8128 edon8618 👑 seekworser leaf_1415 紫前燈太 triangle_coder 👑 みうね
お気に入りにしたユーザー ProblemId : 13348 / 自分の提出
問題文最終更新日: 2026-09-05 12:38:15
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
第3回 岩井星人アンソロジープログラミングコンテスト (順位表) の他の問題:

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.

  1. Approach: repeat moves until reaching any of the cells occupied by the huge bug
  2. Neutralization: finish off the huge bug and collect its carcass
  3. Disposal: repeat moves until reaching the trash can at cell $(P,Q)$, then throw away the carcass of the huge bug
  4. 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
サンプル1のGIFアニメーション

The animation above visualizes the following movements.

  1. Initially, Iwai Seijin is recording a video at cell $(1,3)$.
  2. Move downward $4$ times to reach cell $(5,3)$.
  3. Move rightward $2$ times to reach cell $(5,5)$.
  4. Cell $(5,5)$ is part of the region occupied by the huge bug, so perform "Neutralization" here.
  5. Move leftward $4$ times to reach cell $(5,1)$.
  6. Cell $(5,1)$ is the cell with the trash can, so throw away the remains of the huge bug here.
  7. Move rightward $2$ times to reach cell $(5,3)$.
  8. Move upward $4$ times to reach cell $(1,3)$.
  9. 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もしくは右上の雲マークをクリックしてアカウントを作成してください。