Ouc oj 1066 frog crossing the river, oucoj
Description
There is a pig Bridge On The River. A frog wants to jump from one side of the river to the other side. There are some stones on the bridge, and frogs hate to step on these stones. Because the length of the bridge is a positive integer from the distance that frogs Skip at a time, we can regard the points that frogs may reach on the bridge as a string of integers ,......, L (where L is the length of the bridge ). A point whose coordinates are 0 indicates the start point of the bridge, and a point whose coordinates are L indicates the end point of the bridge. The frog starts from the starting point of the bridge and keeps jumping to the ending point. The distance of a hop is any positive integer (including S and T) between S and T ). When a frog jumps to or skips a point whose coordinate is L, even if the frog has jumped out of the bridge. The question shows the length of the bridge, the distance between frogs and the distance S, T, and the stone position on the bridge. Your task is to determine the number of stones that a frog must step on to cross the river.
Input
The input contains multiple groups of data. The first row of each group of data has a positive integer L (1 <= L <= 20000), indicating the length of the bridge. The second row has three positive integers, S, T, and M, respectively, representing the minimum distance, maximum distance, and the number of stones on the bridge for a frog jump, 1 <= S <= T <= 10, 1 <= M <= 100. The third row has M different positive integers, indicating the position of the M stones on the number axis (data ensures that there are no stones at the start and end points of the bridge ). All adjacent integers are separated by a space. The input ends with a value of 0.
Output
Output a row of data in each group. This row contains only one integer, indicating the number of stones that the frog needs to step on when crossing the river.
Sample Input
102 3 52 3 5 6 7105 5 110
Sample output
20
This is a very basic dp problem. I am afraid I will forget it later. I have nothing to say about it. I will directly paste the ac code.
#include<stdio.h>
int
main()
{
int
L;
scanf
(
"%d"
,&L);
while
(L)
{
int
S,T,M,i,j;
scanf
(
"%d%d%d"
,&S,&T,&M);
int
a[L+T],b[L+T+1];
for
(i=1;i<L+T;i++)
{
a[i]=0;
b[i]=20010;
}
b[0]=0;
for
(i=0;i<M;i++)
{
scanf
(
"%d"
,&j);
a[j]=1;
}
for
(i=S;i<L+T;i++)
{
for
(j=S;j<=T;j++)
{
if
(i-j>=S||i-j==0)
b[i]=b[i]<b[i-j]+a[i]?b[i]:b[i-j]+a[i];
if
(i>L)
{
if
(b[i]<b[L])b[L]=b[i];
}
}
}
printf
(
"%d\n"
,b[L]);
scanf
(
"%d"
,&L);
}
return
0;
}
/**************************************************************
Problem: 1066
User: 13579246810
Language: C
Result: Correct
Time:4 ms
Memory:1120 kb
****************************************************************/