我是编程初学者,我想了解如何解决这个问题。谢谢 [英] I am beginner in programming, I'd like to understand how to solve this problem. Thank you

查看:85
本文介绍了我是编程初学者,我想了解如何解决这个问题。谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

当一个年轻的走私者到达他正在发展的村庄时,一个生活在偏远河流附近的走私者已成为河流过境物流的顾问。虽然他喜欢他的新工作,但是那些向他提供嘲笑谜语的人常常接近他。这些是他想要一劳永逸地解决的简单的河流穿越问题。



河道

河流过境问题(in)是必须将一些人从一个银行传递到另一个河岸,船上的地点数量有限。除此之外还有以下约束条件:如果没有第三个给定的个人,则不能设置一对个人。



该船位于岸边离开开始,然后在所有人的最后一侧(与出发的对面)结束时,将行驶方向交替到最后。这不一定适合每次旅行。但是,船上肯定有人。



问题

你必须帮助顾问做一个能解决任何河流的程序穿越上述形式的问题。



例子

穿越老渡船经常出现的问题是狼,山羊和卷心菜。在这个问题上,一个农民必须乘坐他的狼,山羊和卷心菜,在一条河上乘船。农夫和每个chacuns东西坐在船上。如果狼和山羊没有农民(无论是在岸上还是在船上),而狼会吃山羊,两种方式都是一样的。



遇到的另一个问题是已经是驯兽师和他们的猴子。三个驯兽师各自伴随着他们马戏团的猴子。六个人必须在一个木筏上过河两个人。如果一只猴子没有被她的训练师监视,那么它现在将被试图攻击任何其他驯兽师。



显然在这两个问题中,所有人都必须过河安然无恙。 (注意:为简单起见,所有人都可以在我们的一般情况下开船。这使得卷心菜可以单独在船上移动..)



输入格式



想要越过N的人数和第一行中B船的最大座位数,以空格分隔。



在其他方面,约束。每个约束是由空格分隔的三个ID的列表。除非第三个标识符也存在,否则不能设置对应于前两个标识符的个人。为简化起见,个别标识符是1和invividus N之间的整数。



输出格式



通过另一方所有人所需的最小航班次数。



如果无法解决,请输入':('输出。



样品输入



2 6

1 5 4

1 6 4

2 4 5

2 6 5

3 4 6

3 5 6


样品输出



11



说明



这个问题对应于猿和动物训练师的问题,其中猴子是个体1,2和3以及驯兽者4,5和6.猴子属于驯兽师xx + 3.



可以进行以下交叉:



(跳转:A,返回: R)+船上个人的标识符



A:1 2

R:2


A:2 3

R:3



A:4 of 5

R:5月2日



答:5 6

答:1



答:1 2

R:2



A:2 3



共计11艘移动,这是这个问题的最小值。

解决方案

尝试以下任何一种:



福克斯,鹅和豆袋拼图 - 维基百科,免费的百科全书 [ ^ ]



Chicken Crossing Puzzle - 解决方案 [ ^ ]



PUZZLE - 狐狸,鸡肉和鸡肉河对岸的粮食 - 怎么样? - YouTube [ ^ ]



鸡肉,狐狸和谷物拼图 [ ^ ]



狐狸,鸡肉,谷物谜语|论坛|装甲游戏 [ ^ ]



有鸡,狐狸和玉米的农民需要越过河流......? | Yahoo Answers [ ^ ]

Context
An old smuggler living near a remote river has become consultant river crossing logistics when younger smugglers arrived in his growing village. Although he loves his new job, it is often approached by people who offer him that he is mocking riddles. These are simple river crossing problems he wants to settle once and for all.

River Passage
The problems of river crossings (in) are to have to pass a number of individuals from one bank to the other of the river with a limited number of places on a boat. Added to this are the constraints formulated as follows: given a pair of individuals can not be set without a third given individual is present.

The boat is on the shore of departure to start and then made trips alternating direction to the end, when he finished at the finish side (opposite to that of departure) with all individuals. It is not necessarily right for each trip. However, there must always be someone on the boat.

Problem
You must help the consultant by making a program that will solve any river crossing problem of the form described above.

Examples
An often posed problems crossing the old ferryman is the wolf, goat and cabbage. In this problem, a farmer must traverse aboard a boat up a river two by taking his wolf, her goat and cabbage. The farmer and each of its chacuns things take a seat on the boat. If the wolf and the goat are present without the farmer (whether on shore or on board), while the wolf will eat the goat and it is the same for both ways.

Another problem encountered is that of already tamers and their monkeys. Three tamers are each accompagés a monkey of their circus. The six individuals must cross the river on a raft for two. If a monkey is not monitored by her trainer, then it will be attempted to attack any other tamer now.

Obviously in these two problems, all individuals must cross the river safe and sound. (Note: For simplicity, all individuals can drive the boat in our general scenario This makes the cabbage could make a move on the boat alone..)

Input Format

The number of individuals wanting to cross N and the maximum number of seats in the boat B on the first line, separated by a space.

On the other lines, the constraints. Each constraint is a list of three IDs separated by spaces. Individuals corresponding to the first two identifiers can not be set unless the third is present also. For simplification, the individual identifiers are integers between 1 and the number of invividus N.

Output Format

The minimum number of sailings required to pass all individuals on the other side.

If no solution is possible, putting ':(' output.

Sample Input

2 6
1 5 4
1 6 4
2 4 5
2 6 5
3 4 6
3 5 6

Sample Output

11

Explanation

This problem corresponds to that of apes and animal trainers with monkeys being individuals 1, 2 and 3 and the tamers 4, 5 and 6. The monkey belongs to tamer x x + 3.

It is possible to make the following crossings:

(Jump: A, Return: R) + identifiers of individuals on board

A: 1 2
R: 2

A: 2 3
R: 3

A: 4 of 5
R: May 2

A: 5 6
A: 1

A: 1 2
R: 2

A: 2 3

This makes a total of 11 ship movements, which is the minimum for this problem.

解决方案

Try any of these:

Fox, goose and bag of beans puzzle - Wikipedia, the free encyclopedia[^]

Chicken Crossing Puzzle - Solution[^]

PUZZLE - fox, chicken & grain across river - HOW? - YouTube[^]

Chicken, Fox and Grain Puzzle[^]

Fox, chicken, grain riddle | Forums | Armor Games[^]

Farmer with a chicken, fox and corn needs to get across river...? | Yahoo Answers[^]


这篇关于我是编程初学者,我想了解如何解决这个问题。谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆