分支界限法

迷宫问题(20分)
题目内容:

定义一个二维数组,例如:

int maze[5][5] = {

           0, 1, 0, 0, 0,

           0, 1, 0, 1, 0,

           0, 0, 0, 0, 0,

           0, 1, 1, 1, 0,

           0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入格式:

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一最短路径。

输出格式:

左上角到右下角的最短路径,格式如样例所示。

输入样例:

0 1 0 0 0

0 1 0 1 0

0 0 0 0 0

0 1 1 1 0

0 0 0 1 0

输出样例:

(0, 0)

(1, 0)

(2, 0)

(2, 1)

(2, 2)

(2, 3)

(2, 4)

(3, 4)

(4, 4)

#include<stdio.h>
#include<stdlib.h>
//对不起,对我来说太难了,所以用了网上大佬的代码
//迷宫问题

struct path
{
int x; //横座标
int y; //纵座标
struct path *next;
};

typedef struct Link
{
struct path *head;
}link;

//模拟为栈
void push(link que, int x, int y)
{
struct path p;
p = (struct path
)malloc(sizeof(struct path));
p->x = x;
p->y = y;
p->next = que->head;;
que->head = p;
/

que->rear->next = p;
que->rear = p;
que->rear->next = NULL;
*/
}

void pop(link *que)
{
//如果不合适将刚入栈的节点删除
que->head = que->head->next;
}

/*
void pop2(link *que, int *x, int *y)
{
struct path *temp;
temp = que->head->next;
que->head->next = temp->next; //将temp结点出队
*x = temp->x;
*y = temp->y;
free(temp);
}
*/

void r(int a[5][5], int x, int y, link *que)
{
if( x < 5 && x >= 0 && y < 5 && y >= 0)
{
if(x == 4 && y == 4)
return;
if(a[x][y + 1] == 0 && (y + 1) < 5)
{
y++;
push(que, x, y);
r(a, x, y, que);
return;
}
if(a[x + 1][y] == 0 && (x + 1) < 5)
{
x++;
push(que, x, y);
r(a, x, y, que);
return;
}
if(a[x - 1][y] == 0 && x - 1 >= 0)
{
a[x][y] = 1;
pop(que);
x–;
r(a, x, y, que);
return;
}
if(a[x][y - 1] == 0 && y - 1 >= 0)
{
a[x][y] = 1;
pop(que);
y–;
r(a, x, y, que);
return;
}
}
}

int main(void)
{
int a[5][5];
int i, j;
int x, y;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
scanf("%d", &a[i][j]);
}
}
link *top, *temp;
top = temp = (link *)malloc(sizeof(link));
top->head = (struct path *)malloc(sizeof(struct path));
r(a, 0, 0, top);
printf("(0, 0)\n");
//此处数组是为了存储路径,因为使用的是栈结构,所以存储的路径必然是倒序!
int PATH[25] = {0};
int count = 0;

//将栈倒序的路径存储进数组中,二维数组太占用空间所以此处我们使用一维数组,并使用数学方法存储!!
while(top->head->next != NULL)
{
	PATH[count] = top->head->x *10 + top->head->y;
	++count;
	top->head = top->head->next;
}
for(i = count - 1; i >= 0; --i)
{	
	int num_x = PATH[i] / 10;  //从数据中提取出X
	int num_y = PATH[i] % 10; //从数组中提取出Y
	//打印路径
	printf("(%d, %d)\n", num_x, num_y);
}
return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值