PTA 老鼠迷宫问题 最小代价 回溯法解决迷宫问题

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int MINLENGTH;
typedef struct Link {
	int x, y;
	struct Link* next;
}LK,*L;
int times = 0;
int row, col;
L getLNode() {
	L LHead = new LK;
	LHead->x = LHead->y = 0;
	LHead->next = NULL;
	return LHead;
}
bool IN(L LHead, L LNode) {
	L Lptr = LHead;
	while (Lptr && Lptr->next) {
		if ((Lptr->next->x == LNode->x) && (Lptr->next->y == LNode->y)) {
			return true;
		}
		Lptr = Lptr->next;
	}
	return false;
}
bool ADDLNode(L LHead,L LNode) {
	L NewNode = getLNode();
	NewNode->x = LNode->x;
	NewNode->y = LNode->y;
	NewNode->next = LHead->next;
	LHead->next = NewNode;
	return true;
}
int** getMap() {
	scanf("%d%d", &row, &col);
	MINLENGTH = row * col;
	int** arr = new int* [row];
	for (int i = 0; i < row; i++)
		arr[i] = new int[col];
	int sx, sy, ex, ey;
	scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			scanf("%2d", &arr[i][j]);
	return arr;
}
int showMap(int** arr, int row, int col) {
	printf("\n");
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++)
			printf("%2d ", arr[i][j]);
		printf("\n");
	}
	return 0;
}
void showLink(L LHead) {
	while (LHead && LHead->next) {
		printf("(%d %d)", LHead->next->x, LHead->next->y);
		LHead = LHead->next;
	}
	//printf("NULL\n");
}
void detect(int sx, int sy, int ex, int ey, int** arr,L PaPaNode) {
	if (sx == ex && sy == ey) {
		添加节点到全局变量中
		/*
		printf("i Found!!! times:%d\n",times++);*/
		printf("(%d %d)", sx, sy);
		showLink(PaPaNode);
		//if(PaPaNode)
		//	printf("\nlength(%d)\n", PaPaNode->x+arr[sx,sy]);
		//else
		//	printf("\nlength(%d)\n", 0);
		MINLENGTH = PaPaNode->x + 1;
		//printf("%d %d %d %d\n", sx, sy, ex, ey);
	}
	else {
		if (sx == row || sx == -1 || sy == col || sy == -1 || arr[sx][sy] == -1)
			return;
		if (PaPaNode && PaPaNode->x == MINLENGTH||PaPaNode&&(MINLENGTH-PaPaNode->x<abs(sx-ex)+abs(sy-ey)))
			return;
		L NewNode = getLNode();
		NewNode->x = sx;
		NewNode->y = sy;
		if (IN(PaPaNode, NewNode)) {
			delete NewNode;
			return;
		}
		L LHead = getLNode();
		if (PaPaNode)
			LHead->x = PaPaNode->x + arr[sx][sy];
		else
			LHead->x = 1;
		L Lptr = LHead;
		while (PaPaNode && PaPaNode->next) {
			Lptr->next = getLNode();
			Lptr->next->x = PaPaNode->next->x;
			Lptr->next->y = PaPaNode->next->y;
			Lptr = Lptr->next;
			PaPaNode = PaPaNode->next;
		}
		ADDLNode(LHead, NewNode);
		if (LHead && LHead->next) {
			if (LHead->next->next)
			{
				if (!(LHead->next->x == sx + 1 && LHead->next->y == sy))
					detect(sx + 1, sy, ex, ey, arr, LHead);
				if (!(LHead->next->x == sx - 1 && LHead->next->y == sy))
					detect(sx - 1, sy, ex, ey, arr, LHead);
				if (!(LHead->next->x == sx && LHead->next->y == sy + 1))
					detect(sx, sy + 1, ex, ey, arr, LHead);
				if (!(LHead->next->x == sx && LHead->next->y == sy - 1))
					detect(sx, sy - 1, ex, ey, arr, LHead);
			}
			else {
				detect(sx + 1, sy, ex, ey, arr, LHead);
				detect(sx - 1, sy, ex, ey, arr, LHead);
				detect(sx, sy + 1, ex, ey, arr, LHead);
				detect(sx, sy - 1, ex, ey, arr, LHead);
			}
		}
	}
}
int main(void) {
	int** arr = getMap();
	showMap(arr, 10, 10);
	detect(3, 6, 8, 8, arr, NULL);
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alasnot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值