Mouse maze algorithm maze backtracking algorithm C language algorithm

From Colorant Parakeet, 1 Year ago, written in C, viewed 253 times.
URL https://pastebin.vip/view/5fed3479
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int visit ( int, int );
  5.  
  6. int maze[7][7] = {{2, 2, 2, 2, 2, 2, 2},
  7.     {2, 0, 0, 0, 0, 0, 2},
  8.     {2, 0, 2, 0, 2, 0, 2},
  9.     {2, 0, 0, 2, 0, 2, 2},
  10.     {2, 2, 0, 2, 0, 2, 2},
  11.     {2, 0, 0, 0, 0, 0, 2},
  12.     {2, 2, 2, 2, 2, 2, 2}
  13. };
  14.  
  15. int startI = 1, startJ = 1;  // 入口
  16. int endI = 5, endJ = 5;  // 出口
  17. int success = 0;
  18.  
  19. int main ( void )
  20. {
  21.     int i, j;
  22.  
  23.     printf ( "显示迷宫:\n" );
  24.     for ( i = 0; i < 7; i++ )
  25.     {
  26.         for ( j = 0; j < 7; j++ )
  27.             if ( maze[i][j] == 2 )
  28.                 printf ( "█" );
  29.             else
  30.                 printf ( "  " );
  31.         printf ( "\n" );
  32.     }
  33.  
  34.     if ( visit ( startI, startJ ) == 0 )
  35.         printf ( "\n没有找到出口!\n" );
  36.     else
  37.     {
  38.         printf ( "\n显示路径:\n" );
  39.         for ( i = 0; i < 7; i++ )
  40.         {
  41.             for ( j = 0; j < 7; j++ )
  42.             {
  43.                 if ( maze[i][j] == 2 )
  44.                     printf ( "█" );
  45.                 else if ( maze[i][j] == 1 )
  46.                     printf ( "◇" );
  47.                 else
  48.                     printf ( "  " );
  49.             }
  50.             printf ( "\n" );
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
  57. int visit ( int i, int j )
  58. {
  59.     maze[i][j] = 1;
  60.  
  61.     if ( i == endI && j == endJ )
  62.         success = 1;
  63.  
  64.     if ( success != 1 && maze[i][j+1] == 0 ) visit ( i, j+1 );
  65.     if ( success != 1 && maze[i+1][j] == 0 ) visit ( i+1, j );
  66.     if ( success != 1 && maze[i][j-1] == 0 ) visit ( i, j-1 );
  67.     if ( success != 1 && maze[i-1][j] == 0 ) visit ( i-1, j );
  68.  
  69.     if ( success != 1 )
  70.         maze[i][j] = 0;
  71.  
  72.     return success;
  73. }
  74.  

Reply to "Mouse maze algorithm maze backtracking algorithm C language algorithm"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website