日期:2014-05-20  浏览次数:20646 次

如何减少碰撞机会??
如何减少碰撞机会??
如实现100% 不会碰撞??


Java code
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);
    }
}


------解决方案--------------------
晕,错了。
应该将x[i]与x[j]互换。以保证取过的值在数组的最后,再限制随机数的取值范围,则避免重复。