夷希微
|
分享:
▲
▼
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,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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("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,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0,0,0,1,0, 1,0,1,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 | |
|
|
|