算法: Mazing-老鼠走迷宮

說明


老鼠走迷宮遞迴求解的基本題型.咱們在二維陣列中使用 2 表示迷宮牆壁,使用 1 來表示老鼠的行走路徑,嘗試以程式求由入口至出口的路徑.數組

解法


老鼠可走上,下,左,右四個方向.入口通常在左上,出口在右下(若是說有其餘出入口狀況則下述老鼠每步默認選擇方向的順序改變)

老鼠從入口出發,每走一步進行一次四個方向的選擇,判斷下步是否有可走方向(因爲入口在左上,出口在左下,則優先選擇右,下方向,以後選擇左,上方向).沒法前進時退回上步選擇下一個可前進方向.

如此在陣列中按照順序測試四個方向,直到走出出口爲止.測試

代碼(c語言版,思路可參考)


參數聲明

int maze[7][7] = { {2,2,2,2,2,2,2},
		   {2,0,0,0,0,0,2}, 
	           {2,0,2,0,2,0,2}, 
	           {2,0,0,2,0,2,2}, 
	           {2,2,0,2,0,2,0}, 
	           {2,0,0,0,0,0,2}, 
	           {2,2,2,2,2,2,2} };//mazing map
	int(*mazeP)[7];
	mazeP = maze;//將二維數組傳遞給數組指針(數組指針是指針指向一個數組)

	int startI = 1, startJ = 1, endI = 5, endJ = 5;//entrance and exit
        int success = 0;//success mark
	int i = 1,j = 1;//initialization of import and export
複製代碼

遞迴方法

//遞迴進行老鼠每步選擇的方向,返回是否成功
int mazing_visit(int i, int j,int(*mazeP)[7],int success)
{
	mazeP[i][j] = 1;

	if (i == 5 && j == 5)success = 1;

	//優先右下,而後左上
	if ( 1 != success && mazeP[i][j + 1] == 0)mazing_visit(i, j + 1, mazeP, success);
	if (success != 1 && mazeP[i + 1][j] == 0)mazing_visit(i + 1, j, mazeP, success);
	if (success != 1 && mazeP[i][j - 1] == 0)mazing_visit(i, j - 1, mazeP, success);
	if (success != 1 && mazeP[i - 1][j] == 0)mazing_visit(i - 1, j, mazeP, success);
		

	if (success != 1)mazeP[i][j] == 0;

	return success;
}
複製代碼

完整代碼

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Mazing-老鼠走迷宮
int mazing_visit(int, int,int (*p)[7],int);

void mazing()
{
	int maze[7][7] = { {2,2,2,2,2,2,2},
						{2,0,0,0,0,0,2}, 
	                    {2,0,2,0,2,0,2}, 
	                    {2,0,0,2,0,2,2}, 
	                    {2,2,0,2,0,2,0}, 
	                    {2,0,0,0,0,0,2}, 
	                    {2,2,2,2,2,2,2} };//mazing map
	int(*mazeP)[7];
	mazeP = maze;//將二維數組傳遞給數組指針(數組指針是指針指向一個數組)

	int startI = 1, startJ = 1, endI = 5, endJ = 5;//entrance and exit
        int success = 0;//success mark
	int i = 1,j = 1;//initialization of import and export

	printf("顯示迷宮: \n");
	for (i = 0; i < 7; i++)
	{
		for (j = 0; j < 7; j++)
		{
			if (mazeP[i][j] == 2)//判斷是不是牆壁
			{
				printf("*");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
	
        //判斷是否存在出口,如有出口則打印出路徑
	if (mazing_visit(startI, startJ, mazeP,success) == 0)
	{
		printf("\n沒有找到出口\n");
	}
	else
	{
		printf("\n顯示路徑:");
		for (i = 0; i < 7; i++)
		{
			for (j = 0; j < 7; j++)
			{
				if (mazeP[i][j] == 2)
				{
					printf("*");
				}
				else if (mazeP[i][j] == 1)
				{
					printf(">");
				}
				else
				{
					printf(" ");
				}
			}
			printf("\n");
		}
			
	}

}

int mazing_visit(int i, int j,int(*mazeP)[7],int success)
{
	mazeP[i][j] = 1;

	if (i == 5 && j == 5)success = 1;

	//優先右下,而後左上
	if ( 1 != success && mazeP[i][j + 1] == 0)mazing_visit(i, j + 1, mazeP, success);
	if (success != 1 && mazeP[i + 1][j] == 0)mazing_visit(i + 1, j, mazeP, success);
	if (success != 1 && mazeP[i][j - 1] == 0)mazing_visit(i, j - 1, mazeP, success);
	if (success != 1 && mazeP[i - 1][j] == 0)mazing_visit(i - 1, j, mazeP, success);
		

	if (success != 1)mazeP[i][j] == 0;

	return success;
}
int main(void)
{
    mazing();
    return 0;
}
複製代碼
相關文章
相關標籤/搜索