迷宫(多出口)

/*
* =====================================================================================
*
* Filename: maze.c
*
* Description:
*
* Version: 1.0
* Created: 2011年12月09日 21时33分32秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int b[5][15]= {0};
int cnt_path = 0;
int min = 65535;
/*stack*/
struct stack_node
{
int x;
int y;
struct stack_node *next_ptr;
};


void push(struct stack_node **top_ptr, int x, int y)
{
struct stack_node *new_ptr;
new_ptr = malloc(sizeof(struct stack_node));
if (new_ptr != NULL)
{
new_ptr->x = x;
new_ptr->y = y;
new_ptr->next_ptr = *top_ptr;
*top_ptr = new_ptr;

}
}
void pop(struct stack_node** top_ptr)
{
struct stack_node* temp_ptr;
temp_ptr = *top_ptr;
*top_ptr = (*top_ptr)->next_ptr;
free(temp_ptr);
}


void print_stack(struct stack_node* current_ptr)
{
if(current_ptr == NULL)
{
printf("the stack is empty.\n");
}
else
{
while(current_ptr != NULL)
{
printf("(%d,%d)\n", current_ptr->x, current_ptr->y);
current_ptr = current_ptr->next_ptr;
}
printf("NULL\n ");
}
}
int is_empty(struct stack_node * top_ptr)
{
return top_ptr == NULL;
}


int find(int a[5][15], int i, int j, int n, int m, struct stack_node *stack_ptr)
{


b[i][j] = 1;
cnt_path++;
push(&stack_ptr, i+1, j+1);


if (a[i][j] == 2)
{
print_stack(stack_ptr);
printf("%d\n", cnt_path);
cnt_path--;
return 0;
}


if (j + 1 < m)//右
{
if (0 != a[i][j+1] && 0 == b[i][j+1] )
{
find(a, i, j+1, n, m,stack_ptr);
}
}


if (i + 1 < n)//下
{
if (0 != a[i+1][j] && 0 == b[i+1][j])
{
find(a, i+1, j, n, m,stack_ptr);
}
}


if (j - 1 >= 0 )//左
{
if (0 != a[i][j-1] && 0 == b[i][j-1])
{
find(a, i, j-1, n, m,stack_ptr);
}
}


if (i - 1 >= 0)//向上
{
if (0 != a[i-1][j] && 0 == b[i-1][j])
{
find(a, i-1, j, n, m,stack_ptr);
}
}

b[i][j] = 0;
cnt_path--;
pop(&stack_ptr);















}


int main()
{
int n,m;
n = 5;
m = 15; //exit 2 start 3
struct stack_node * stack_ptr = NULL;//在第第一次push的时候NULL会被复制到top_ptr->next_ptr中
int a[5][15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
3,1,1,1,1,1,1,1,1,1,1,1,2,0,0,
0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,
0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,2,0,0};




find(a, 1, 0, n, m, stack_ptr);//1,0代表出发的坐标,也就是3的坐标




}

程序在VC++ 6下顺利编译通过。 一、 实验目的: (1) 熟练掌握链栈的基本操作及应用。 (2) 利用链表作为栈的存储结构,设计实现一个求解迷宫的非递归程序。 二、实验内容: 【问题描述】 以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对信任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。 【基本要求】 首先实现一个链表作存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以三元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走到下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1),(1,2,2),(2,2,2),(3,2,3),(3,1,2),……。 【测试数据】 迷宫的测试数据如下:左上角(1,1)为入口,右下角(8,9)为出口。 1 2 3 4 5 6 7 8 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 以方阵形式输出迷宫及其通路。 输出: 请输入迷宫的长和宽:5 5 请输入迷宫内容: 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 迷宫的路径为 括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向) (1,1,1,↓) (2,1,2,→) (2,2,1,↓) (3,2,1,↓) (4,2,2,→) (4,3,1,↓) (5,3,2,→) (5,4,2,→) (5,5,0,) 迷宫路径探索成功!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值