// Maze.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACK_ADD_SIZE 10
#define ROW_NUMBER 3
#define COL_NUMBER 3
typedef struct
{
int row;
int col;
int direction;
bool canPass;
bool inStack;
} Cell;
typedef struct
{
Cell *base;
Cell *top;
int stackSize;
} PathStack;
int InitStack(PathStack &stack)
{
stack.base = (Cell*)malloc(sizeof(Cell) * STACK_INIT_SIZE);
if (stack.base == NULL)
{
return -1;
}
stack.top = stack.base;
stack.stackSize = STACK_INIT_SIZE;
return 0;
}
int Push(PathStack &stack, Cell element)
{
if (stack.top - stack.base >= stack.stackSize)
{
stack.base = (Cell*)realloc(stack.base, sizeof(PathStack) * (stack.stackSize + STACK_ADD_SIZE));
if (stack.base == NULL)
return -1;
stack.top = stack.base + stack.stackSize;
stack.stackSize += STACK_ADD_SIZE;
}
*stack.top = element;
stack.top++;
return 0;
}
Cell Pop(PathStack &stack)
{
if (stack.top == stack.base)
{
Cell tmp;
tmp.row = tmp.col = tmp.direction = -1;
return tmp;
}
return *(--stack.top);
}
Cell GetTop(PathStack stack)
{
if (stack.base == stack.top)
{
Cell cellTop;
cellTop.row = -1;
cellTop.col = -1;
cellTop.direction = -1;
cellTop.inStack = false;
cellTop.canPass = false;
return cellTop;
}
return *(--stack.top);
}
int FindAllPath(int mazeArray[ROW_NUMBER][COL_NUMBER])
{
Cell cellArray[ROW_NUMBER][COL_NUMBER];
int accessNum = 0;
for (int i = 0; i < ROW_NUMBER; i++)
{
for (int j = 0; j < COL_NUMBER; j++)
{
cellArray[i][j].row = i;
cellArray[i][j].col = j;
cellArray[i][j].direction = 0;
if (mazeArray[i][j])
{
cellArray[i][j].canPass = false;
}
else
{
cellArray[i][j].canPass = true;
}
cellArray[i][j].inStack = false;
}
}
PathStack stack;
InitStack(stack);
if (!Push(stack, cellArray[0][0]))
{
cellArray[0][0].inStack = true;
}
else
{
return -1;
}
int row, col; // 当前位置的坐标
row = col = 0;
while (stack.base != stack.top)
{
// 依东南西北四个方位进行探索
Cell cellTop = GetTop(stack);
row = cellTop.row;
col = cellTop.col;
if (row == 0 && col == 0)
{
for (int i = 0; i < ROW_NUMBER; i++)
{
for (int j = 0; j < COL_NUMBER; j++)
{
if (i == 0 && j == 0)
{
continue;
}
if (mazeArray[i][j])
{
cellArray[i][j].canPass = false;
}
else
{
cellArray[i][j].canPass = true;
}
cellArray[i][j].direction = 0;
}
}
}
if (cellTop.row == ROW_NUMBER - 1 && cellTop.col == COL_NUMBER - 1)
{
accessNum++;
Cell *pBase = stack.base;
printf("第%d条通路: ", accessNum);
for (int i = 0; i < stack.top - stack.base; i++)
{
if (i == stack.top - stack.base - 1)
{
printf("[%d, %d]\n", (*pBase).row, (*pBase).col);
}
else
{
printf("[%d, %d]->", (*pBase).row, (*pBase).col);
}
pBase++;
}
Pop(stack);
cellArray[ROW_NUMBER - 1][COL_NUMBER - 1].inStack = false;
continue;
}
int east, south, west, north;
east = col + 1;
south = row + 1;
west = col - 1;
north = row - 1;
if (east < COL_NUMBER && !cellArray[row][col].direction) // 探索东方位
{
if (cellArray[row][east].canPass && !cellArray[row][east].inStack)
{
Push(stack, cellArray[row][east]);
cellArray[row][east].inStack = true;
col = east;
}
cellArray[row][east - 1].direction = 1;
continue;
}
if (south < ROW_NUMBER && cellArray[row][col].direction <= 1)
{
if (cellArray[south][col].canPass && !cellArray[south][col].inStack)
{
Push(stack, cellArray[south][col]);
cellArray[south][col].inStack = true;
row = south;
}
cellArray[south - 1][col].direction = 2;
continue;
}
if (west >= 0 && cellArray[row][col].direction <= 2) // 探索东方位
{
if (cellArray[row][west].canPass && !cellArray[row][west].inStack)
{
Push(stack, cellArray[row][west]);
cellArray[row][west].inStack = true;
col = west;
}
cellArray[row][west + 1].direction = 3;
continue;
}
if (north >= 0 && cellArray[row][col].direction <= 3)
{
if (cellArray[north][col].canPass && !cellArray[north][col].inStack)
{
Push(stack, cellArray[north][col]);
cellArray[north][col].inStack = true;
row = north;
}
cellArray[north + 1][col].direction = 4;
continue;
}
cellArray[row][col].canPass = false;
cellArray[row][col].inStack = false;
Cell cellPop = Pop(stack);
cellArray[cellPop.row][cellPop.col].direction = 0;
if (mazeArray[cellPop.row][cellPop.col])
{
cellArray[cellPop.row][cellPop.col].canPass = false;
}
else
{
cellArray[cellPop.row][cellPop.col].canPass = true;
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int mazeArray[ROW_NUMBER][COL_NUMBER];
for (int i = 0; i < ROW_NUMBER; i++)
{
for (int j = 0; j < COL_NUMBER; j++)
{
scanf("%d", &mazeArray[i][j]);
}
}
FindAllPath(mazeArray);
return 0;
}
栈--求迷宫所有解
最新推荐文章于 2023-10-02 22:34:23 发布