各位大虾求助!两道用动态规划解决的算法题。最好用c或者c++

hxn8819197 2009-12-01 10:58:03
第一道: Budget Travel
An American travel agency is sometimes asked to estimate the minimum cost of traveling from one city to another by automobile. The travel agency maintains lists of many of the gasoline stations along the popular routes. The list contains the location and the current price per gallon of gasoline for each station on the list.
In order to simplify the process of estimating this cost, the agency uses the following rules of thumb about the behavior of automobile drivers.
• A driver never stops at a gasoline station when the gasoline tank contains more than half of its capacity unless the car cannot get to the following station (if there is one) or the destination with the amount of gasoline in the tank.
• A driver always fills the gasoline tank completely at every gasoline station stop.
• When stopped at a gasoline station, a driver will spend $2.00 on snacks and goodies for the trip.
• A driver needs no more gasoline than necessary to reach a gasoline station or the city limits of the destination. There is no need for a "safety margin."
• A driver always begins with a full tank of gasoline.
• The amount paid at each stop is rounded to the nearest cent (where 100 cents make a dollar).
You must write a program that estimates the minimum amount of money that a driver will pay for gasoline and snacks to make the trip.
Input
Program input will consist of several data sets corresponding to different trips. Each data set consists of several lines of information. The first 2 lines give information about the origin and destination. The remaining lines of the data set represent the gasoline stations along the route, with one line per gasoline station. The following shows the exact format and meaning of the input data for a single data set.
Line 1: One real number -- the distance from the origin to the destination Line 2: Three real numbers followed by an integer
• The first real number is the gallon capacity of the automobile's fuel tank.
• The second is the miles per gallon that the automobile can travel.
• The third is the cost in dollars of filling the automobile's tank in the origination city.
• The integer (less than 51) is the number of gasoline stations along the route.
Each remaining line: Two real numbers
• The first is the distance in miles from the origination city to the gasoline station.
• The second is the price (in cents) per gallon of gasoline sold at that station.
All data for a single data set are positive. Gasoline stations along a route are arranged in nondescending order of distance from the origin. No gasoline station along the route is further from the origin than the distance from the origin to the destination There are always enough stations appropriately placed along the each route for any car to be able to get from the origin to the destination.
The end of data is indicated by a line containing a single negative number.
Output
For each input data set, your program must print the data set number and a message indicating the minimum total cost of the gasoline and snacks rounded to the nearest cent. That total cost must include the initial cost of filling the tank at the origin. Sample input data for 2 separate data sets and the corresponding correct output follows.
Sample Input
475.6
11.9 27.4 14.98 6
102.0 99.9
220.0 132.9
256.3 147.9
275.0 102.9
277.6 112.9
381.8 100.9
516.3
15.7 22.1 20.87 3
125.4 125.9
297.9 112.9
345.2 99.9
-1
Output for the Sample Input
Data Set #1
minimum cost = $27.31
Data Set #2
minimum cost = $38.09


第二道:The Game
One morning, you wake up and think: ``I am such a good programmer. Why not make some money?'' So you decide to write a computer game.
The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.

One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:

It consists of straight segments, each one being either horizontal or vertical.
It does not cross any other game pieces.

(It is allowed that the path leaves the board temporarily.)

Here is an example:
(图帖不上来,见http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1148)


The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.

The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.


Input

The input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a ``X'' if there is a game piece at this location, and a space if there is no game piece.

Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing ``0 0 0 0".

The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.


Output

For each board, output the line ``Board #n:'', where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with ``Pair m: '', where m is the number of the pair (starting the count with 1 for each board). Follow this by ``ksegments.'', where k is the minimum number of segments for a path connecting the two game pieces, or ``impossible.'', if it is not possible to connect the two game pieces as described above.

Output a blank line after each board.


Sample Input

5 4
XXXXX
X X
XXX X
XXX
2 3 5 3
1 3 4 4
2 3 3 4
0 0 0 0
0 0


Sample Output

Board #1:
Pair 1: 4 segments.
Pair 2: 3 segments.
Pair 3: impossible.

虽然题目是英文的,但意思很好理解。希望各位高手帮忙解决下~~
...全文
345 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxn8819197 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 haoshen1987 的回复:]
OJ呀,我正在努力向这种难度大点的题进发。
[/Quote]

加油进发啊。。早日帮我把这个题目搞定啊。。
hxn8819197 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 woshishabi1984 的回复:]
第二题就是连连看。。。LS的高手英文有待强化(Joke。。别在意)

BFS+A*吧。。。暂时也没什么突破性的想法了。。。
[/Quote]

哈哈 你是大师中的大师~~
抽空帮我把第二题的代码搞搞吧,都是老师布置的作业,第一题我搞定了,第二题做不出了。。
3qqqqqu~~~
hxn8819197 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 litaoye 的回复:]
这类题目对于我,最难的部分往往在于理解题意,尤其是这种输入内容较多,输出也多的!
仅仅是看明白规则,就得很长时间。LZ既然都已经很明确可以用DP解了,那就大胆的做一下吧。

第一题,基本上就是每一站有停或不停两种状态,有时候往往还只有1种状态(超过半箱油,或到不了下一站),
那就搜索吧,配合int64做Hash(51站),其实跟Dp效率基本上是相同的,有时候还能更高一些。
程序逻辑肯定比Dp简单。

第二题目前还在理解中。
[/Quote]

哈哈,谢谢你在英语实力不济的情况下还花这么长时间看着这个题。
第一题我用DP写了下,不过答案老不对,就直接贴过来了,你也抽空写写,把代码贴上来瞧瞧?
第二题,楼下说的很好,就是连连看。。不过不知道怎么做哎。。
绿色夹克衫 2009-12-02
  • 打赏
  • 举报
回复
对第二题的理解目前仅限于“I am such a good programmer. Why not make some money?”
其实我自己有时也难免会想想这个问题,嘿嘿!
绿色夹克衫 2009-12-02
  • 打赏
  • 举报
回复
这类题目对于我,最难的部分往往在于理解题意,尤其是这种输入内容较多,输出也多的!
仅仅是看明白规则,就得很长时间。LZ既然都已经很明确可以用DP解了,那就大胆的做一下吧。

第一题,基本上就是每一站有停或不停两种状态,有时候往往还只有1种状态(超过半箱油,或到不了下一站),
那就搜索吧,配合int64做Hash(51站),其实跟Dp效率基本上是相同的,有时候还能更高一些。
程序逻辑肯定比Dp简单。

第二题目前还在理解中。
绿色夹克衫 2009-12-02
  • 打赏
  • 举报
回复
另外,咱跟高手还有挺长一段差距的!目前还处于天天向上阶段!

[Quote=引用 4 楼 woshishabi1984 的回复:]
第二题就是连连看。。。LS的高手英文有待强化(Joke。。别在意)

BFS+A*吧。。。暂时也没什么突破性的想法了。。。
[/Quote]
绿色夹克衫 2009-12-02
  • 打赏
  • 举报
回复
呵呵,何止是有待加强呀!简直是惨不忍睹呀!

[Quote=引用 4 楼 woshishabi1984 的回复:]
第二题就是连连看。。。LS的高手英文有待强化(Joke。。别在意)

BFS+A*吧。。。暂时也没什么突破性的想法了。。。
[/Quote]
woshishabi1984 2009-12-02
  • 打赏
  • 举报
回复
第二题就是连连看。。。LS的高手英文有待强化(Joke。。别在意)

BFS+A*吧。。。暂时也没什么突破性的想法了。。。
hua_zhixing_ 2009-12-01
  • 打赏
  • 举报
回复
OJ呀,我正在努力向这种难度大点的题进发。

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧