如何减少碰撞机会??

angus203 2008-05-22 04:19:41
如何减少碰撞机会??
如实现100% 不会碰撞??


import java.util.*;

class t1
{
public static void main(String[] args)
{
Object[][][] ojb1 = new Object[10][5][9];
Object[][][] ojb2 = new Object[7][5][9];
Object[][][] ojb3 = new Object[6][5][9];
int total = 0;
int count = 0;

for(int i=0; i<104; i++ ){

while(count < 100){
int x = (int) (Math.random() * 5);
int y = (int) (Math.random() * 9);
int z = (int) (Math.random() * 10);
int m = (int) (Math.random() * 7);
int n = (int) (Math.random() * 6);

if(ojb1[z][x][y] == null && ojb1[m][x][y] == null && ojb3[n][x][y] == null){
ojb1[z][x][y] = new String("No Empty");
ojb1[m][x][y] = new String("No Empty");
ojb1[n][x][y] = new String("No Empty");
System.out.println("Added " + i);
break;
}else{
count+=1;
}
}


if(count >=100){
System.out.println("Conflict " + i);
total +=1;
}

}

System.out.println("Total of conflict: " + total);
}
}
...全文
65 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyberpeng 2008-05-22
  • 打赏
  • 举报
回复
晕,错了。
应该将x[i]与x[j]互换。以保证取过的值在数组的最后,再限制随机数的取值范围,则避免重复。
cyberpeng 2008-05-22
  • 打赏
  • 举报
回复
给你个思路吧。
在a, b, c, d的范围内随即取值,不能重复(碰撞?)。
x[0]=a
x[1]=b
x[2]=c
x[3]=d

Random rand = new Random();
int temp;
for (int j = 4; j > 0; j--) {
int i = rand.nextInt(j);
System.out.println(x[i]);
temp = x[i];
x[i] = x[4 - j];
x[4 - j] = temp;
}

大意就是这样
cuijie_cn 2008-05-22
  • 打赏
  • 举报
回复
不太好控制吧
hongke1490 2008-05-22
  • 打赏
  • 举报
回复
我这里没有碰撞,代码有问题:
if(ojb1[z][x][y] == null && ojb1[m][x][y] == null && ojb3[n][x][y] == null)
的第2个判断应该是ojb2[m][x][y] == null
angus203 2008-05-22
  • 打赏
  • 举报
回复
Random 的数字, 我希望做到好像有一幅地图一样….知道哪个位置已经有对象..不会撞向它
angus203 2008-05-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 java2000_net 的回复:]
random 不保证这个,毕竟是随机的。呵呵!
[/Quote]

如何改善random ?
angus203 2008-05-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cyberpeng 的回复:]
能不能把问题描述的具体一点。一个标题,一大段代码……
[/Quote]

Object[][][] ojb1 = new Object[n][5][9];
Object[][][] ojb1 = new Object[x][5][9];
Object[][][] ojb1 = new Object[y][5][9];

3 者位置[5][9]同时都是 null..就return true..

例如:
ojb1 [1][2][3] = null
obj2 [5][2][3] = null
obj3 [3][2][3] = null
这时就 return true;

当3个ojbect 一边存入数据..一边计算剩下多少个同时共有的空位..(希望这个)
老紫竹 2008-05-22
  • 打赏
  • 举报
回复
random 不保证这个,毕竟是随机的。呵呵!
cyberpeng 2008-05-22
  • 打赏
  • 举报
回复
能不能把问题描述的具体一点。一个标题,一大段代码……

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧