SRM 440 DIVII 500中文翻译及思路(C#)

Problem Statement(陈述)     

According to research conducted recently, listening to classical music increases one's mental abilities, while listening to metal decreases them. Now, yet another experiment is being conducted to try to prove or disprove this statement.

 

根据最新的研究表明,听古典音乐能增加一个人的思维能力,但是听金属摩擦的声音则降低这种能力。现在,有一个实验,正试图证明或推翻这一陈述。

 

In this new experiment, a mouse is placed in a rectangular maze consisting of NxM squares. Each square either contains a wall or is empty. The maze is structured in such a way that for any two empty squares, there exists exactly one path between them. A path is a sequence of pairwise distinct empty squares such that every two consecutive squares are neighboring. Two squares are considered neighboring if they share a common edge.

 

在这个新的实验中,一只老鼠被放置在一个由NxM方块组成的长方形迷宫中。每一个方块都代表了墙壁或空。迷宫是这样一种结构,任何两个方块之间存在着一条路。这条路是一连串成对的空方块,每两个连续的方块是挨着的。两个方块如果他们有共同的边则被视为友邻。

 

One of the empty squares in the maze contains a piece of cheese. The mouse's goal is to reach that square without visiting the same square twice. The mouse can only move between neighboring squares. Since the mouse has been listening to classical music for a week, he is extremely intelligent and guaranteed to achieve his goal.

 

在迷宫中有一个方块里包含一块奶酪。老鼠的目标是到达这个方块,但是不能通过相同的方块两次以上。老鼠只能在相邻的两个方块间移动。假设老鼠已经听古典音乐一个星期了,他非常聪明而且保证能实现目标。

 

As the mouse moves from his starting point to the cheese, he may encounter some squares where he must choose between several neighboring squares to continue. This happens when the mouse steps into a square which has more than one neighboring empty square, excluding the square from which he came, or when he has more than one neighboring empty square at the start.

 

老鼠从他的出发点到奶酪,可能会遇到一些方块,他必须选择一些相邻的方块继续前行。老鼠走到这种方块,该方块有一个以上为空的友邻时,但不包括他已经经过的方块。

 

These situations are called "decisions" and the mouse will always make the right choice. You are given a string[] maze representing the maze. It contains N elements, each containing M characters. Empty squares are denoted by '.', walls are denoted by uppercase 'X', the mouse's starting point is denoted by 'M', and the square containing the cheese is denoted by '*'. Return the number of decisions the mouse will make on his way to the cheese.

 

这些情况被称为决定,并且老鼠一直都会做出正确的选择。给你一个代表迷宫的字符串(string[] maze)。它包含多个个元素,每个元素由多个字符组成。空方块指由'. '表示 ,墙由大写的字符' X'表示 ,老鼠的出发点由字符' M'表示,有奶酪的方块由字符' * '表示 。返回老鼠到达奶酪途中决定的次数。

 

 

Definition(定义)   

Class(类名): MazeWanderingEasy

Method(方法): decisions

Parameters(参数): string[]

Returns(返回值类型): int

Method signature(方法定义样式):public int decisions(string[] maze)

 

 

Constraints(限制)

-          maze will contain between 1 and 50 elements, inclusive.

-          迷宫包含150个元素。

-          element of maze will contain between 1 and 50 characters, inclusive.

-          迷宫的每个元素将包含150个字符。

-          Elements of maze will be of the same length.

-          迷宫的元素为相同的长度。

-          maze will contain only '.', 'X', 'M' or '*' characters.

-          迷宫只能包含以下字符:'.','X','M' '*'

-          There will be exactly one '*' character in maze.

-          迷宫中字符'*'的位置必须正确

-          There will be exactly one 'M' character in maze.

-          迷宫中字符'M'的位置必须正确

-          For every pair of empty squares in the maze, there will exist exactly one path between them.

-          迷宫中一组空方块之间,只存在唯一一条路径

 

 

Examples(样例)

0)     

{"*.M"}

Returns: 0

From each square, the mouse can only move to one other square, so he never has to make any decisions.

老鼠只能移动到另一个方块,所以他没有作出任何决定。

 

1)     

{"*.M",

".X."}

Returns: 1

The mouse has to make a decision right at the start.

老鼠需要在开始时就做出正确的判断

 

2)     

{"...",

 "XMX",

"..*"}

Returns: 2

The mouse makes decisions at both squares before reaching the cheese.

老鼠在他到达奶酪之前做出判断

 

3)     

{".X.X......X",

 ".X*.X.XXX.X",

".XX.X.XM...",

"......XXXX."}

Returns: 3

 

4)     

{"..........*",

 ".XXXXXXXXXX",

 "...........",

"XXXXXXXXXX.",

"M.........."}

Returns: 0

 

 

Idea(思路)

个人感觉可以把这个问题的模型转换为树。以起点M为根结点,以空方块'.'为子结点,依次构造成一棵树。然后从根结点开始遍历,一直到*,在这条路径上,有不在那条路径上的子结点的结点数目即为“决定”的次数。

 

这里以样例2为例:

{"...",

 "XMX",

"..*"}

为叙述方便,我们为所有的空方块编号,即:

1   2   3

X  M  X

4   5   *

 

则可构造出如下的树形结构:

然后查找'*'所在的位置,由M*的路径即为:M,5,*

 

M的结点有一个子结点2不在路径上,即在M上有一次“决定”;然后,5的结点有一个子结点4,同样在5处也有一次“决定”。则返回“决定”数为2

 

其实,我才学到数据结构的树这一章,看题后,无意中想到可以用树结构来解决这个问题,就试着做了一下,真的可以…但是,树的算法(主要是递归算法)上,还不熟悉,所以还没写出Source code

不知道这个思路是否可行,恳请各位指点咯!~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值