這是一個隨機漫步的程式,請幫我看看哪裡有問題,
它跑不出結果啊!! #include
#include
#include
#include
#include
#include
void main()
{
int ibug=-1,jbug=-1,imove[8],jmove[8],m=-1,n=-1,map[40][20];
int count=0,x,y,stop=0,a=-1,r;
imove[0]=-1;jmove[0]=1;
imove[1]=0;jmove[1]=1;
imove[2]=1;jmove[2]=1;
imove[3]=1;jmove[3]=0;
imove[4]=1;jmove[4]=-1;
imove[5]=0;jmove[5]=-1;
imove[6]=-1;jmove[6]=-1;
imove[7]=-1;jmove[7]=0;
srand(time(0));
std::cout << "現在有塊nxm的磁磚,";
while(n<=2 || n>40)
{
std::cout << "n值需界於3~40!\n請輸入n值:";
std::cin >> n;
}
std::cout << "\n請輸入m值,";
while(m<=2 || m>20)
{
std::cout << "m值需界於3~20!\n請輸入m值:";
std::cin >> m;
}
std::cout << "\n有一隻蟑螂在磁磚上,請輸入它現在的x座標\n";
std::cout << "x須大於零,且不大於"<< n;
while(ibug<=0||ibug>n)
{
std::cout << "請輸入x值:";
std::cin >> ibug;
}
std::cout << "\n請輸入它現在的y座標\n";
std::cout << "y須大於零,且不大於"<< m;
while(jbug<=0||jbug>n)
{
std::cout << "請輸入x值:";
std::cin >> jbug;
}
for(x=0;x<=n;x )
{
for(y=0;y<=m;y )
{
map[x][y]=0;
}
}
map[ibug][jbug] ;
count=1;
while(count<50000 || stop==1)
{
a=-1;
while(a<=0)
{
r=rand()%8;
if(ibug imove[r]<=n && ibug imove[r]>=0 && jbug jmove[r]<=m && jbug jmove[r]>=0)
{
ibug=ibug imove[r];
jbug=jbug jmove[r];
a=1;
map[ibug][jbug] ;
}
}
count ;
stop=1;
for(x=0;x<=n;x )
{
for(y=0;y<=m;y )
{
if(map[x][y]==0)
stop=0;
}
}
}
std::cout<<"執行結束,蟑螂走了" <