5

LRV(Least recent visit) アルゴリズムを使用してプログラムを作成しています。基本的に、ロボットがグリッド (2D char 配列) を通過するアルゴリズムを設計します。ロボットはグリッドを横断しながら、各セルがEMPTY('O' でOCCUPIED定義)、('S' で定義)、またはBLOCKED('X' で定義) のいずれかであるかどうかをチェックします。セルは、Sensor と呼ばれるオブジェクトによってのみ占有されます (これには独自のクラスがあります)。BLOCKEDセルはトラバースできません。ロボットが動く必要があるたびに、センサーから方向を受け取ります。そのため、最初はロボットをグリッド上に配置し、センサーをドロップしてそこから方向を取得するか、既存のセンサーから方向を取得します。

私のプログラムについて説明したので、具体的な質問は、getVisitingDirectionINT を返すメソッドを持つクラス Sensor があるということです。各方向 (タイプ INT の北、南、東、西) のカウンターがあります。これがクラスです。

package ITI1121A;
public class Sensor {

private int cColumns;
private int cRows;
private int North;
private int South;
private int West;
private int East;

public Sensor(int sX, int sY) { 

cColumns = sX;
cRows = sY;
South= -1;
North = -1;
West = -1;
East = -1;

}
/* ADD YOUR CODE HERE */
public int getX ()
{return cColumns;}
public int getY ()
{return cRows;}

public int getVisitingDirection(GridMap g1)
  boolean temp;
{
  if(cRows==0){
  //top row
    if(cColumns==0){
    temp=g1.isCellBlocked(cColumns+1,cRows);
    if (temp=false){
    return West++;
    }

    }

  }

}

public void increaseCounter(int direction)
{}

}

今私が立ち往生しているのは getVisitingDirection です。グリッドの左上端 (座標 0,0) を確認する if ステートメントを作成しようとしましたが、それで終わりです。ロボットに方向を与え、その方向のカウンターを増やすメソッドが必要です。ここでコンセプトを理解することさえ本当に難しいです。どんな助けでも大歓迎です!ありがとうヴァルン

4

1 に答える 1

3

正しいパスに設定する関数を疑似コードに入れました。

// lets assume binary code where 0000 represents top, right, bottom, left 
// (0011 would mean can go bottom or left)
public int getVisitingDirection()
{
    String tmpBinary = "b"; // to initialize the field

    // check if can go up
    tmpBinary += (cCollums>0&&checkIfUpIsBlocked()) "1" : "0";
    // TODO check if can go right (compare with size + 1)
    // check if can go bottom (compare with size +1 )
    // check if can go left (check if > 0)

    // when this runs tmpBinary will be in the form of a binary representation
    // this will be passed to the robot that can then chooses where to go
    // 1111 would mean that all squares are clear to go
    // 1101 would mean top, right and left
    // etc...

}

private boolean checkIfUpIsBlocked()
{
    // TODO must check if the cell is blocked
    return true;
}

checkIfUpIsBlocked + メソッドを作成する必要があることに注意してください。

その上、縫い目はかなり良いです。
int フィールドは読みやすく、人的エラーが発生しにくいため、列挙型で変更することをお勧めします。

int番号で道順を返す方法は?
バイナリ ロジックを使用して単一の int を返し、複数の方向を表すことができます。

0000 (int 0)  => no possible direction
0001 (int 1)  => left direction
0010 (int 2)  => bottom direction
0011 (int 3)  => left and bottom
0100 (int 4)  => right direction
(...)
1111 (int 15) => all directions possible
于 2012-01-30T08:16:05.870 に答える