No.20 Desert Oasis
タグ : / 解いたユーザー数 260
作問者 :
Problem Statement
Taro is a traveling merchant who walks through the desert.
He is now about to head for the next town.
The desert has places that are easy to travel through and places that are not,
and from his many years of experience, Taro knows
how much stamina he will lose by going to each place.
The desert extends without limit, but Taro only knows an area of \(N \times N\) cells,
and because going outside it would put his life in danger, he never goes there.
In the desert there is an oasis at location \((O_x,O_y)\), and by going to this place,
he can, "only once", make his stamina "\(2\) times" its current value. It may exceed his initial stamina.
(There may also be no oasis.)
Taro is now standing at location \((1,1)\), and the next town is at location \((N,N)\).
He can only move to the cells adjacent in the four directions (up, down, left, right) that share an edge with his cell,
and when he moves to the next cell, his stamina decreases by the desert level \((L_{xy})\) of the cell he moved to.
If the desert level of the destination cell is \(0\), his stamina does not decrease,
but Taro dies the moment his stamina becomes \(0\) or less.
The location of the oasis may also have a desert level of \(1\) or more,
in which case the effect of the oasis is applied after his stamina has decreased by the desert level.
Tell Taro whether he can safely reach the next town.
The oasis is never at $(1,1)$.
Input
\(N\ V\ O_x\ O_y\)
\(L_{11}\ L_{21}\ L_{31}\ \dots\ L_{N1}\)
\(L_{12}\ L_{22}\ L_{32}\ \dots\ L_{N2}\)
\(\dots\)
\(L_{1N}\ L_{2N}\ L_{3N}\ \dots\ L_{NN}\)
On line \(1\),
an integer $N$ \((2 \leq N \leq 200)\) representing the side length of the desert,
an integer $V$ \((1 \leq V \leq 500)\) representing Taro's stamina, and
a pair of integers $N$ \((1 \leq O_x,O_y \leq N)\) representing the position of the oasis, where $(O_x,O_y)$ is never given as $(1,1)$, and $(O_x,O_y) = (0,0)$ means there is no oasis,
are given, separated by spaces.
From line \(2\) onward,
\(0 \leq L_{xy} \leq 9 : (x,y)\), the desert level at the location \((1 \leq x,y \leq N)\),
is given, separated by half-width spaces.
Output
Print "YES" if he can reach the town, and "NO" if he cannot. (Do not include the quotation marks.)
Print a newline at the end.
Sample
Sample 1
Input
3 3 0 0 0 1 1 2 0 1 1 2 0
Output
YES
Since \(O_x = 0\) and \(O_y = 0\), there is no oasis in this case.
Walking in the order \((1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(3,2)\rightarrow(3,3)\) loses the least stamina.
The required stamina is \(2\), so he can reach the destination.
Sample 2
Input
10 3 8 6 0 9 0 0 0 9 0 0 0 9 0 9 0 9 0 0 0 9 0 0 0 9 0 9 9 9 9 9 9 0 0 9 0 9 0 0 0 9 0 0 0 9 0 9 0 9 0 0 9 0 0 9 0 0 0 0 9 1 9 0 0 9 9 0 9 9 9 9 0 0 0 9 0 0 0 0 0 0 9 0 0 9 9 9 9 9 9 0 9 1 0 0 0 0 0 0 0 0 9 2
Output
YES
For most of the way he can pass through cells with desert level \(0\), so he loses no stamina at all, but
his stamina decreases by a total of \(3\) just before the destination, so he must stop by the oasis on the way.
If he stops by the oasis, his stamina decreases by \(1\) from \(3\) and is then doubled to \(2\) times by the effect of the oasis,
so his stamina becomes \(4\). Therefore, he can reach the destination.
Sample 3
Input
10 30 8 8 0 1 1 9 7 6 1 1 1 1 1 5 1 4 6 7 1 1 3 1 1 1 5 6 6 6 1 1 0 1 0 1 5 6 7 1 0 7 9 1 2 1 5 6 7 1 9 8 1 1 1 1 5 5 5 0 9 9 1 5 1 1 4 5 4 1 9 9 2 1 1 2 3 4 1 0 9 9 4 6 5 1 2 1 1 1 9 8 1 2 1 1 1 1 1 1 9 3 9 1
Output
NO
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。