本小白初入c++不久 在写有关迷宫问题时遭遇难题
求求大佬指点迷津
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Way { //到达终点的不同路径
char abc[100];
} way1[2];
int way2 = 0;
struct node {
int x;
} Node[10];//返回步数
static int nx = 0;
typedef struct queenstack { //定义栈
char data[100];
int top;
int bottom;
} stack;
typedef struct maze { //地图
int map[50][50];
int d;
int c;
} Maze;
typedef struct point { //定义一个点
int x;
int y;
} Point;
stack *stackinitial() { //栈的初始化
stack *p = (stack *)malloc(sizeof(stack));
if (p == NULL) //分配失败
return 0;
p->top = 0;
p->bottom = 0;
return p;
}
void stackpush(stack *p, char str) { //入栈
if (p->top < 64)
p->data[p->top] = str;
p->data[p->top + 1] = '\0';
p->top++;
}
char stackpop(stack *p, char str) { //出栈
if (p->top != 0) {
str = p->data[p->top - 1];
p->data[p->top - 1] = '\0';
p->top--;
return str;
}
return str;
}
void move(Point *n, char a) { //点的移动
if (a == 'R')
n->x--;
else if (a == 'L')
n->x++;
else if (a == 'U')
n->y++;
else if (a == 'D')
n->y--;
}
void Owe(Maze *m) { //地图输入函数
int i, j;
char mmm[50][50];
for (i = 0;; i++) {
gets(mmm[i]);
if (mmm[i][0] == '\0') {
break;
}
}
m->d = i; //排
m->c = strlen(mmm[0]); //列
for (i = 0; i < m->d; i++) {
for (j = 0; j < m->c; j++)
m->map[i][j] = mmm[i][j] - '0';
}
}
void go(Point *n, int i, stack *p) { //走路函数
switch (i) {
case 1:
n->x++;
stackpush(p, 'R');
break;
case 2:
n->y++;
stackpush(p, 'D');
break;
case 3:
n->x--;
stackpush(p, 'L');
break;
case 4:
n->y--;
stackpush(p, 'U');
break;
}
}
int direction(Maze *m, Point *n, stack *p, Point *l) {
int a[4] = {0}; //方向
int c[4] = {0};
int i, j = 0, core = 0; //统计可行方向
if (m->map[n->y][n->x + 1] == 0 && p->data[p->top - 1] != 'L' && n->x + 1 < m->c) //右
a[0] = 1;
if (m->map[n->y + 1][n->x] == 0 && p->data[p->top - 1] != 'U' && n->y + 1 < m->d) //下
a[1] = 2;
if (m->map[n->y][n->x - 1] == 0 && p->data[p->top - 1] != 'R' && n->x - 1 >= 0) //左
a[2] = 3;
if (m->map[n->y - 1][n->x] == 0 && p->data[p->top - 1] != 'D' && n->y - 1 >= 0) //上
a[3] = 4;
for (i = 0; i < 4; i++) {
if (a[i] != 0) {
core++;//
c[j] = a[i];
j++;
}
}
if (core == 1) {
go(n, c[0], p);
if (n->x == l->x && n->y == l->y) {
strcpy(way1[way2].abc, p->data); //将可行道路 统计
way2++;
}
} else if (core > 1 && core < 4) {
int re;
nx++;
Node[nx].x = 0; //在这一条线路走了多少步
for (i = 0; i < core; i++) { //分叉路 每条路走一遍
if (n->x == l->x && n->y == l->y) { //防止因为抵达终点程序回到过去结点走第二条路
re = 1;
break;
}
go(n, c[i], p);
if (n->x == l->x && n->y == l->y) {
strcpy(way1[way2].abc, p->data); //将可行道路 统计
way2++;
break;
}
do {
char a = 'a';
Node[nx].x++;
re = direction(m, n, p, l);
if (re == 0) {
for (; Node[nx].x > 0; Node[nx].x--) {
move(n, stackpop(p, a)); //回退
}
}
if (n->x == l->x && n->y == l->y)
break;
} while (Node[nx].x != 0 || n->x == l->x && n->y == l->y);
}
}
if (core == 0)
return 0;
else
return 1;
}
void run(Maze *m, Point *s, Point *l) { //
stack *p;
Point *n = s; //当前位置
p = stackinitial();
while (way2 == 0) { //如果地图所有点都被走过 则结束
direction(m, n, p, l); //
}
printf("%s", way1[0].abc);
}
int main(int argc, char *argv[]) {
Maze *m = (Maze *)malloc(sizeof(Maze)); //生成迷宫
Point *s = (Point *)malloc(sizeof(Point)); //入口位置
Point *l = (Point *)malloc(sizeof(Point)); //出口位置
Owe(m);//调用迷宫输入函数
s->y = 0;
s->x = 0;
l->y = m->d - 1;
l->x = m->c - 1;
run(m, s, l);
return 0;
}