简单迷宫
#include <stdio.h>
#include <stdlib.h>
typedef struct NodePosition
{
int row;
int col;
int up;
int down;
int left;
int right;
} NodePosition;
typedef struct StackNode
{
struct StackNode *next;
NodePosition nodePos;
} StackNode, *StackNodePtr;
typedef struct PosStack
{
int length;
StackNodePtr top;
} PosStack;
int CheckPass(NodePosition pos, int *mapPtr, int *flag);
void PushStack(PosStack *ps, NodePosition pos);
int PopStack(PosStack *ps, NodePosition *posPtr);
void FindPath(NodePosition pos, int *mapPtr, PosStack *ps, int *flag);
int mapRow = 0, mapCol = 0;
int main()
{
scanf("%d %d", &mapRow, &mapCol);
int *mapPtr = (int *)malloc(sizeof(int *) * mapRow * mapCol);
int *flag = (int *)malloc(sizeof(int *) * mapRow * mapCol);
for (int i = 0; i < mapRow; i++)
{
for (int j = 0; j < mapCol; j++)
{
scanf("%d", mapPtr + mapRow * i + j);
*(flag + mapRow * i + j) = 0;
}
}
for (int i = 0; i < mapRow; i++)
{
for (int j = 0; j < mapCol; j++)
{
printf("%d", *(mapPtr + mapRow * i + j));
}
printf("\n");
}
NodePosition pos;
pos.row = pos.col = 0;
pos.left = pos.up = 1;
pos.right = pos.down = 0;
PosStack *ps = (PosStack *)malloc(sizeof(PosStack *));
ps->length = 0;
ps->top = (StackNodePtr)malloc(sizeof(StackNodePtr));
FindPath(pos, mapPtr, ps, flag);
if (ps->length)
{
while (ps->length)
{
NodePosition *posPtr = (NodePosition *)malloc(sizeof(NodePosition *));
PopStack(ps, posPtr);
printf("(%d, %d)", posPtr->row, posPtr->col);
}
}
return 0;
}
int CheckPass(NodePosition pos, int *mapPtr, int *flag)
{
if (pos.row >= 0 &&
pos.row < mapRow &&
pos.col >= 0 &&
pos.col < mapCol &&
*(mapPtr + mapRow * pos.row + pos.col) == 0 &&
*(flag + mapRow * pos.row + pos.col) == 0)
return 1;
return 0;
}
void PushStack(PosStack *ps, NodePosition pos)
{
StackNodePtr stackNode = (StackNodePtr)malloc(sizeof(StackNodePtr));
stackNode->nodePos = pos;
stackNode->next = ps->top;
ps->top = stackNode;
ps->length++;
}
int PopStack(PosStack *ps, NodePosition *posPtr)
{
if (ps->length == 0)
{
printf("PosStack is empty");
return 0;
}
StackNodePtr q = ps->top;
*posPtr = q->nodePos;
ps->top = q->next;
ps->length--;
return 1;
}
void FindPath(NodePosition pos, int *mapPtr, PosStack *ps, int *flag)
{
if (CheckPass(pos, mapPtr, flag))
{
*(flag + mapRow * pos.row + pos.col) = 1;
if (pos.row == mapRow - 1 && pos.col == mapCol - 1)
{
PushStack(ps, pos);
return;
}
NodePosition pos_new;
pos_new.left = pos_new.right = pos_new.down = pos_new.up = 0;
if (pos.right == 0)
{
pos.right = 1;
PushStack(ps, pos);
pos_new.row = pos.row;
pos_new.col = pos.col + 1;
pos_new.left = 1;
FindPath(pos_new, mapPtr, ps, flag);
}
else if (pos.down == 0)
{
pos.down = 1;
PushStack(ps, pos);
pos_new.row = pos.row + 1;
pos_new.col = pos.col;
pos_new.up = 1;
FindPath(pos_new, mapPtr, ps, flag);
}
else if (pos.left == 0)
{
pos.left = 1;
PushStack(ps, pos);
pos_new.row = pos.row;
pos_new.col = pos.col - 1;
pos_new.right = 1;
FindPath(pos_new, mapPtr, ps, flag);
}
else if (pos.up == 0)
{
pos.up = 1;
PushStack(ps, pos);
pos_new.row = pos.row - 1;
pos_new.col = pos.col;
pos_new.down = 1;
FindPath(pos_new, mapPtr, ps, flag);
}
else
{
NodePosition *posPtr = (NodePosition *)malloc(sizeof(NodePosition *));
if (PopStack(ps, posPtr))
{
*(flag + mapRow * (*posPtr).row + (*posPtr).col) = 0;
FindPath(*posPtr, mapPtr, ps, flag);
}
else
return;
}
}
else
{
NodePosition *posPtr = (NodePosition *)malloc(sizeof(NodePosition *));
if (PopStack(ps, posPtr))
{
*(flag + mapRow * (*posPtr).row + (*posPtr).col) = 0;
FindPath(*posPtr, mapPtr, ps, flag);
}
else
return;
}
}
地下迷宫
#include <stdio.h>
#include <stdlib.h>
typedef struct NodePosition
{
int row;
int col;
int left;
int right;
int up;
int down;
int lives;
} NodePosition;
typedef struct StackNode
{
struct StackNode *next;
NodePosition nodePos;
} StackNode, *StackNodePtr;
typedef struct PositionStack
{
StackNodePtr top;
int length;
} PositionStack;
int CheckPass(NodePosition pos, int *mapPtr, int *flag);
void PushStack(PositionStack *ps, NodePosition pos);
int PopStack(PositionStack *ps, NodePosition *posPtr);
void FindPath(NodePosition pos, int *mapPtr, PositionStack *ps, int *flag);
int mapRow = 4;
int mapCol = 4;
int frogLives = 10;
int main()
{
int *mapPtr = (int *)malloc(sizeof(int *) * mapRow * mapCol);
int *flag = (int *)malloc(sizeof(int *) * mapRow * mapCol);
int map[4][4] = {{0, 1, 1, 0},
{0, 0, 1, 0},
{1, 0, 0, 0},
{1, 1, 0, 0}};
for (int i = 0; i < mapRow; i++)
{
for (int j = 0; j < mapCol; j++)
{
*(mapPtr + mapRow * i + j) = map[i][j];
*(flag + mapRow * i + j) = 0;
}
}
for (int i = 0; i < mapRow; i++)
{
for (int j = 0; j < mapCol; j++)
{
printf("%d", *(mapPtr + mapRow * i + j));
}
printf("\n");
}
NodePosition pos;
pos.row = pos.col = 0;
pos.left = pos.up = 1;
pos.right = pos.down = 0;
pos.lives = frogLives;
PositionStack *ps = (PositionStack *)malloc(sizeof(PositionStack *));
ps->length = 0;
ps->top = (StackNodePtr)malloc(sizeof(StackNodePtr));
FindPath(pos, mapPtr, ps, flag);
if (ps->length)
{
while (ps->length)
{
NodePosition *posPtr = (NodePosition *)malloc(sizeof(NodePosition *));
PopStack(ps, posPtr);
printf("(%d, %d)", posPtr->row, posPtr->col);
}
}
return 0;
}
int CheckPass(NodePosition pos, int *mapPtr, int *flag)
{
if (pos.row >= 0 &&
pos.row < mapRow &&
pos.col >= 0 &&
pos.col < mapCol &&
*(mapPtr + mapRow * pos.row + pos.col) == 0 &&
*(flag + mapRow * pos.row + pos.col) == 0)
return 1;
else
return 0;
}
void PushStack(PositionStack *ps, NodePosition pos)
{
StackNodePtr stackNode = (StackNodePtr)malloc(sizeof(StackNodePtr));
stackNode->nodePos = pos;
stackNode->next = ps->top;
ps->top = stackNode;
ps->length++;
}
int PopStack(PositionStack *ps, NodePosition *posPtr)
{
if (ps->length == 0)
{
printf("PosStack is empty");
return 0;
}
StackNodePtr q = ps->top;
*posPtr = q->nodePos;
ps->top = q->next;
ps->length--;
return 1;
}
void FindPath(NodePosition pos, int *mapPtr, PositionStack *ps, int *flag)
{
if (CheckPass(pos, mapPtr, flag))
{
*(flag + mapRow * pos.row + pos.col) = 1;
if (pos.row == 0 && pos.col == mapCol - 1)
{
PushStack(ps, pos);
return;
}
NodePosition pos_new;
pos_new.left = pos_new.right = pos_new.up = pos_new.down = 0;
if (pos.right == 0)
{
pos.right = 1;
PushStack(ps, pos);
pos_new.row = pos.row;
pos_new.col = pos.col + 1;
pos_new.left = 1;
if ((pos.lives - 1) >= 0)
{
pos_new.lives = pos.lives - 1;
FindPath(pos_new, mapPtr, ps, flag);
}
else
{
printf("Can not escape\n");
return;
}
}
else if (pos.down == 0)
{
pos.down = 1;
PushStack(ps, pos);
pos_new.row = pos.row + 1;
pos_new.col = pos.col;
pos_new.up = 1;
pos_new.lives = pos.lives;
FindPath(pos_new, mapPtr, ps, flag);
}
else if (pos.left == 0)
{
pos.left = 1;
PushStack(ps, pos);
pos_new.row = pos.row;
pos_new.col = pos.col - 1;
pos_new.right = 1;
if ((pos.lives - 1) >= 0)
{
pos_new.lives = pos.lives - 1;
FindPath(pos_new, mapPtr, ps, flag);
}
else
{
printf("Can not escape\n");
return;
}
}
else if (pos.up == 0)
{
pos.up = 1;
PushStack(ps, pos);
pos_new.row = pos.row - 1;
pos_new.col = pos.col;
pos_new.down = 1;
if ((pos.lives - 3) >= 0)
{
pos_new.lives = pos.lives - 3;
FindPath(pos_new, mapPtr, ps, flag);
}
else
{
printf("Can not escape\n");
return;
}
}
else
{
NodePosition *posPtr = (NodePosition *)malloc(sizeof(NodePosition *));
if (PopStack(ps, posPtr))
{
*(flag + mapRow * (*posPtr).row + (*posPtr).col) = 0;
FindPath(*posPtr, mapPtr, ps, flag);
}
else
return;
}
}
else
{
NodePosition *posPtr = (NodePosition *)malloc(sizeof(NodePosition *));
if (PopStack(ps, posPtr))
{
*(flag + mapRow * (*posPtr).row + (*posPtr).col) = 0;
FindPath(*posPtr, mapPtr, ps, flag);
}
else
return;
}
}