| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 996 人关注过本帖
标题:关于迷宫的算法求解问题
只看楼主 加入收藏
qq8801103
Rank: 5Rank: 5
来 自:苏州中科大软件学院
等 级:职业侠客
威 望:1
帖 子:422
专家分:340
注 册:2009-10-8
结帖率:73.96%
收藏
已结贴  问题点数:10 回复次数:3 
关于迷宫的算法求解问题
用c语言写迷宫算法 谢谢
搜索更多相关主题的帖子: 迷宫 算法 求解 
2010-04-01 22:07
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:3 
#include "stdio.h"
#include <Windows.h>
#include "conio.h"
 int main()
 {
int i,j,temp; char s;int x=22,y=23;
int a[24][25]={
 1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,
 1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,
 1,0,1,0,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,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,
 1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,
 1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,
 1,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,
 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,
 1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,
 1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,
 1,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,
 1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,
 1,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,
 1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1,
 1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,
 1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,0,1,
 1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,
 1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,
 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
 1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,
 1,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,
 1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,
 1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,
 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
 } ;
 temp=a[x][y];
 a[x][y]=2;
for (i=0;i<24;i++)
 {
 for (j=0;j<25;j++)
 printf("%c",a[i][j]);
 printf("\n");
 }
 a[x][y]=temp;
 loop: s=getch();
while (s!='0')
{
 switch (s)
 {
 case 'a': if (a[x][y-1]==0) y=y-1;else goto loop ;break;
 case 'd': if (a[x][y+1]==0) y=y+1;else goto loop ;break;
 case 'w': if (a[x-1][y]==0 ) x=x-1;else goto loop ;break;
 case 's': if (a[x+1][y]==0 ) x=x+1;else goto loop ;break;
 default :goto loop;
 }
 //system("cls");
 temp=a[x][y];
 a[x][y]=2;
 if (a[0][1]==2) {printf("great job!\n"); getch(); break;}
 for (i=0;i<24;i++)
 {
 for (j=0;j<25;j++)
 printf("%c",a[i][j]);
 printf("\n");
 }
 a[x][y]=temp;
 s=getch();
}

 }
2010-04-02 08:12
xichong
Rank: 7Rank: 7Rank: 7
来 自:四川南充
等 级:黑侠
威 望:2
帖 子:146
专家分:582
注 册:2009-6-10
收藏
得分:4 
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10000
#define STACKINCREMENT 100
#define M 10
#define N 10
int flag=0;
typedef struct//坐标位置
{
    int x;
    int y;
}PosType;
typedef struct//栈中元素结构体
{
    int ord;//序号
    PosType seat;//坐标位置
    int di;//通道方向(1东2南3西4北)
}SElemType;
typedef struct//栈
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;
////////////////////////////////////////栈操作
void initialStack(SqStack *s)
{
    s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!s->base) exit(0);
    s->top=s->base;
    s->stacksize=STACK_INIT_SIZE;
}
void Push(SqStack *s,SElemType e)
{
    if(s->top-s->base>=s->stacksize)
    {
        s->base=(SElemType *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));
        if(!s->base) exit(0);
        s->top=s->base+s->stacksize;
        s->stacksize+=STACKINCREMENT;
    }
    (*(s->top)).ord=e.ord;
    (*(s->top)).seat.x=e.seat.x;
    (*(s->top)).seat.y=e.seat.y;
    (*(s->top)).di=e.di;
    s->top++;
}
void Pop(SqStack *s,SElemType *e)
{
    if(s->top==s->base) exit(0);
    (s->top)--;
    e->ord=(*(s->top)).ord;
    e->seat.x=(*(s->top)).seat.x;
    e->seat.y=(*(s->top)).seat.y;
    e->di=(*(s->top)).di;
}
int StackEmpty(SqStack *s)
{
    if(s->top==s->base) return(1);
    else return(0);
}
void ClearStack(SqStack *s)
{
    s->top=s->base;
    s->stacksize=0;
}
//////////////////////////////////////////初始化迷宫
void initialarray(int (*p)[N])
{
    int i,j;
    int maze[M][N]={1,1,1,1,1,1,1,1,1,1,
                    1,0,0,1,0,0,0,1,0,1,
                    1,0,0,0,0,1,0,1,0,1,
                    1,0,0,1,1,0,0,0,0,1,
                    1,1,1,1,1,0,0,1,0,1,
                    1,0,0,0,1,0,0,0,0,1,
                    1,0,1,0,0,1,1,0,0,1,
                    1,0,1,1,1,0,1,1,0,1,
                    1,1,0,0,0,0,0,0,0,1,
                    1,1,1,1,1,1,1,1,1,1};/***/
    printf("打印迷宫:\n");
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d",maze[i][j]);
            *(*(p+i)+j)=maze[i][j];/***/
        }
        printf("\n");
    }
}
///////////
void NextPos(PosType *p,int d)//确定下一个位置的坐标
{
    switch(d)
    {
    case 1:p->y++;break;
    case 2:p->x++;break;
    case 3:p->y--;break;
    case 4:p->x--;break;
    }    /////////////////////////(1东2南3西4北)改变坐标
}
void MazePath(int (*maze)[N])//核心函数//---int (*maze)[N]
{   
    int curstep;
    int mark[M][N]={0};
    SqStack s;
    PosType curpos;
    SElemType e;//栈中元素结构体 1.序号;2.坐标位置;3.通道方向
    initialStack(&s);
    curpos.x=1;
    curpos.y=1;
    curstep=1;
    do
    {
        if((!maze[curpos.x][curpos.y])&&(!mark[curpos.x][curpos.y]))//*******
        {
            mark[curpos.x][curpos.y]=1;
            e.ord=curstep;//**e=(curstep,curpos,1)
            e.seat.x=curpos.x;
            e.seat.y=curpos.y;
            e.di=1;//***
            Push(&s,e);
            if((curpos.x==M-2)&&(curpos.y==N-2))
            {
               
                flag=1;
                printf("走出迷宫的一条坐标路径为:\n[终点]");
                while(!StackEmpty(&s))
                {
                    Pop(&s,&e);
                    printf("<--(%d,%d)",e.seat.x,e.seat.y);
                }
                printf("<--[起点]");
                printf("\n");
                ClearStack(&s);
            }
                NextPos(&curpos,1);//curpos=NextPos(curpos,1)
                curstep++;
        }
            else
            {
                if(!StackEmpty(&s))
                    Pop(&s,&e);
                while(e.di==4&&!StackEmpty(&s))
                {
                    mark[e.seat.x][e.seat.y]=1;
                    Pop(&s,&e);
                }
                if(e.di<4)
                {
                    e.di++;
                    Push(&s,e);
                    curpos.x=e.seat.x;//**curpos=NextPos(e.seat,e,di)
                    curpos.y=e.seat.y;
                    NextPos(&curpos,e.di);///**
                }
            }
    }while(!StackEmpty(&s));
}
void main()
{
    int array[M][N];
    initialarray(array);
    MazePath(array);
    if(flag==0)
        printf("该迷宫不能走出去!\n");
}
2010-04-03 10:16
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:3 
贴错了
2010-04-03 13:57
快速回复:关于迷宫的算法求解问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023661 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved