简体   繁体   中英

Finding shortest Path in maze with co-ordinates

Hey I'm a beginner i wrote program which is looking for shortest way, from start to end in 2 dimensional array. But my problem is that i can't find Full Path with co-ordinalte. I mean for example in that :

  public static void main(String[] args) {


    int[][] pom = new int[4][4];


    int arr[][] =
            {{0, 0, 0, 0, 0},
                    {0, 1, 1, 1, 0},
                    {0, 1, 1, 1, 1},
                    {0, 1, 1, 1, 1},
                    {0, 0, 1, 1, 1}};


    int startX = 3;
    int startY = 1;
    int endX = 2;
    int endY = 3;

    int min_dist = shortestPath(arr, pom, startX, startY, endX, endY, Integer.MAX_VALUE, 0);

    System.out.println(min_dist);

}

I woulkd like to get on output : (3,1),(3,2),(2,2),(2,3)

This is my method for that, Im trying to do that with list , but still dont have idea how to do that and how to properly clear them during process (Class Point has only 2 atrributes, int x and int y)

  static List<Point> tmp = new ArrayList<>();


public static boolean uCanGo(int mat[][], int visited[][], int x, int y) {

    if (mat[x][y] == 1 && visited[x][y] == 0) {
        //   tmp.add(new Point(x, y));

        return true;

    }


    return false;
}


public static int shortestPath(int arr[][], int visited[][], int startX, int startY, int endX, int endY, int finalDistance, int currentDistance) {
    if (startX == endX && startY == endY) {

        return Integer.min(finalDistance, currentDistance);

    }
    visited[startX][startY] = 1;



    if ((startY + 1 < 4) && uCanGo(arr, visited, startX, startY + 1)) {
        finalDistance = shortestPath(arr, visited, startX, startY + 1, endX, endY, finalDistance, currentDistance + 1);
    }
    if ((startX + 1 < 4) && uCanGo(arr, visited, startX + 1, startY)) {


        finalDistance = shortestPath(arr, visited, startX + 1, startY, endX, endY, finalDistance, currentDistance + 1);

    }
    if ((startX - 1 > 0) && uCanGo(arr, visited, startX - 1, startY)) {


        finalDistance = shortestPath(arr, visited, startX - 1, startY, endX, endY, finalDistance, currentDistance + 1);

    }
    if ((startY - 1 > 0) && uCanGo(arr, visited, startX, startY - 1)) {

        finalDistance = shortestPath(arr, visited, startX, startY - 1, endX, endY, finalDistance, currentDistance + 1);
    }
    visited[startX][startY] = 0;
    return finalDistance;
}

Sorry for problem and thanks

After help, i still cant find problem for example in that array there should be : (3,3)(4,3)(5,3)(6,3)(6,4)(6,5)

       int arr[][] =
                        {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                        {0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
                        {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1},
                        {0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0},
                        {0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0},
                        {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1},
                        {0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0},
                        {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1},
                        {0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1},
                        {0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1},
                        {0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1},
                };

        int startX = 3;
        int startY = 3;
        int endX = 6;
        int endY = 5;

Since you wanted a path I changed the returntype of your method and made some minor changes:

public static void main(String[] args) {
    int arr[][] = { { 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0 }, { 0, 1, 1, 1, 1 }, { 0, 1, 1, 1, 1 }, { 0, 0, 1, 1, 1 } };

    int startX = 3;
    int startY = 1;
    int endX = 2;
    int endY = 3;

    List<Point> path = new ArrayList<>();
    path.add(new Point(startX, startY));
    List<Point> foundPath = shortestPath(arr, startX, startY, endX, endY, Integer.MAX_VALUE, 0, path);

    for (Point p : foundPath) {
        System.out.print("(" + p.x + "," + p.y + "), ");
    }
    System.out.println();
}

public static boolean uCanGo(int mat[][], List<Point> visited, int x, int y) {
    if (mat[x][y] == 0) {
        return false;
    }
    for (Point p : visited) {
        if (p.x == x && p.y == y) {
            return false;
        }
    }
    return true;
}

public static List<Point> shortestPath(int arr[][], int startX, int startY, int endX, int endY, int finalDistance,
        int currentDistance, List<Point> visited) {
    if (startX == endX && startY == endY) {
        return visited;
    }

    if ((startY + 1 < 4) && uCanGo(arr, visited, startX, startY + 1)) {
        List<Point> nextPath = new ArrayList<>(visited);
        nextPath.add(new Point(startX, startY + 1));
        return shortestPath(arr, startX, startY + 1, endX, endY, finalDistance, currentDistance + 1, nextPath);
    }
    if ((startX + 1 < 4) && uCanGo(arr, visited, startX + 1, startY)) {
        List<Point> nextPath = new ArrayList<>(visited);
        nextPath.add(new Point(startX + 1, startY));
        return shortestPath(arr, startX + 1, startY, endX, endY, finalDistance, currentDistance + 1, nextPath);

    }
    if ((startX - 1 > 0) && uCanGo(arr, visited, startX - 1, startY)) {
        List<Point> nextPath = new ArrayList<>(visited);
        nextPath.add(new Point(startX - 1, startY));
        return shortestPath(arr, startX - 1, startY, endX, endY, finalDistance, currentDistance + 1, nextPath);

    }
    if ((startY - 1 > 0) && uCanGo(arr, visited, startX, startY - 1)) {
        List<Point> nextPath = new ArrayList<>(visited);
        nextPath.add(new Point(startX, startY - 1));
        return shortestPath(arr, startX, startY - 1, endX, endY, finalDistance, currentDistance + 1, nextPath);
    }
    return visited;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM