如何在网格上找到从A到B的最短路径?

时间:2017-01-14 17:11:26

标签: c++ recursion matrix

在我的练习中,我需要在矩阵上找到从一个点到另一个点的最短路径,并且在路上有一些障碍。我正在使用C ++代码。

例如:

X X X X X X X X

X X X 4 X X 3 X

X X 8 X X X X X

0 X X X X 6 X X

如果我需要从持有 0 的位置到达 3 的位置,我知道我必须向右走六步并向上走两步。但是,在我的练习中,我无法传递另一个数字;路径必须仅使用标记为 X 的位置。

我试着写一个递归函数来解决这个问题,但是它没有用。

有人可以帮助我吗?

这是我写的代码:

int Rec(Point start, Point finish,Direction dira)
{
    int up = 0, down = 0, left = 0, right = 0;
    int res;
    Point tmp;
    if (finish.getX() == start.getX() && finish.getY()==start.getY())
        return 0;
    else if (dira == UP)
    {
        tmp = start;
        tmp.setY(start.getY() - 1);
        if (screen->getBoardChar(tmp) == ' '){
            up++;
            up += Rec(tmp, finish, UP);
        }       
        tmp = start;
        tmp.setX(start.getX() + 1);
        if (screen->getBoardChar(tmp) == ' '){
            right++;
            right += Rec(tmp, finish, RIGHT);
        }       
        tmp = start;
        tmp.setX(start.getX() - 1);
        if (screen->getBoardChar(tmp) == ' '){
            left += Rec(tmp, finish, LEFT);
            left++;
        }
    }
    else if (dira == DOWN)
    {
        tmp = start;
        tmp.setY(start.getY() + 1);
        if (screen->getBoardChar(tmp) == ' ') {
            down++;
            down += Rec(tmp, finish, DOWN);
        }
        tmp = start;
        tmp.setX(start.getX() + 1);
        if (screen->getBoardChar(tmp) == ' ') {
            right++;
            right += Rec(tmp, finish, RIGHT);
        }
        tmp = start;
        tmp.setX(start.getX() - 1);
        if (screen->getBoardChar(tmp) == ' ') {
            left += Rec(tmp, finish, LEFT);
            left++;
        }
    }
    else if (dira == LEFT)
    {
        tmp = start;
        tmp.setY(start.getY() + 1);
        if (screen->getBoardChar(tmp) == ' ') {
            down++;
            down += Rec(tmp, finish, DOWN);
        }
        tmp = start;
        tmp.setX(start.getX() - 1);
        if (screen->getBoardChar(tmp) == ' ') {
            left += Rec(tmp, finish, LEFT);
            left++;
        }
        tmp = start;
        tmp.setY(start.getY() - 1);
        if (screen->getBoardChar(tmp) == ' ') {
            up++;
            up += Rec(tmp, finish, UP);
        }
    }
    else
    {
        tmp = start;
        tmp.setY(start.getY() + 1);
        if (screen->getBoardChar(tmp) == ' ') {
            down++;
            down += Rec(tmp, finish, DOWN);
        }
        tmp = start;
        tmp.setY(start.getY() - 1);
        if (screen->getBoardChar(tmp) == ' ') {
            up++;
            up += Rec(tmp, finish, UP);
        }
        tmp = start;
        tmp.setX(start.getX() + 1);
        if (screen->getBoardChar(tmp) == ' ') {
            right++;
            right += Rec(tmp, finish, RIGHT);
        }
    }
    res = Smallest(up, down, left, right);
    return res;
}

1 个答案:

答案 0 :(得分:0)

很容易你有两个位置你现在在哪里,你需要去哪里,所以你做while循环,直到你到达右边x然后做while循环直到你到达右边y。你还需要知道女巫的方向,所以你已经计算出它不是太难了

int char[4][8] = { values };
int fromnum;// from witch number
int tonum;// to number 
int fromx, fromy;//from coordinates
int tox,toy; // to coordinates 
for (int i = 0; i < 4; i++) {
    for (int a = 0; a < 8; a++) {
        //find a cordinates of your start and end point
    }
}

// when you know cordinates you can just supstract them and you will know the path