凯西男孩
|
分享:
▼
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 |
|
|
|