3

すべての壁の間にリンクを作成しようとしている 2D グリッドがあります。

グリッドは次のように構成されます。

    grid = new State[8][8];
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            grid[i][j] = State.blank;
        }
    }

ヘビのゲームのように壁を通り抜けて反対側に行けるロボットがあります。

たとえば、ロボットが北を向いていて x[0]y[1] の位置にある場合、x[7]y[1] に接続する必要があります。

ロボットは、前方の 3 つのブロック (左に 1 つ、右に 1 つ、真正面に 1 つ) の内容を読み取ることもできる必要があります。

# x = empty space
# R = robot
# S = spaces robots sensors pick up

北を向いていた場合、これはロボットが拾うものです:

[S][S][S][x][x][x][x][x]
[x][R][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]

同様に、ロボットが東を向いていた場合、これは拾うものです。

[x][x][S][x][x][x][x][x]
[x][R][S][x][x][x][x][x]
[x][x][S][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]

私が抱えている問題は、ロボットが壁を通過するだけでなく、壁を通してセンサーを読み取ることができるようにするための適切なアルゴリズムを見つけることです。

ロボットが左上隅にあり、北を向いている場合、次のように壁を通して読み上げます。

[R][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[S][S][x][x][x][x][x][S]

ご想像のとおり、私はすでに IF ステートメントの長さのチャンクを実行しようとしましたが、非常識にならずにすべてをカバーするには可能性が多すぎます。

また、特定の状況に置かれたときの X と Y の変更を紙に書き留めましたが、アルゴリズムを示唆するパターンは実際には見当たりません。

どんな助けでも大歓迎です!

4

4 に答える 4

1

次のようなイテレータを使用しますx = (x + 1) % array.length

(またはx = (x - 1) % array.length

于 2013-02-26T18:06:36.800 に答える
1
public class Robot {
    public int x;
    public int y;
    public Robot(int x,int y) {
        this.x = x;
        this.y = y;
    }
    public void move(int direction, int steps) {
        switch(direction) {
            case 1: //north
                int temp1 = (x-steps)%8;
                x = temp1<0?(temp1+8):temp1;
                break;
            case 2: //south
                x = (x+steps)%8;
                break;
            case 3: //west
                int temp3 = (y-steps)%8;
                y = temp3<0?(temp3+8):temp3;
                break;
            case 4: //east
                y = (y+steps)%8;
                break;
            default:
                System.out.println("I'm not smart enough to handle the direciton provided!");
        }
    }

    public static void main(String[] args) {
        int[][] grid = new int[8][8];
        Robot robot = new Robot(0,0);
        System.out.println("I'm starting at (0,0).");
        robot.move(3, 9);
        System.out.println("I'm moving west by 9 steps.");
        System.out.println("I've arrived at ("+robot.x+","+robot.y+").");
    }
}

上記のコードでアイデアが得られることを願っています。私はそれをテストしました。お気軽にお試しください。ロボットの前にある 3 つのブロックの計算も同様です。あなたは自分でそれを理解することができます。

于 2013-02-26T19:07:45.420 に答える
1

いくつかに分けて書いてみますので、参考になれば幸いです。したがって、X 座標と Y 座標で表される 8x8 グリッドがあり、(0,0) が左上隅、(7,7) が右下隅です。アルゴリズムは次のようになります。

walking through walls:
N -> x = x, y = (y==0)?7:y-1
S -> x = x, y = (y==7)?0:y+1
E -> x = (x==7)?0:x+1, y = y
W -> x = (x==0)?7:x-1, y = y

Look ahead
N -> LH1 = x=x, y=y-1
     LH2 = x=x-1, y=y-1
     LH3 = x=x+1, y=y-1
S -> LH1 = x=x, y=y+1
     LH2 = x=x-1, y=y+1
     LH3 = x=x+1, y=y+1
E -> LH1 = x=x+1, y=y
     LH2 = x=x+1, y=y-1
     LH3 = x=x+1, y=y+1
W -> LH1 = x=x-1, y=y
     LH2 = x=x-1, y=y-1
     LH3 = x=x-1, y=y+1

このアルゴリズムを Java メソッドに変換すると、次のようになります。

public int getNextX (int currentX, String direction)
{
    if ("N".equals (direction) || "S".equals (direction))
    {
        return currentX;
    }
    else if ("E".equals (direction))
    {
        return ((currentX==7) ? 0 : currentX + 1);
    }
    else if ("W".equals (direction))
    {
        return ((currentX==0) ? 7 : currentX - 1);
    }
}

public int getNextY (int currentY, String direction)
{
    if ("E".equals (direction) || "W".equals (direction))
    {
        return currentY;
    }
    else if ("S".equals (direction))
    {
        return ((currentY==7) ? 0 : currentY + 1);
    }
    else if ("N".equals (direction))
    {
        return ((currentY==0) ? 7 : currentY - 1);
    }
}


public ArrayList getLookAheads (int currentX, int currentY, String direction)
{
    ArrayList lookAheads = new ArrayList ();
    int x[3];
    int y[3];
    if ("N".equals (direction))
    {
        // LH1
        x[0] = currentX;
        y[0] = currentY - 1;

        // LH2
        x[1] = currentX - 1;
        y[1] = currentY - 1;

        // LH3
        x[2] = currentX + 1;
        y[2] = currentY - 1;
    } 
    else if ("S".equals (direction))
    {
        // LH1
        x[0] = currentX;
        y[0] = currentY + 1;

        // LH2
        x[1] = currentX - 1;
        y[1] = currentY + 1;

        // LH3
        x[2] = currentX + 1;
        y[2] = currentY + 1;
    } 
    else if ("E".equals (direction))
    {
        // LH1
        x[0] = currentX + 1;
        y[0] = currentY;

        // LH2
        x[1] = currentX + 1;
        y[1] = currentY - 1;

        // LH3
        x[2] = currentX + 1;
        y[2] = currentY + 1;
    } 
    else if ("E".equals (direction))
    {
        // LH1
        x[0] = currentX - 1;
        y[0] = currentY;

        // LH2
        x[1] = currentX - 1;
        y[1] = currentY - 1;

        // LH3
        x[2] = currentX - 1;
        y[2] = currentY + 1;
    }

    for (int i=0;i < 3;i++)
    {
        HashMap h = new HashMap ();
        h.put ("X", new Integer (getNextX (x[i], direction)));
        h.put ("Y", new Integer (getNextY (y[i], direction)));

        lookAheads.add (h);
    }

    return lookAheads;
}

メソッドの構文はテストしていません (メモ帳に書いただけです)。コンパイル エラーがある場合はご容赦ください。

それが役立つことを願っています。

于 2013-02-26T18:45:20.453 に答える
0

これは、モジュラス演算子(%)を使用することで非常に簡単に解決できます。モジュロは、特定の上限の下で値を循環させます。したがって、ロボットのx値が最大境界を超えると、単純に0に戻ります。これにより、ロボットは右側の1つの壁を移動でき、x座標が0にリセットされ、左側に表示されます。ステージの側面。

于 2013-02-26T18:05:42.583 に答える