| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6656 人关注过本帖
标题:今天编了一个走迷宫程序
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:13 
今天编了一个走迷宫程序
#include<stdio.h>//基本库
#include<stdlib.h>//系统库
#include<windows.h>//光标定位、字符颜色函数库
#include<time.h>//时间函数库
#include<conio.h>//键值读入函数库
#define width 40
#define height 40
#define OK 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW 0
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef bool Status;
unsigned char map[]={
0xFF,0xFF,0xB0,0x41,0x97,0x5D,0x94,0x55,0x84,0x05,0xA4,0x77,0xA4,0x41,0xBC,0x41,
0x87,0xEF,0xA0,0x29,0xA7,0xA9,0xA0,0xA9,0xA0,0xA1,0xBF,0xBF,0x80,0x81,0xFF,0xFF,
};
char pel[]="  ☆★●□■";

struct xy
{
    int x;
    int y;
    bool flag;
    bool no_entry;
}zuobiao[256]={0};


typedef struct
 {
     int ord;
     int dir;
     struct xy pos;
}SElemType;
SElemType foot[50]={0};
typedef struct{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

void color(int b)         //颜色函数
{
     HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
     SetConsoleTextAttribute(hConsole,b) ;
 }
void HideCursor()//隐藏光标
{
     HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_CURSOR_INFO cursor_info={1,0};
     SetConsoleCursorInfo(hOut,&cursor_info);
 }

void gotoxy(int x,int y)//设置字符显示位置
{
     HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
     COORD loc={x,y};
     SetConsoleCursorPosition(hOut,loc);
     
 }
 
 void MarkPrint(struct xy curpos)
{
    curpos.no_entry=1;
}

 
void drawmap()
 {//画场景
   
    unsigned char draw;
    system("mode con cols=60 lines=32");
    HideCursor();
    gotoxy(10,5);
    color(14);
    for(int i=0;i<32;i++)
    {
        draw=map[i];
        for(int j=0;j<8;j++)
        {
            zuobiao[i*8+j].x=10+(i%2)*16+j*2;
            zuobiao[i*8+j].y=5+i/2;
            if((draw&0x80)==0x80)
            {
                zuobiao[i*8+j].no_entry=1;
                printf("%c%c",pel[10],pel[11]);
            }
            
            else
            printf("%c%c",pel[8],pel[9]);
            draw<<=1;
            
        }
        if(i%2)
        gotoxy(10,5+(i+1)/2);   
    }
   
 }
 
 void drawstart(struct xy &startpos,struct xy &endpos)
 {
     gotoxy(12,6);
     color(15);
     printf("%c%c",pel[2],pel[3]);
     gotoxy(38,19);
     printf("%c%c",pel[4],pel[5]);
     gotoxy(10,22);
     startpos.x=12,startpos.y=6;
     startpos.flag=0;startpos.no_entry=0;
     endpos.x=38,endpos.y=19;
 }

Status InitStack(SqStack &S)
{
    S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base)exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}

Status Push(SqStack &S,SElemType e)
{
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S.base)exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
    if(S.top==S.base)return ERROR;
    e=*--S.top;
    return OK;
}

Status StackEmpty(SqStack S)
{
    if(S.top==S.base)return TRUE;
    return FALSE;
}

Status Pass(struct xy curpos)
{
    if(curpos.flag==1||curpos.no_entry==1)return FALSE;
    else return TRUE;
}

void FootPrint(struct xy curpos)
{
    zuobiao[(curpos.y-5)*16+(curpos.x-10)/2].no_entry=1;
}

struct xy Nextpos(struct xy curpos,int dir)
{
    struct xy pos;
    if(dir==1)
    {
     pos.x=curpos.x+2;
     pos.y=curpos.y;   
    }
   
    else if(dir==2)
    {
     pos.y=curpos.y+1;
     pos.x=curpos.x;
    }
   
    else if(dir==3)
    {
     pos.x=curpos.x-2;
     pos.y=curpos.y;   
    }
   
    else if(dir==4)
    {
     pos.y=curpos.y-1;
     pos.x=curpos.x;   
    }
    pos.flag=zuobiao[(pos.y-5)*16+(pos.x-10)/2].no_entry;
    pos.no_entry=0;
    return pos;
}

SElemType step(int i,struct xy curpos,int dir)
{
    SElemType e;
    e.dir=dir;
    e.ord=i;
    e.pos.x=curpos.x;
    e.pos.y=curpos.y;
    e.pos.flag=1;
    return e;
}

main()
{
    SqStack S;
    int i=0;
    struct xy startpos,endpos,curpos={0};
    drawmap();
    drawstart(startpos,endpos);
    InitStack(S);   
    curpos=startpos;
    do{
        if(Pass(curpos))
        {
            FootPrint(curpos);
            foot[i]=step(i,curpos,1);
            Push(S,foot[i]);
            if(curpos.x==endpos.x-2&&curpos.y==endpos.y)break;
            curpos=Nextpos(curpos,1);
            i++;
        }
        else{
            if(!StackEmpty(S))
            {
                Pop(S,foot[i]);
                i--;
                while(foot[i].dir==4&&!StackEmpty(S))
                {
                    FootPrint(foot[i].pos);
                    Pop(S,foot[i]);
                    i--;
                    
                }
                if(foot[i].dir<4)
                {
                    foot[i].dir++;
                    Push(S,foot[i]);
                    i++;
                    curpos=Nextpos(foot[i].pos,foot[i].dir);
                }
            }
        }
    }while(!StackEmpty(S));
    gotoxy(10,0);
    printf("按回车键显示走迷宫!");
    while(getchar()!='\n');
    for(i=1;foot[i].ord!=0;i++)
    {
        gotoxy(foot[i].pos.x,foot[i].pos.y);
        color(13);
        printf("%c%c",pel[6],pel[7]);
        Sleep(1000);   
    }
    gotoxy(10,22);
    printf("一共走了%d步!\n",i-1);
}
搜索更多相关主题的帖子: include 
2017-03-14 22:19
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:7 
赞一个~收藏~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-03-14 22:20
校草
Rank: 1
等 级:新手上路
帖 子:50
专家分:7
注 册:2017-3-15
收藏
得分:7 
unsigned char map[]={
0xFF,0xFF,0xB0,0x41,0x97,0x5D,0x94,0x55,0x84,0x05,0xA4,0x77,0xA4,0x41,0xBC,0x41,
0x87,0xEF,0xA0,0x29,0xA7,0xA9,0xA0,0xA9,0xA0,0xA1,0xBF,0xBF,0x80,0x81,0xFF,0xFF,
};

楼主,这些是什么意思啊
2017-03-15 17:08
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
收藏
得分:7 
codebloks编译不了,17个错误。
2017-03-15 17:15
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
以下是引用校草在2017-3-15 17:08:49的发言:

unsigned char map[]={
0xFF,0xFF,0xB0,0x41,0x97,0x5D,0x94,0x55,0x84,0x05,0xA4,0x77,0xA4,0x41,0xBC,0x41,
0x87,0xEF,0xA0,0x29,0xA7,0xA9,0xA0,0xA9,0xA0,0xA1,0xBF,0xBF,0x80,0x81,0xFF,0xFF,
};

楼主,这些是什么意思啊

这是地图,每个数字如0xFF代表8个方块,0xff表示11111111。如果位是1就加打印墙,否则打印空档。
这些数字总共有256个位,代表一个16*16的点阵
这些代码可用汉字取模软件生成。
2017-03-15 17:42
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
回复 4楼 Emotiona
我用的dev c++
2017-03-15 17:42
校草
Rank: 1
等 级:新手上路
帖 子:50
专家分:7
注 册:2017-3-15
收藏
得分:0 
以下是引用ehszt在2017-3-15 17:42:19的发言:

 
这是地图,每个数字如0xFF代表8个方块,0xff表示11111111。如果位是1就加打印墙,否则打印空档。
这些数字总共有256个位,代表一个16*16的点阵
这些代码可用汉字取模软件生成。
程序代码:
const long CRU_1901_2012MISC_NRECORD=59191;
const int CRU_1901_2012MISC_DATA_LENGTH=12604;
const int CRU_1901_2012MISC_INDEX_LENGTH=7;
const int CRU_1901_2012MISC_HEADERSIZE=650;
unsigned char CRU_1901_2012MISC_HEADER[CRU_1901_2012MISC_HEADERSIZE-4]={
    0x01,0x02,0x8A,0x00,0x00,0x00,0x07,0x00,0x02,0x04,0x6C,0x6F,0x6E,0x00,0x2D,0x31,0x38,0x30,0x30,0x00,
    0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x38,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x00,
    0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
    0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x04,0x6C,0x61,0x74,0x00,0x2D,0x39,0x30,0x30,
    0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x39,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x31,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,
    0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0B,0x00,0x00,0x31,0x3C,0x00,0x04,0x05,
    0x6D,0x64,0x74,0x72,0x00,0x2D,0x31,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
    0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x65,0x2B,
    0x30,0x36,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
    0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x2E,0x30,0x31,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x1B,0x00,0x00,0x05,0x40,0x05,0x6D,0x77,0x65,0x74,0x00,0x2D,0x31,0x30,0x30,0x30,0x30,0x00,0x00,
    0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x31,0x30,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
    0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x2E,0x30,0x31,
    0x00,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x05,0x40,0x05,0x6D,0x66,0x72,0x73,0x00,0x2D,
    0x31,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x30,0x30,0x30,0x30,0x30,0x00,0x00,0x00,
    0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x30,0x2E,0x30,0x31,0x00,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x05,0x40,
    0x04,0x65,0x6C,0x76,0x00,0x2D,0x31,0x30,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
    0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x30,0x30,
    0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
    0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x2E,0x30,0x30,0x31,0x00,0x00,0x00,0x00,0x00,0x01,
    0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x1B,0x00,0x00,0x00,0x01};

那这里的这些16进制跟你那些一样吗?在读别人写的程序。。还请赐教
2017-03-15 21:04
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
回复 7楼 校草
不知道哦,那要看你程序的用途了。见过乐谱,图像,通讯协议CRC值都这样表示。
2017-03-15 21:13
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
回复 8楼 ehszt
这段是俄罗斯方块的部分代码~~~里面的方块存放位置也是用16进制表示的~

程序代码:
struct BLOCK   //保存俄罗斯方块
{
    //上色1  没有就是0  16二进制
    //变形   4种变形(重复)
    WORD dir[4];  //保存方案
    COLORREF color;  //颜色
}g_Bldcks[7]=
{
    {0xC600,0x4C80,0xC600,0x4C80,GREEN},  //z  1100 0110 0000 0000
    {0x0660,0x0660,0x0660,0x0660,BLUE},  //
    {0x4460,0x02E0,0x0622,0x0740,MAGENTA},  //
    {0x2260,0x0E20,0x0644,0x0470,YELLOW},  //
    {0x0C60,0x2640,0x0c60,0x2640,CYAN},  //
    {0x0360,0x4620,0x0360,0x4620,GREEN},
    {0x4E00,0x4C40,0x0E40,0x4640,BROWN},
};

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-03-15 21:53
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
回复 9楼 九转星河
应该是方块的四种状态吧,其实就是一个由点阵构成的图形。汉字,字符也是由点阵构成的。
2017-03-15 22:04
快速回复:今天编了一个走迷宫程序
数据加载中...
 
   



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

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