夷希微
|
分享:
▲
▼
Re:【求助】c++也能写迷宫吗?><"
喔喔~~不小心又路过~~进来看到才知道~~原来这个题目还没解决呀 ^^" 原本这种题目老师出出来~~就不可能只是要您将迷宫地图画出来而已呢~~ :P 小妹一开始不就说了~~迷宫的地图小妹帮您画~~但走迷宫的功能您要自己写的 ^^ 坦白说~~如果小妹直接给您解答的话~~其实是害了您的呢~~所以才要您自己动手的 ^^ 不过看样子~~这个题目您好像是解不出来了~~对吧 ^^" 看在版主说此篇值得进入经华区的份上~~那小妹我就再鸡婆一次好了 ^^ 不过因为小妹还是坚持您要自己动手~~故小妹不依您题目的意思去写~~ 您要 8*8 的~~那小妹就写个 25*25 的范例~~请您自已修改吧 ^_^ 还有~~小妹只给 C 的写法而已~~所以也请您自己改为 C++ 的写法吧(大同小异的) ^^ PS.因为程式很短~~所以小妹我就懒的打注解了~~看不懂的语法请自行查书~嘻 ^^y 自动走迷宫程式码(递回): 复制程式
#include <stdio.h>
#include <conio.h>
#define MAP_StartX 16
#define MAP_StartY 1
#define StartX 1
#define StartY 0
int MAP[25][25]={{1,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,0,0,0,0, 0,0,0,1,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1},
{1,0,1,0,1, 1,1,0,1,0, 1,0,1,0,1, 0,1,1,1,1, 0,1,1,1,1},
{1,0,1,0,1, 0,1,0,1,0, 1,0,1,0,1, 0,0,0,1,1, 0,1,0,0,1},
{1,0,1,0,1, 0,1,0,1,0, 1,0,1,0,1, 1,1,0,0,0, 0,1,1,0,1},
{1,0,1,1,1, 0,1,0,1,0, 1,0,0,0,1, 0,0,0,1,0, 1,1,1,0,1},
{1,0,0,0,0, 0,1,0,1,0, 1,0,1,0,1, 0,1,1,1,0, 0,0,0,0,1},
{1,0,1,1,1, 1,1,0,1,0, 1,0,1,0,0, 0,0,0,1,0, 1,1,1,1,1},
{1,0,0,0,0, 0,0,0,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P_X=0;MAP_X<25;MAP_X++)
{
if(MAP[MAP_Y][MAP_X]==1)
{
gotoxy(MAP_StartX+MAP_X*2,MAP_StartY+MAP_Y);
printf("■");
}
}
}
gotoxy(1,1);printf("Ready...");
gotoxy(MAP_StartX+(StartX*2),MAP_StartY+StartY);
printf("◎");
gotoxy(MAP_StartX+(StartX*2),MAP_StartY+StartY);
getch();
gotoxy(1,1);printf("Running...");
run(StartX,StartY);
gotoxy(1,1);printf("Success!!!");
getch();
}
//---------------------------------------------------------------------------
void delay(int value)
{
int BaseNum=10000; //延迟时间基数,请自行依电脑速度调整
int delaytime=BaseNum*value;
int i;
for(i=0;i<delaytime;i++);
}
//---------------------------------------------------------------------------
int run(int x,int y)
{
if(MAP[y][x]==2)Success=1;
gotoxy(MAP_StartX+(x*2),MAP_StartY+y);
printf("◎");
gotoxy(MAP_StartX+(x*2),MAP_StartY+y);
MAP[y][x]=-1;
delay(1000);
if(Success==1)return 1;
if((Success != 1) && ((MAP[y][x+1] == 0)||(MAP[y][x+1] == 2)))run(x+1,y);
if((Success != 1) && ((MAP[y+1][x] == 0)||(MAP[y+1][x] == 2)))run(x,y+1);
if((Success != 1) && ((MAP[y][x-1] == 0)||(MAP[y][x-1] == 2)))run(x-1,y);
if((Success != 1) && ((MAP[y-1][x] == 0)||(MAP[y-1][x] == 2)))run(x,y-1);
if(Success != 1)MAP[y][x]=0;
return Success;
}
//---------------------------------------------------------------------------
手动走迷宫写法: 复制程式
#include <stdio.h>
#include <conio.h>
#define MAP_StartX 16
#define MAP_StartY 1
#define StartX 1
#define StartY 1
int MAP[25][25]={{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,0,0,0,0, 0,0,0,1,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1},
{1,0,1,0,1, 1,1,0,1,0, 1,0,1,0,1, 0,1,1,1,1, 0,1,1,1,1},
{1,0,1,0,1, 0,1,0,1,0, 1,0,1,0,1, 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,0,1},
{1,0,1,0,1, 0,1,0,1,1, 1,0,1,0,1, 1,1,0,1,0, 1,0,1,0,1},
{1,0,0,0,1, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,1,0, 0,0,1,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,2,1}};
void printxy(int x,int y,char *word);
void main(void)
{
int MAP_X,MAP_Y;
int x,y;
char KeyNum;
clrscr();
for(MAP_Y=0;MAP_Y<25;MAP_Y++)
{
for(MAP_X=0;MAP_X<25;MAP_X++)
{
if(MAP[MAP_Y][MAP_X]==1)
{
gotoxy(MAP_StartX+MAP_X*2,MAP_StartY+MAP_Y);
printf("■");
}
}
}
gotoxy(1,1);printf("Running...");
gotoxy(MAP_StartX+(StartX*2),MAP_StartY+StartY);
printf("◎");
gotoxy(MAP_StartX+(StartX*2),MAP_StartY+StartY);
x=StartX;
y=StartY;
while(KeyNum!=27&&MAP[y][x]!=2)
{
KeyNum=getch();
switch(KeyNum)
{
case 72: //上
if(MAP[y-1][x]!=1)
{
printxy(x,y," ");
y--;
printxy(x,y,"◎");
}
break;
case 80: //下
if(MAP[y+1][x]!=1)
{
printxy(x,y," ");
y++;
printxy(x,y,"◎");
}
break;
case 75: //左
if(MAP[y][x-1]!=1)
{
printxy(x,y," ");
x--;
printxy(x,y,"◎");
}
break;
case 77: //右
if(MAP[y][x+1]!=1)
{
printxy(x,y," ");
x++;
printxy(x,y,"◎");
}
break;
default:
break;
}
}
gotoxy(1,1);printf("Success!!!");
getch();
}
//---------------------------------------------------------------------------
void printxy(int x,int y,char *word)
{
gotoxy(MAP_StartX+(x*2),MAP_StartY+y);
printf("%s",word);
gotoxy(MAP_StartX+(x*2),MAP_StartY+y);
}
//---------------------------------------------------------------------------
加油唷 ^_^
[ 此文章被夷希微在2005-04-04 19:26重新编辑 ]
此文章被评分,最近评分记录财富:50 (by panasonic732) | 理由: 假如能整理发表到范例区更棒...@@a | |
|
|
|