java++迷宫选出最优距离_解决这个迷宫的最佳算法?

您需要评估每个可能的移动并采取最小距离的移动 . 类似于以下内容:

int minDistance(int x, int y, int prevX, int prevY, int distance) {

if (CollionWithBorder(x, y) // can't take this path

return int.MAX_VALUE;

if (NoCollionWithBorder(x, y) // it's OK to take this path

{

// update the distance only when there is a long change in direction

if (LongDirectionChange(x, y, prevX, prevY))

distance = distance + 1;

)

if (ReachedDestination(x, y) // we're done

return distance;

// find the path with the minimum distance

return min(minDistance(x, y + 1, x, y, distance), // go right

minDistance(x + 1, y, x, y, distance), // go up

minDistance(x - 1, y, x, y, distance), // go down

minDistance(x, y - 1, x, y, distance)); // go left

}

bool LongDirectionChange(x, y, prevX, prevY) {

if (y-2 == prevY && x == prevX) ||(y == prevY && x-2 == prevX)

return true;

return false;

}

这是假设不允许对角线移动 . 如果是,请将它们添加到min()调用:

minDistance(x + 1, y + 1, distance), // go up diagonally to right

minDistance(x - 1, y - 1, distance), // go down diagonally to left

minDistance(x + 1, y - 1, distance), // go up diagonally to left

minDistance(x - 1, y + 1, distance), // go down diagonally to right

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值