老鼠走迷宮算法中的一點優化

         老鼠走迷宮的算法當中在搜索路線時,有一些位置是死衚衕,事先標記出來可以大大減少無效搜索,提高搜索速度。

比如三個方向都是牆壁,只有一個方向可以走通的非起點非終點位置就可以標記出來,在搜索時根本不用再去計算。

 

原始圖

 

標記圖

 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#ifdef _WIN32
   #include <windows.h>
   void gotoxy(int x, int y);
#endif

void visit(int x, int y);

#define MOUSEMAZE1 1
#define MOUSEMAZE2 1

#ifdef MOUSEMAZE1
#define XSIZE 70
#define YSIZE 22
int maze[YSIZE][XSIZE] =
 {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  {2,0,0,0,0,0,0,0,2,2,2,0,0,0,2,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,2,0,0,2,0,2,0,0,2,2,0,2,2,0,2,2,2,2,2,2,2,0,0,2,0,2,2,0,2},
  {2,0,2,0,2,0,2,0,0,2,2,0,2,2,0,2,2,2,2,2,2,2,0,0,2,0,0,0,2,2,0,2,0,2,2,2,2,2,2,2,0,0,2,0,0,0,2,2,0,0,2,0,0,0,2,2,0,0,2,2,2,0,0,0,2,0,0,0,2,2},
  {2,0,2,0,0,0,2,0,0,0,2,2,2,2,2,2,2,0,0,2,0,0,0,2,2,0,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,0,0,2,2,0,2,2,2,2,2,2,2,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,0,2},
  {2,0,2,0,2,0,2,0,2,0,2,2,0,2,0,0,0,0,2,2,0,0,2,0,0,0,2,2,0,0,0,0,2,2,2,0,0,2,0,2,0,2,2,2,2,2,2,0,0,2,0,0,0,2,2,0,0,0,0,0,0,0,2,0,0,0,2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,0,2},
  {2,0,2,0,2,0,2,0,2},
  {2,0,2,0,0,0,2,0,2},
  {2,0,2,0,2,0,2,0,2},
  {2,0,2,0,2,2,2,0,2},
  {2,0,2,0,2,2,2,0,2},
  {2,0,2,0,2,2,2,0,2},
  {2,2,2,2,2,2,2,2,2}};
#endif
#endif

int startx = 1, starty = 1;              /* 入口 */
int endx = XSIZE - 2, endy = YSIZE - 2;  /* 出口 */
const int top = 2;
const int left = 5;

long routes = 0;

#ifdef _WIN32
   #define WALLCODE 'X'

   HANDLE hOut;

   void gotoxy(int x, int y)
   {
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOut, pos); /* 設置光標位置 */
   }
#else
   #define WALLCODE 219
#endif


int main(void)
{
 int i, j, k;

#ifdef _WIN32
   hOut = GetStdHandle(STD_OUTPUT_HANDLE);
#endif

 for (i = 0; i < YSIZE; i++)
  {
   for (j = 0; j < XSIZE; j++)
    {
     gotoxy(j + left, i + top);
     if (maze[i][j] == 2)
      printf("%c", WALLCODE);
     else
      printf(" ");
    }

   printf("\n");
  }
 gotoxy(startx + left, starty + top);
 printf("S");
 gotoxy(endx + left, endy + top);
 printf("E");

 getch();

 /* 多次搜索標記死角,直到找不到爲止 */
 k = 1;
 while(k)
  {
   k = 0;
   for (i = starty; i <= endy; i++)
    {
     for (j = startx; j <= endx; j++)
      {
       if (maze[i][j] == 0 && (!(j == startx && i == starty)) && (!(j == endx && i == endy)))
        {
         if (maze[i - 1][j] != 0 && maze[i][j + 1] != 0 && maze[i + 1][j] != 0)
          {
           maze[i][j] = 3;
           k = 1;
           gotoxy(j + left, i + top);
           printf("%c", 'c');
          }
         else if (maze[i][j + 1] != 0 && maze[i + 1][j] != 0 && maze[i][j - 1] != 0)
          {
           maze[i][j] = 3;
           k = 1;
           gotoxy(j + left, i + top);
           printf("%c", 'c');
          }
         else if (maze[i + 1][j] != 0 && maze[i][j - 1] != 0 && maze[i - 1][j] != 0)
          {
           maze[i][j] = 3;
           k = 1;
           gotoxy(j + left, i + top);
           printf("%c", 'c');
          }
         else if (maze[i][j - 1] != 0 && maze[i - 1][j] != 0 && maze[i][j + 1] != 0)
          {
           maze[i][j] = 3;
           k = 1;
           gotoxy(j + left, i + top);
           printf("%c", 'c');
          }
        }
      }
    }
  }

 gotoxy(1, 1);
 getch();

 visit(startx, starty);
 printf("\n%ld", routes);

 getch();
}

void visit(int x, int y)
{
 maze[y][x] = 1;

 if (y == endy && x == endx)
  {
   gotoxy(1, 1);
   printf("%ld", ++routes);
  }

 if (maze[y + 1][x] == 0)
  visit(x, y + 1);
 if (maze[y][x + 1] == 0)
  visit(x + 1, y);
 if (maze[y - 1][x] == 0)
  visit(x, y - 1);
 if (maze[y][x - 1] == 0)
  visit(x - 1, y);

 maze[y][x] = 0;
}


發佈了29 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章