0

i have this "test" let's say.. i need to solve this problem, it's not homework but i need it in order to pass a test.

I could need a little help, because i don't understand what i need to do. I need the algorithm here! So it goes: The table below represents a labyrinth. "1" means you cannot pass through that value, "0" means you can pass through that value. "T" is the treasure to achieve and "H" is the entrance. The coordinates are : H(3.1) , T(2.8).

-I need a program that reads a matrix A[1..M,1..N] which itself represents a labyrinth with elements [0,1] and also reads the H,T value.

It should print a way to the treasure , if there is one, else it should say "No way to the treasure"he Matrix is:

    1 1 1 1 1 1 1 1 1 1
    1 1 0 0 0 1 0 T 0 1
    H 0 0 1 1 1 0 1 1 1
    1 1 0 0 0 0 0 0 0 1
    1 1 1 1 1 1 1 1 1 1
4

1 回答 1

1

使用Wall Follower方法。您需要有一个变量来存储您正在查看的方向(左、右、上、下)。然后你继续朝那个方向前进,直到撞到墙。当你在墙上时,你一直向左转,直到你可以继续走路。这样做直到你达到目标。

或者在伪代码中:

var walkingDirection = up;
while (not at target)
    if (next field in walkingDirection is not a wall)
        go to next field in walkingDirection
    else
        turn right
    end if
end while

然而,这将在不是简单连接的迷宫中失败(阅读该链接)。

另一种稍微困难的方法可能是A* 算法

于 2013-03-26T17:53:08.763 回答