ludewecy 2021-06-17 16:30 采纳率: 100%
浏览 22
已结题

紧急情况:本地运行正常 上传到服务器缺显示运行时错误(SIGSEGV)

本小白初入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;
}
  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2021-06-17 16:32
    关注

    SIGSEGV

        试图对只读映射区域进行写操作

    是不是程序里还是有野指针、越界、或者那数值当地址使用的情况

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月18日
  • 已采纳回答 3月10日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么