Java 访问二维阵列的每个单元需要多少步骤?

Java 访问二维阵列的每个单元需要多少步骤?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有以下问题,我自己无法解决 有一个8x8维的二维阵列。一只狗被随机放在这个维度内。狗不允许走出维度,如果它这样做了,这些步骤不被计算在内。狗走到另一个广场的每一步都被计算在内。每次模拟应计算狗至少一次访问8x8尺寸的所有正方形所需的总步数 我如何知道是否所有64个正方形都至少有一次步进以停止模拟并打印步进 int [] [] onSquare = new int [8][8]; int counter = 0; int x = rnd.nextInt(8); // random positi

我有以下问题,我自己无法解决

有一个8x8维的二维阵列。一只狗被随机放在这个维度内。狗不允许走出维度,如果它这样做了,这些步骤不被计算在内。狗走到另一个广场的每一步都被计算在内。每次模拟应计算狗至少一次访问8x8尺寸的所有正方形所需的总步数

我如何知道是否所有64个正方形都至少有一次步进以停止模拟并打印步进

int [] [] onSquare = new int [8][8];
int counter = 0;

int x = rnd.nextInt(8); // random position for the dog 0-7
int y = rnd.nextInt(8);


for (int i = 0; i < onSquare.length; i++) {
    for (int j = 0; j < onSquare.length; j++) {
        onSquare [i] [j] = -1;    // dog is on no square in beginning
    }
while (onSquare[i][j] == -1) {
    int dog = rnd.nextInt(4)+1;    // random movement from dog

    if (dog == 1) {
      x++; // x,y show position of the dog
      counter++; // number of steps
      if (onSquare[i][j] == onSquare[x][y]) { <---- This gives me errors when checking if x,y lies within i,j
         onSquare[i][j] = 1; // stepped on square
      }

    }
    if (dog == 2) {
      x--;
      counter++;
      onSquare[i][j] = 1;
    }
    if (dog == 3) {
      y++;
      counter++;
      onSquare[i][j] = 1;
    }
    if (dog == 4) {
      y--;
      counter++;
      onSquare[i][j] = 1;
    }
    if (x < 0 || y < 0 || x > onSquare.length || y > onSquare.length) {
      counter++;
      onSquare[i][j] = 0;
    }
}
}
int[]onSquare=newint[8][8];
int计数器=0;
int x=rnd.nextInt(8);//狗的随机位置为0-7
int y=rnd.nextInt(8);
对于(int i=0;ionSquare.length){
计数器++;
方[i][j]=0;
}
}
}

我将把这篇文章作为一系列建议来写,否则我会觉得我基本上是在为你写作业

听起来您应该去掉
维度
,因为它没有做任何有用的事情

您应该将值
8
存储在变量中

int size = 8;
这听起来像是你要检查所有的广场何时都被参观过,还要检查采取了多少步骤,包括重新参观旧广场。为此,你需要两个计数器,而不是一个

int steps = 0;
int visited = 0;
您需要进行边界检查,以确保
x
y
不会超出边界

if (dog == 1 && x < size - 1) {
   x += 1;
   steps += 1;
当你访问过每个地方时,你需要停下来

while (visited < size * size) {
while(访问
使用步数计数器跟踪移动,对于每个移动,增加其值

不要使用布尔数组,而是使用int数组跟踪遍历的单元格

int [] [] onSquare = new int [8] [8];
将所有单元格初始化为-1,表示狗尚未移动到单元格

for(int i = 0 ; i < 8; i++ )
    for(int j = 0; j < 8 ; j++ )
        onSquare[i][j]=-1;
for(int i=0;i<8;i++)
对于(int j=0;j<8;j++)
方[i][j]=-1;
当狗进入时,将其值指定为1,表示狗的存在

当狗退出时,将其值指定为0,表示没有狗

一旦所有单元格都具有非负值,停止模拟并显示步长计数器的值

编辑:由于您正在努力解决问题,下面是完整的代码:

    /**
 * This simulation assumes dog movement is discrete relative to grid cells
 * i.e. its either in one of these cells at a time, overlapping two cells in not allowed!!
 * **/
public class DogMovementSimulation 
{
    int onBoard[][] = null;
    int dogPosX = 0;
    int dogPosY = 0;
    int dogPrevPosX = 0;
    int dogPrevPosY = 0;

    int directionOfMovement = 0;
    int stepsCount = 0;

    DogMovementSimulation()
    {
        onBoard = new int[8][8];
        //initialize each position in onBoard to -1 ,implying dog has not been placed yet, not even once!!
        for( int i = 0 ; i < 8 ; i++ )
        {
            for( int j = 0 ; j < 8 ; j++ )
            {
                onBoard[i][j] = -1;//implying dog has not been placed yet, not even once!!
            }
        }

        //place dog in random cell
        dogPosX = (int)Math.round(Math.random()*7);//generating random number between 0 and 7, since index is from 0 to 7 as there are 8 cell!!
        dogPosY = (int)Math.round(Math.random()*7);
        //assigning 1 to onBoard at index dogPosX,dogPosY to indicate dog has been placed there
        onBoard[dogPosX][dogPosY] = 1;
    }

    /*this function returns false if any cell has -1,else true
     * cause when all cells have been traversed , each cell have non negative value,either 0 or 1 
     *  */
    public boolean areAllCellsTraversed()
    {
        boolean result = true;

        for( int i = 0 ; i < 8 ; i++ )
        {
            for( int j = 0 ; j < 8 ; j++ )
            {
                if( onBoard[i][j] == -1 )//implying this cell not traversed yet,i.e dog not placed in this cell yet!!
                {
                    result = false;
                }
            }
        }
        return result;
    }

    public void simulate()
    {
        //loop while all cells have been not traversed
        while( !areAllCellsTraversed() )
        {
            directionOfMovement = (int)Math.round(Math.random()*3);//generating random number between 0 and 3
            switch( directionOfMovement )
            {
            case 0://move left-to-right
                dogPosX += 1;
                if( dogPosX >= 7 ) dogPosX = 0; //since largest array index is 1 less than its size, we compare with 7 instead of 8                 
                break;

            case 1://move right-to-left
                dogPosX -= 1;
                if( dogPosX <= 0 ) dogPosX = 7;                 
                break;

            case 2://move top-to-bottom
                dogPosY += 1;
                if( dogPosY >= 7 ) dogPosY = 0;                 
                break;

            case 3://move bottom-to-top
                dogPosY -= 1;
                if( dogPosY <= 0 ) dogPosY = 7;                 
                break;
            }

            //assign 0 to previous position, meaning dog is no longer there
            onBoard[dogPrevPosX][dogPrevPosY] = 0;
            //assign 1 to new position , meaning dog is here
            onBoard[dogPosX][dogPosY] = 1;

            stepsCount++;
            dogPrevPosX = dogPosX;
            dogPrevPosY = dogPosY;                  
        }   
        //once all cells have been traversed , print result!!
        printSteps();
    }   

    /*prints the total number of step taken to traverse all cells*/
    public void printSteps()
    {
        System.out.println("Total steps taken by dog to traverse all cell = "+stepsCount);
    }

    public static void main(String[] args)
    {
        DogMovementSimulation dms = new DogMovementSimulation();
        dms.simulate();     
    }
}
/**
*该模拟假设狗的运动相对于网格单元是离散的
*也就是说,它一次在其中一个单元格中,不允许重叠两个单元格!!
* **/
公共类DogMovementSimulation
{
int车载[][]=null;
int dogPosX=0;
int-dogPosY=0;
int-dogprovosx=0;
int-dogprovosy=0;
int directionOfMovement=0;
int-stepsunt=0;
DogMovementSimulation()
{
机载=新整数[8][8];
//将船上的每个位置初始化为-1,这意味着狗还没有被放置,甚至一次也没有!!
对于(int i=0;i<8;i++)
{
对于(int j=0;j<8;j++)
{
船上[i][j]=-1;//暗示狗还没有被安置,甚至一次也没有!!
}
}
//把狗放在随机的牢房里
dogPosX=(int)Math.round(Math.random()*7);//生成0到7之间的随机数,因为索引是从0到7的,因为有8个单元格!!
dogPosY=(int)Math.round(Math.random()*7);
//在索引dogPosX处向船上分配1,dogPosY表示狗已经被放置在那里
车载[dogPosX][dogPosY]=1;
}
/*如果任何单元格具有-1,则此函数返回false,否则返回true
*原因当遍历所有单元格时,每个单元格都有非负值,0或1
*  */
公共布尔值areAllCellsTraversed()
{
布尔结果=真;
对于(int i=0;i<8;i++)
{
对于(int j=0;j<8;j++)
{
if(board[i][j]==-1)//表示此单元格尚未被遍历,即狗尚未放置在此单元格中!!
{
结果=假;
}
}
}
返回结果;
}
公共空间模拟()
{
//未遍历所有单元格时循环
而(!areAllCellsTraversed())
{
directionOfMovement=(int)Math.round(Math.random()*3);//生成0到3之间的随机数
开关(方向移动)
{
案例0://从左向右移动
dogPosX+=1;
如果(dogPosX>=7)dogPosX=0;//由于最大数组索引比其大小小1,我们将与7而不是8进行比较
打破
案例1://从右向左移动
dogPosX-=1;
如果(dogPosX=7)dogPosY=0;
打破
案例3://从下到上移动
dogPosY-=1;

如果(dogPosY)“维度”是什么意思?为什么要在列中初始化?你需要添加边界检查。这是家庭作业吗?似乎这是他们给你的一个起点,你必须填写缺失的部分或其他内容?@dave cousineau dimension表示可用于单步执行的方块数嗯,不确定,但听起来你不需要<代码>维度
数组。
onSquare
数组足以跟踪狗的位置,x和y足以跟踪狗现在的位置,8足以跟踪区域的边界。“访问2d数组的每个单元格需要多少步?”介于63和85之间∞. --- 无论起始位置如何,都有一个
    /**
 * This simulation assumes dog movement is discrete relative to grid cells
 * i.e. its either in one of these cells at a time, overlapping two cells in not allowed!!
 * **/
public class DogMovementSimulation 
{
    int onBoard[][] = null;
    int dogPosX = 0;
    int dogPosY = 0;
    int dogPrevPosX = 0;
    int dogPrevPosY = 0;

    int directionOfMovement = 0;
    int stepsCount = 0;

    DogMovementSimulation()
    {
        onBoard = new int[8][8];
        //initialize each position in onBoard to -1 ,implying dog has not been placed yet, not even once!!
        for( int i = 0 ; i < 8 ; i++ )
        {
            for( int j = 0 ; j < 8 ; j++ )
            {
                onBoard[i][j] = -1;//implying dog has not been placed yet, not even once!!
            }
        }

        //place dog in random cell
        dogPosX = (int)Math.round(Math.random()*7);//generating random number between 0 and 7, since index is from 0 to 7 as there are 8 cell!!
        dogPosY = (int)Math.round(Math.random()*7);
        //assigning 1 to onBoard at index dogPosX,dogPosY to indicate dog has been placed there
        onBoard[dogPosX][dogPosY] = 1;
    }

    /*this function returns false if any cell has -1,else true
     * cause when all cells have been traversed , each cell have non negative value,either 0 or 1 
     *  */
    public boolean areAllCellsTraversed()
    {
        boolean result = true;

        for( int i = 0 ; i < 8 ; i++ )
        {
            for( int j = 0 ; j < 8 ; j++ )
            {
                if( onBoard[i][j] == -1 )//implying this cell not traversed yet,i.e dog not placed in this cell yet!!
                {
                    result = false;
                }
            }
        }
        return result;
    }

    public void simulate()
    {
        //loop while all cells have been not traversed
        while( !areAllCellsTraversed() )
        {
            directionOfMovement = (int)Math.round(Math.random()*3);//generating random number between 0 and 3
            switch( directionOfMovement )
            {
            case 0://move left-to-right
                dogPosX += 1;
                if( dogPosX >= 7 ) dogPosX = 0; //since largest array index is 1 less than its size, we compare with 7 instead of 8                 
                break;

            case 1://move right-to-left
                dogPosX -= 1;
                if( dogPosX <= 0 ) dogPosX = 7;                 
                break;

            case 2://move top-to-bottom
                dogPosY += 1;
                if( dogPosY >= 7 ) dogPosY = 0;                 
                break;

            case 3://move bottom-to-top
                dogPosY -= 1;
                if( dogPosY <= 0 ) dogPosY = 7;                 
                break;
            }

            //assign 0 to previous position, meaning dog is no longer there
            onBoard[dogPrevPosX][dogPrevPosY] = 0;
            //assign 1 to new position , meaning dog is here
            onBoard[dogPosX][dogPosY] = 1;

            stepsCount++;
            dogPrevPosX = dogPosX;
            dogPrevPosY = dogPosY;                  
        }   
        //once all cells have been traversed , print result!!
        printSteps();
    }   

    /*prints the total number of step taken to traverse all cells*/
    public void printSteps()
    {
        System.out.println("Total steps taken by dog to traverse all cell = "+stepsCount);
    }

    public static void main(String[] args)
    {
        DogMovementSimulation dms = new DogMovementSimulation();
        dms.simulate();     
    }
}