No.467 The Hidden Game
Problem Statement
When I opened a drawer of my father's desk, I happened to find a box, beautifully wrapped and tied with a ribbon.
I opened it without permission and found a game inside. It looked fun, so I decided to play it in secret.
This game is played by moving a character around an infinitely large field covered with square cells.
Because the creator has a nasty personality, moving is a little troublesome.
From its current cell, the character can move to any cell whose Chebyshev distance $(L_\infty-$distance$)$ from the current cell is one of $d_1 \dots d_n$.
The Chebyshev distance between $(x_1,y_1)$ and $(x_2,y_2)$ is given by $max(|x_1-x_2|,|y_1-y_2|)$. $($Chebyshev distance: Wikipedia$)$
The character is now at the cell $(0,0)$.
Find the minimum number of moves required to go from $(0,0)$ to $(x,y)$.
If it is impossible to get there, output $-1$.
Input
$n$ $d_1 \dots d_n$ $x \ y$
Line $1$ contains $n$, the number of distances the character can move.
Line $2$ contains $d_i$, separated by spaces.
Line $3$ contains the coordinates $(x,y)$ of the destination cell, separated by a space.
All input values are integers.
$1 \le n \le 15$
$1 \le d_i \le 10^9 \ (d_i$ are all distinct$)$
$-10^9 \le x,y \le 10^9$
Output
Output the minimum number of moves required to reach $(x,y)$.
If it is impossible to get there, output $-1$.
Sample
Sample 1
Input
3 3 4 5 12 12
Output
3
Example: $(0,0) \ → \ (3,3) \ → \ (7,7) \ → \ (12,12)$
Sample 2
Input
2 5 9 4 0
Output
2
Example: $(0,0) \ → \ (9,0) \ → \ (4,0)$
Sample 3
Input
1 1 45 14
Output
45
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
tubo28