迷宫求解:严蔚敏老师的数据结构的实现
2009-09-29 16:00
267 查看
本程序的运行环境为tc2.0。
#include <stdio.h>
#include <conio.h>
#define NULL 0
struct Array
{
int x;
int y;
}arr[64],*top,*base;
int n=0;
int a[8][8]={{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,1,1,0,0},{0,1,1,1,0,0,0,0},{0,0,0,1,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,1,1,0},{1,0,0,0,0,0,0,0}};
void MazePath(int i,int j)
{
if(i>=0 && j>=0 && j<8 && i<8 && a[i][j]==0 && Exit(i,j))
{
if(n!=0) top++;
top->x=i;
top->y=j;
n=n+1;
if(!(i==7 && j==7))
MazePath(i,j+1);
}
else
{
if(i==top->x && j-1==top->y)
MazePath(top->x+1,top->y);
else if(i==top->x+1 && j==top->y)
MazePath(top->x,top->y-1);
else if(i==top->x && j+1==top->y)
MazePath(top->x-1,top->y);
else
{
do
{
top--;
if(top->x==(top+1)->x && top->y+1==(top+1)->y)
{
MazePath(top->x+1,top->y);
break;
}
else if(top->x+1==(top+1)->x && top->y==(top+1)->y)
{
MazePath(top->x,top->y-1);
break;
}
else if(top->x==(top+1)->x && top->y-1==(top+1)->y)
{
MazePath(top->x-1,top->y);
break;
}
}while(top!=NULL);
}
}
}
int Exit(int i,int j)
{
struct Array *p;
p=base;
while(p!=top)
{
if(p->x==i && p->y==j)
return(0);
p++;
}
return(1);
}
void main()
{
int b;
clrscr();
top=base=arr;
MazePath(0,0);
while(top!=base)
{
printf("%d,%d/n",top->x,top->y);
top--;
}
printf("%d,%d/n",base->x,base->y);
scanf("%d",&b);
}
#include <stdio.h>
#include <conio.h>
#define NULL 0
struct Array
{
int x;
int y;
}arr[64],*top,*base;
int n=0;
int a[8][8]={{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,1,1,0,0},{0,1,1,1,0,0,0,0},{0,0,0,1,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,1,1,0},{1,0,0,0,0,0,0,0}};
void MazePath(int i,int j)
{
if(i>=0 && j>=0 && j<8 && i<8 && a[i][j]==0 && Exit(i,j))
{
if(n!=0) top++;
top->x=i;
top->y=j;
n=n+1;
if(!(i==7 && j==7))
MazePath(i,j+1);
}
else
{
if(i==top->x && j-1==top->y)
MazePath(top->x+1,top->y);
else if(i==top->x+1 && j==top->y)
MazePath(top->x,top->y-1);
else if(i==top->x && j+1==top->y)
MazePath(top->x-1,top->y);
else
{
do
{
top--;
if(top->x==(top+1)->x && top->y+1==(top+1)->y)
{
MazePath(top->x+1,top->y);
break;
}
else if(top->x+1==(top+1)->x && top->y==(top+1)->y)
{
MazePath(top->x,top->y-1);
break;
}
else if(top->x==(top+1)->x && top->y-1==(top+1)->y)
{
MazePath(top->x-1,top->y);
break;
}
}while(top!=NULL);
}
}
}
int Exit(int i,int j)
{
struct Array *p;
p=base;
while(p!=top)
{
if(p->x==i && p->y==j)
return(0);
p++;
}
return(1);
}
void main()
{
int b;
clrscr();
top=base=arr;
MazePath(0,0);
while(top!=base)
{
printf("%d,%d/n",top->x,top->y);
top--;
}
printf("%d,%d/n",base->x,base->y);
scanf("%d",&b);
}
相关文章推荐
- (数据结构)栈_迷宫求解(严蔚敏P50) _模仿
- 数据结构和算法设计(迷宫求解问题的栈和队列的实现)
- 数据结构的应用——使用栈实现任意迷宫的求解
- C++栈实现数据结构中的迷宫求解
- 严蔚敏《数据结构》中迷宫算法java实现
- 【数据结构】迷宫问题求解(链栈,DFS)
- JS手撸数据结构系列 (五) ——图的遍历与迷宫求解
- 数据结构迷宫问题C++实现
- 数据结构(严蔚敏)之二——链表的c语言实现
- 迷宫求解——附栈的实现
- 严蔚敏版数据结构学习笔记(1):线性表的顺序表示和实现
- 穷举法迷宫求解简单实现(C)
- java使用链栈实现迷宫求解
- 迷宫求解(数据结构)
- 数据结构严蔚敏版快速排序算法c语言实现
- 数据结构(迷宫求解c++)
- 10、数据结构笔记之十栈的应用之迷宫求解实现
- 数据结构之三元组顺序表实现稀疏矩阵运算(参考整理严蔚敏数据结构)
- PHP实现基于回溯法求解迷宫问题的方法详解
- 利用堆栈实现迷宫问题的求解