今天贴出一个迷宫算法,所谓迷宫,既然用一个二维数组来描述,墙用0X2A表示,不能走,起点给定一个坐标,终点为一个给的确定的值。使用了图论的深度优先遍历,自己定义了一个路径栈,下面上代码
1 #include "stdafx.h" 2 #include "windows.h" 3 4 bool findNext(int row, int col); //主要寻路函数,row,col分别为需要确定的行列坐标 5 void MyNodePush(int row, int col); //路径值入栈 6 void MyNodePop(); //路径出栈 7 void MyDodeDelete();//最后的清理函数,用于清除路径栈 8 bool isValidPoint(int row, int col); //判断当前点是否为有效点 9 void DisPlay();//打印路径 10 typedef struct _NODE //自定义的结点,栈用双向链表来构造 11 { 12 int row; 13 int rol; 14 struct _NODE *NextNode; 15 struct _NODE *BackNode; 16 }NODE, *PNODE; 17 18 19 BYTE myStr[11][16] = { 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x20, 0x2A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2A, 0x2E, 0x2E, 0x2E, 0x2A, 0x2A, 0x2A, 0x2A, 20 0x20, 0x2A, 0x2E, 0x2A, 0x2A, 0x2A, 0x2A, 0x2E, 0x2E, 0x2E, 0x2A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2A, 0x20, 0x2A, 0x2E, 0x2E, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2E, 0x2A, 21 0x20, 0x20, 0x2A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2A, 0x2E, 0x2E, 0x2E, 0x2A, 0x2E, 0x2E, 0x2E, 0x2A, 0x2A, 0x20, 0x2A, 0x2A, 0x2A, 0x2A, 0x2E, 0x2A, 0x2E, 0x2A, 0x2E, 0x2E, 0x2E, 0x2A, 0x2A, 0x2A, 22 0x2A, 0x20, 0x2A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2A, 0x2E, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x20, 0x20, 0x2A, 0x2E, 0x2A, 0x2A, 0x2A, 0x2E, 0x2E, 0x2A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2A, 23 0x20, 0x2A, 0x2E, 0x2E, 0x2A, 0x2A, 0x2A, 0x2E, 0x2A, 0x2A, 0x2E, 0x2A, 0x2A, 0x2A, 0x2E, 0x2A, 0x43, 0x2E, 0x2E, 0x2A, 0x2A, 0x2A, 0x2A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2A, 0x58, 0x2E, 0x2E, 0x2A, 24 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A}; 25 //迷宫,0x2a为墙 0x58为终点, 26 27 PNODE HeadNode;//栈底指针 28 PNODE CurrentNode;//栈顶指针 29 int _tmain(int argc, _TCHAR* argv[]) 30 { 31 HeadNode = (PNODE)malloc(sizeof(NODE)); //为头结点分配内在 32 memset(HeadNode, -1, sizeof(NODE)); 33 CurrentNode = HeadNode;//栈底栈顶指针初始时为重合 34 35 if (findNext(1, 0))//开始寻路,初始位置为(1,0) 36 { 37 DisPlay(); 38 } 39 else 40 { 41 printf_s("no way to get out from the maze"); 42 } 43 44 /* 45 MyNodePush(1, 2); 46 MyNodePop(); 47 MyNodePush(1, 2); 48 MyNodePush(1, 2); 49 MyNodePop(); 50 MyNodePush(1, 2); 51 */ 52 MyDodeDelete(); 53 system("pause"); 54 return 0; 55 } 56 bool findNext(int row, int col) 57 { 58 MyNodePush(row, col); //将此点入栈 59 if (myStr[row][col] == 0x58) //判断是否为终点是的话返回正确 60 return true; 61 if (!isValidPoint(row, col)) //如果是墙或者超过了迷宫范围则该点出栈并返回错 62 { 63 MyNodePop(); 64 return false; 65 } 66 if (findNext(row - 1, col) || findNext(row + 1, col) || findNext(row, col - 1) || findNext(row, col + 1))//递归的寻找该点的上下左右四个点,只要有一个点返回真则可以判断路径为真,因为只有当点为终点的时候才会返回真,这里可以优化,由那一点走过来可以不用寻找
67 { 68 return true; 69 } 70 else 71 { 72 MyNodePop();//如果从该点上下左右四个点出发的路径都不能找到终点,则该点不可能位于正确路径上。 73 return false; 74 } 75 } 76 void MyNodePush(int row, int col) 77 { 78 PNODE NewNOde =(PNODE) malloc(sizeof(NODE)); 79 NewNOde->row = row; 80 NewNOde->rol = col; 81 NewNOde->NextNode = NULL; 82 NewNOde->BackNode = CurrentNode; 83 CurrentNode->NextNode = NewNOde; 84 CurrentNode = NewNOde; 85 } 86 void MyNodePop() 87 { 88 if (CurrentNode != HeadNode) 89 { 90 CurrentNode->BackNode->NextNode = NULL; 91 PNODE temp = CurrentNode->BackNode; 92 free(CurrentNode); 93 CurrentNode = temp; 94 } 95 96 } 97 void MyDodeDelete() 98 { 99 if (CurrentNode == HeadNode) 100 return; 101 PNODE nodeToDelete = HeadNode; 102 while (true) 103 { 104 PNODE temp = nodeToDelete; 105 if (nodeToDelete->NextNode == NULL) 106 { 107 free(nodeToDelete); 108 break; 109 } 110 nodeToDelete = nodeToDelete->NextNode; 111 free(temp); 112 } 113 } 114 bool isValidPoint(int row, int col) 115 { 116 if (row < 0 || row>10 || col < 0 || col>15)//这里根据是否是墙,或者超过迷宫边界判断 117 return false; 118 if (myStr[row][col] == 0x2a) 119 return false; 120 PNODE temp = HeadNode; 121 while (temp->NextNode!=NULL)//这里遍历已经访问过的结点,如果已访问过,则返回错误 122 { 123 if (row == temp->row && col == temp->rol) 124 return false; 125 temp = temp->NextNode; 126 /* 127 if (temp->NextNode == NULL) 128 { 129 if (row == temp->row && col == temp->rol) 130 return false; 131 break; 132 } 133 */ 134 } 135 return true; 136 } 137 void DisPlay() 138 { 139 PNODE temp = HeadNode; 140 while (temp->NextNode != NULL) 141 { 142 if (temp !=HeadNode) 143 printf_s("%x,%x ", temp->row, temp->rol); 144 temp = temp->NextNode; 145 if (temp->NextNode == NULL) 146 { 147 printf_s("%x,%x ", temp->row, temp->rol); 148 break; 149 } 150 } 151 }