走过了的点不能在走,求走1000步后的轨迹。
又会的麻烦帮帮忙!!谢谢!
我自己编了一下,不会显示轨迹,还有随机函数也不知道对不对,请高人指点指点:
#include<iostream.h>
void main()
{ int x=0,y=0,i,c;
float t;
int *p;
int a[1000];
*p=&a;
for(i=0;i<=100;i++)
{ t=rand();
if(0<t<=0.25)
{ y++;
c=cmp(x,y);
if(c)
{ a[i]={(x,y)};
p++;
}
}
if(0<t<=0.5)
{ x++;
c=cmp(x,y);
if(c)
{ a[i]={(x,y)};
p++;
}
}
if(0.5<t<=0.75)
{ y--;
c=cmp(x,y);
if(c)
{ a[i]={(x,y)};
p++;
}
}
if(0.75<t<=1.0)
{ x--;
c=cmp(x,y);
if(c)
{ a[i]={(x,y)};
p++;
}
}
}
}
int cmp(j,k)
{ int j,k;
strcmp( (j,k),*p);
}
随便写了一个垃圾的, 你参考看考
import java.math.BigDecimal;
class DrinkedWalker {
static void walk(int[][] road) {
int x = 0, y = 0;
for (int i = 0; i < 1000; i++) {
double randomNum = Math.random();
BigDecimal b = new BigDecimal(randomNum);
float roundNum = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
String strDirect = Long.toString(Math.round(roundNum / 0.25));
int direction = Integer.parseInt(strDirect);
int tmpX = x, tmpY = y;
switch(direction) {
case 0:
case 1:
x = x - 1;
break;
case 2:
y = y + 1;
break;
case 3:
x = x + 1;
break;
default:
y = y - 1;
}
if (x >= 0 && y >= 0 && road[x][y] != 1) {
road[x][y] = 1;
} else {
System.out.println("Stop at x=" + tmpX + "; y=" + tmpY);
return;
}
}
System.out.println("Stop at x=" + x + "; y=" + y);
return;
}
public static void main(String[] args) {
int[][] road = new int[100][100];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
road[i][j] = 0;
}
}
walk(road);
}
}
请问高手用那种语言编的?Java?大概能看懂,只是有些命令语句没见过。