>  기사  >  백엔드 개발  >  미로 속의 쥐가 여러 걸음이나 점프를 할 수 있나요?

미로 속의 쥐가 여러 걸음이나 점프를 할 수 있나요?

WBOY
WBOY앞으로
2023-08-29 12:17:06829검색

미로 속의 쥐가 여러 걸음이나 점프를 할 수 있나요?

미로 속의 쥐 문제는 잘 알려진 역추적 문제 중 하나입니다. 여기서 우리는 문제가 거의 변하지 않은 것을 볼 수 있습니다. NxN 미로 M이 주어졌다고 가정하자. 시작점은 왼쪽 위 모서리 M[0, 0]이고, 끝점은 오른쪽 아래 모서리 M[N – 1, N – 1]입니다. 마우스가 시작점에 배치됩니다. 우리의 목표는 마우스가 목적지에 도달할 수 있도록 시작점에서 끝점까지의 경로를 찾는 것입니다. 여기에서는 쥐가 점프할 수 있습니다(변형). 이제 몇 가지 제한 사항이 있습니다.

  • 마우스는 오른쪽이나 아래로 움직일 수 있습니다.
  • 미로 속 세포에 0이 있으면 세포가 막혔다는 뜻입니다.
  • 0이 아닌 셀은 유효한 경로를 나타냅니다.
  • 칸에 있는 숫자는 쥐가 그 칸에서 할 수 있는 최대 점프 횟수를 나타냅니다.
  • ul>

    알고리즘

    ratInMaze

    begin
       if destination is reached, then
          print the solution matrix
       else
          1. Place the current cell inside the solution matrix as 1
          2. Move forward or jump (check max jump value) and recursively check if move leads to solution or not.
          3. If the move taken from the step 2 is not correct, then move down, and check it leads to the solution or not
          4. If none of the solutions in step 2 and 3 are correct, then make the current cell 0.
       end if
    end

    #include <iostream>
    #define N 4
    using namespace std;
    void dispSolution(int sol[N][N]) {
       for (int i = 0; i < N; i++) {
          for (int j = 0; j < N; j++)
             cout << sol[i][j] << " ";
          cout << endl;
       }
    }
    bool isSafe(int maze[N][N], int x, int y) { //check whether x,y is valid or not
       // when (x, y) is outside of the maze, then return false
       if (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] != 0)
          return true;
       return false;
    }
    bool ratMazeSolve(int maze[N][N], int x, int y, int sol[N][N]) {
       if (x == N - 1 && y == N - 1) { //if destination is found, return true
          sol[x][y] = 1;
          return true;
       }
       if (isSafe(maze, x, y)) {
          sol[x][y] = 1; //mark 1 into solution matrix
          for (int i = 1; i <= maze[x][y] && i < N; i++) {
             if (ratMazeSolve(maze, x + i, y, sol)) //move right
                return true;
             if (ratMazeSolve(maze, x, y + i, sol)) //move down
                return true;
          }
          sol[x][y] = 0; //if the solution is not valid, then make it 0
          return false;
       }
       return false;
    }
    bool solveMaze(int maze[N][N]) {
       int sol[N][N] = { { 0, 0, 0, 0 },
          { 0, 0, 0, 0 },
          { 0, 0, 0, 0 },
          { 0, 0, 0, 0 }
       };
       if (!ratMazeSolve(maze, 0, 0, sol)) {
          cout << "Solution doesn&#39;t exist";
          return false;
       }
       dispSolution(sol);
       return true;
    }
    main() {
       int maze[N][N] = { { 2, 1, 0, 0 },
          { 3, 0, 0, 1 },
          { 0, 1, 0, 1 },
          { 0, 0, 0, 1 }
       };
       solveMaze(maze);
    }

    출력

    1 0 0 0
    1 0 0 1
    0 0 0 1
    0 0 0 1

위 내용은 미로 속의 쥐가 여러 걸음이나 점프를 할 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제