凱西男孩
|
分享:
▼
x0
|
[C/C++] 求助【幫改程式碼】
這是一個迷宮的程式,8*8的,大致上就是這樣,我就是改不好,好心的大大幫個忙吧!謝謝,感激不盡。 #include <iostream.h> #include <stdio.h> #include <stdlib.h> int visit(int, int); int maze={{1,0,1,1,1,1,1,1}, {0,0,1,0,1,0,0,1}, {1,0,1,0,1,0,1,1}, {1,0,1,0,0,0,0,1}, {1,0,0,0,1,1,0,1}, {1,0,1,0,0,1,0,1}, {1,0,1,0,1,1,0,1}, {1,1,1,1,1,1,2,1}}; int ei = 6, ej = 6; int success = 0; int main(void) { int i, j; do{ cout << "請輸入x座標(0-7):" << endl; cin >> i; .. 訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容
|
|
x0
[樓 主]
From:台灣中華電信
| Posted:2005-04-18 18:13 |
|
|
s89tsvs62219
|
分享:
▲
▼
這是小弟做過的老鼠走迷宮~~看一下對你有沒有幫助~ 複製程式
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<fstream.h>
//class datalist;
#define fast 200
#define filepath "c:\\迷宮.txt"
int a[12][12]={
{1,1,1,1,1,1,1,1,1,1,1,1},
{99,0,0,1,0,1,0,0,0,0,0,1},
{1,1,0,1,0,1,0,1,1,1,0,1},
{1,1,0,1,0,0,0,1,0,0,0,1},
{1,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,1,0,0,0,1,0,0,0,1},
{1,1,0,1,0,1,0,1,1,1,0,1},
{1,1,0,0,0,0,0,1,0,0,0,1},
{1,1,0,1,1,1,1,1,0,1,0,1},
{1,1,0,1,1,1,1,1,0,1,0,1},
{1,1,0,0,0,0,0,1,0,1,0,100},
{1,1,1,1,1,1,1,1,1,1,1,1},};
void picture()
{
for (int i=0;i<=11;i++)
{for (int j=0;j<=11;j++)
{if (a[ i ][j]==1)
cout << "■";
else
if (a[ i ][j]==0)
cout << " ";
else
if (a[i][j]==99)
cout<< "@";
else
cout<<"╳";
}
cout <<endl;
}
}
class datalist
{
public:
datalist *next;
int x,y,direct;
datalist(int xx,int yy,int dd,datalist *hh)
{x=xx;y=yy;direct=dd;
next=hh;
}
~datalist()
{
// cout<< "\n 解構子" <<endl;
// system("pause");
}
};
void printdata(datalist *head)
{
while(head != '\0')
{
cout << "x="<<head->x<< " y="<<head->y<< " direct="<<head->direct<<endl;
head=head->next;
//system("pause");
}
}
datalist* comeback(datalist *head)
{
int ck;
datalist *temp;
do{
a[head->y][head->x]=0;
temp=head;
head=head->next;
delete temp;
a[head->y][head->x]=99;
system("CLS");
picture();
switch(head->direct)
{//1上 2下 4左 8右
case 1:
head->direct =head->direct-1;
break;
case 2:
case 3:
case 6:
case 7:
head->direct =head->direct-2;
break;
case 4:
case 5:
head->direct =head->direct-4;
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
head->direct =head->direct-8;
break;
default:
cout<< "方向有錯"<<endl;
}
ck=clock();
while((clock()-ck)<fast)
{
}
}while(head->direct==0);
return head;
}
datalist* checkwell(datalist *head,int x, int y)
{
if (head !='\0')
if (head->x == x && head->y==y)
return head;
else
return checkwell(head->next ,x,y);
else
return '\0'
}
void main()
{
fstream file;
int x=0,y=1,direct=8,ck;
datalist *head='\0',*wellhead='\0'
head=new datalist(x,y,direct,head);
/*
//============================
file.open(filepath,ios::in);
if (!file)
{
cerr<<"XXXXX"<<endl;
exit(1);
}
for (int i=0;i<12;i++)
for (int j=0;j<12;j++)
{
file>> ck;
a[i][j]=ck;
}
file.close();
//============================
*/
picture();
system("pause");
//繪地圖
while(a[y][x]!=100)
{
ck=clock();
while((clock()-ck)<fast)
{
}
if (head->direct==0)
head=comeback(head);
switch(head->direct)
{//1上 2下 4左 8右
case 1:
x=head->x;
y=head->y-1;
break;
case 2:
case 3:
case 6:
case 7:
x=head->x;
y=head->y+1;
break;
case 4:
case 5:
x=head->x-1;
y=head->y;
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
x=head->x+1;
y=head->y;
break;
default:
cout<< "方向有錯"<<endl;
}
//判斷方向可走
direct=0;
if(head->direct !=4)
if (a[y][x+1]==0 || a[y][x+1]==100)
{wellhead=checkwell(head,x+1,y);
if (wellhead=='\0')
direct=direct+8;
else
wellhead->direct =wellhead->direct-4;
}
if(head->direct !=8)
if (a[y][x-1]==0 || a[y][x-1]==100)
{wellhead=checkwell(head,x-1,y);
if (wellhead=='\0')
direct=direct+4;
else
wellhead->direct =wellhead->direct-8;
}
if(head->direct !=1)
if (a[y+1][x]==0 || a[y+1][x]==100)
{wellhead=checkwell(head,x,y+1);
if (wellhead=='\0')
direct=direct+2;
else
wellhead->direct =wellhead->direct-1;
}
if(head->direct !=2)
if (a[y-1][x]==0 || a[y-1][x]==100)
{wellhead=checkwell(head,x,y-1);
if (wellhead=='\0')
direct=direct+1;
else
wellhead->direct =wellhead->direct-2;
}
a[head->y][head->x]=0;
if (a[y][x]!=100) a[y][x]=99;
head=new datalist(x,y,direct,head);
system("CLS");
picture();
}
//file.open(filepath,ios::out);
//if (!file)
//{
// cerr<<"XXXXX"<<endl;
// exit(1);
//}
//for(y=0;y<12;y++)
//{for(x=0;x<12;x++)
// file<<a[y][x]<< " ";
//file<<endl;}
//file.close();
while(head->next !='\0')
{
a[head->y][head->x]=99;
head=head->next;
}
picture();
//cout<<"走出了"<<endl;
}
[ 此文章被s89tsvs62219在2005-04-20 00:40重新編輯 ]
|
|
x0
[1 樓]
From:台灣 台北市 | Posted:2005-04-18 20:15 |
|
|
|