2D空間でオブジェクトを追跡する


11

説明

この課題のタスクは、n×n空間で特定のオブジェクトを追跡するプログラムまたは機能を考案することです。

I / O

プログラムには3つの入力が与えられますが、これらは適切な方法で取得できます。

n飛行機の側面のサイズになります。(したがって、n=5場合、飛行機はなります)。常に奇数の整数であると仮定できます。5×5n

sオブジェクトの開始位置になり、座標のペアとして指定されます。(x,y)

D順序付けられたペアのベクトルになります。Dの形式に従います。ここで、は、基本的および主要な方向に対して常にのいずれかであり、は「ティック」の数の整数。D=[(d0,t0),(d1,t1),...,(dn,tn)]dkt k'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'tk

これらの入力が与えられると、プログラムは平面内のオブジェクトの追跡を出力する必要があります。

ルール

出力には平面の境界が含まれている必要あります。例えば:

-21012 +
+┌─────┐┐
2││
1││
0││
1││
2││
-└─────┘

空の平面の例です。上記および横の数字は参照用であり、印刷する必要はありません。5×5

空白でない限り(または空白としてレンダリングする限り)、境界に任意の文字を使用できます。選択するキャラクターは、平面全体を区切る必要があります。つまり、それらの間に隙間がないようにする必要があります。

許容されるプレーンには次のものがあります。

┌──┐.... ---- +-+
││。。| | | |
││。。| | | |
└──┘; ....; ----; +-+

許可されないプレーンは次のとおりです。

      .... .... ++++。。
            。。+ +。。
            。+ +。。
    ; ....; ....; + +; 。。

追跡されるオブジェクトは、平面上のスペースを1つだけ占有し、境界文字と異なる限り、選択した任意の文字になります。

追跡されるオブジェクトのトレースは、プレーン上のスペースを1つしか占有せず、オブジェクトとは異なる限り、選択したどのような文字でもかまいません。

各要素についてにおける、オブジェクトが移動しなければならないに向かってスペースを、と後ろにトレースを残します。(dk,tk)Dtd

オブジェクトが境界にぶつかると、それが反映されます。オブジェクトにまだ動きがある場合は、オブジェクトが反映された方向に動き続けます。

参考のために、これらの指示は相互に反映しています。

NS →上部または下部の境界が満たされたとき。

EW →横方向の境界が満たされたとき。

最終出力には、可能な限り最新のトレースが含まれます。つまり、オブジェクトがすでにトレースがあるスペースにトレースを残す場合、新しいトレース文字が古いトレース文字を上書きします。

通常どおり、標準の抜け穴はデフォルトで禁止されています

得点:

これは挑戦です。

例:

入力:、、n=5s=(0,0)D=[(NW,2),(S,2),(E,1)]

解決する:

t=0

    0
 ┌─────┐
 ││
 ││
0│○│
 ││
 ││
 └─────┘

t=2

    0
 ┌─────┐
 │○│
 │\│
0│\│
 ││
 ││
 └─────┘

t=4

    0
 ┌─────┐
 │∧│
 │| \│
0│○\│
 ││
 ││
 └─────┘

t=5

    0
 ┌─────┐
 │∧│
 │| \│
0│└○\│
 ││
 ││
 └─────┘

(0は参照用であり、最終出力にある必要はありません。)


n=9s=(3,1)D=[(N,2),(SW,8),(SE,3),(NE,8)]

t=10

      0     
 ┌─────────┐
 ││
 ││
 ││
 │∧│
0│/ | │
 │○/ | │
 │⟨/│
 │\ /│
 │∨│
 └─────────┘

SWNWNWNE

t=21

      0     
 ┌─────────┐
 │○│
 │\│
 │\│
 │\│
0│/ |⟩│
 │∧/ /│
 │⟨\ / /│
 │\ \ /│
 │∨∨│
 └─────────┘

テストケース:

n=5s=(0,0)D=[(NW,2),(S,2),(E,1)]

出力:

    0
 ┌─────┐
 │∧│
 │| \│
0│└○\│
 ││
 ││
 └─────┘


入力:n=9s=(3,1)D=[(N,2),(SW,8),(SE,3),(NE,8)]

出力:

      0     
 ┌─────────┐
 │○│
 │\│
 │\│
 │\│
0│/ |⟩│
 │∧/ /│
 │⟨\ / /│
 │\ \ /│
 │∨∨│
 └─────────┘


入力:n=3s=(1,1)D=[(N,5),(W,5)]

出力:

   0
 ┌───┐
 │|│
0│-○┐│
 │|│
 └───┘


入力:n=11s=(3,5)D=[(NW,8),(E,5),(SE,3),(SW,5),(N,6),(NE,10)]

出力:

       0
 ┌────────────┐
 │∧│
 │/ \│
 │┌-/-\ \│
 │\ | / \ \│
 │\ | \ \│
0│| /⟩│
 │| \ / /│
 │| /○│
 │| / \│
 │∨\│
 │\│
 └────────────┘

この挑戦はサンドボックスされたことを言及するのを忘れました。
J.サレ

私たちは取ることができます'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'代わりに、整数0インデックスとして(または1インデックス)?そう[('NW',2),('S',2),('E',1)]なっ[[7,2],[4,2],[2,1]]例えば。
ケビンクルーッセン

@KevinCruijssen確かに、問題ありません。答えでそれを指摘してください。
J.サレ

1
@Arnauldはい、単一のトレース文字を使用できます。テストケースを簡単に視覚化できるように、複数使用しましたが、必須ではありません。トレース文字がトレースされるオブジェクトの文字と異なることを確認してください。
J.サレ

1
@Arnauld " 追跡されるオブジェクトは、平面上のスペースを1つだけ占有し、境界文字と異なる限り、選択する文字であれば何でもかまいません。追跡されるオブジェクトのトレースは、選択した文字であれば、それらは平面上の1スペースのみを占有し、オブジェクトとは異なるためです。
ケビンクルーイッセン

回答:


9

JavaScript(ES6)、228バイト

(n,x,y,[[dir,len],[dir,len],...])07

0境界、1トレース、および3最終位置の文字列を出力します。

(n,x,y,a)=>(g=X=>Y>n?'':(Y%n&&X%n&&a.map(([d,l],i)=>(M=H=>(h-X|v-Y||(k|=a[i+!l]?1:3),l--&&M(H=(h+H)%n?H:-H,h+=H,v+=V=(v+V)%n?V:-V)))(~-(D='12221')[d],V=~-D[d+2&7]),h=x+n/2,v=n/2-y,k=' ')&&k)+(X<n?'':`
`)+g(X++<n?X:!++Y))(Y=!++n)

オンラインでお試しください!

どうやって?

JavaScriptでは、「キャンバス」(つまり、文字のマトリックス)への初期化と描画は少し面倒で時間がかかります。

このコードは異なる戦略を使用しています。出力を2D配列に保存する代わりに、左から右、上から下に文字ごとに文字列を作成します。各反復で:

  • 0境界を越えている場合、aを出力します。
  • それ以外の場合、フルパスをシミュレートし、現在の位置を通過しているかどうかを確認します。出力するか1、出力するか、3そうでなければスペースを出力します。
  • 正しい境界に達したら、改行を追加します。

全体として、これは最短のアプローチではないかもしれませんが、試してみる価値があると思いました。


9

ジャワ10、350の 343 340 336バイト

(n,s,S,D)->{int N=n+2,x=N/2+s,y=N/2-S,i=N*N;var r=new char[N][N];for(;i-->0;)r[i/N][i%N]=i/N*(i%N)<1|i/N>n|i%N>n?'#':32;r[y][x]=42;for(var d:D)for(i=d[0];d[1]-->0;r[y+=i%7<2?1/y*2-1:i>2&i<6?y<n?1:-1:0][x+=i>0&i<4?x<n?1:-1:i>4?1/x*2-1:0]=42)i=y<2&i<2|y>=n&i>2&i<5?4-i:x<2&i>4|x>=n&i>0&i<4?8-i:y<2&i>6?5:y<n|i!=5?i:7;r[y][x]=79;return r;}

Dは、方向が0から始まる整数である2D整数配列ですN=0, NE=1, E=2, SE=3, S=4, SW=5, W=6, NW=7。開始x,y座標は、2つの別個のパラメーターsSです。出力は文字行列です。境界、トレイル、および終了位置として
使用#します(ただし、必要に応じて、同じバイト数のUnicode範囲内の任意のASCII文字を3つすべて使用できます)。*O[33,99]

オンラインでお試しください。

@ceilingcatのおかげで-4バイト。
動きを単純化し、どの方向に向かっていくかによって、ゴルフをさらに確実に行うことができます。

説明:

(n,s,S,D)->{           // Method with `n`,`s,S`,`D` parameters & char-matrix return-type
  int N=n+2,           //  Set `N` to `n+2`, since we use it multiple times
      x=N/2+s,         //  Calculate the starting `x` coordinate
      y=N/2-S,         //  Calculate the starting `y` coordinate
      i=N*N;           //  Index integer
  var r=new char[N][N];//  Result char-matrix of size `N` by `N`
  for(;i-->0;)         //  Loop `i` in the range (`N**2`, 0]
    r[i/N][i%N]=       //    Set the cell at position `i` divmod-`N` to:
      i/N*(i%N)<1|i/N>n|i%N>n?
                       //     If we're at a border:
       '#'             //      Set the current cell to '#'
      :                //     Else:
       32;             //      Set the current cell to ' ' (a space) instead
  r[y][x]=42;          //  Then set the starting position `x,y` to a '*'
  for(var d:D)         //  Loop over the `D` input:
    for(i=d[0];        //   Set `i` to the current direction
        d[1]-->0       //   Inner loop the current `d` amount of times
        ;              //     After every iteration:
         r[y+=         //      Change `y` based on the current direction
            i%7<2?     //       If the current direction is N, NE, or NW
             1/y*2-1   //        If we're at the top border:
                       //         Go one row down
                       //        Else
                       //         Go one row up
            :i>2&i<6?  //       Else-if the current direction is S, SE, or SW
             y<n?      //        If we're at the bottom border
              1        //         Go one row up
             :         //        Else
              -1       //         Go one row down
            :          //       Else (directions E or W)
             0]        //        Stay in the same row
          [x+=         //      Change `x` based on the current direction
            i>0&i<4?   //       If the current direction is E, NE, or SE
             x<n?      //        If we're NOT at the right border
              1        //         Go one column to the right
             :         //        Else:
              -1       //         Go one column to the left
            :i>4?      //       Else-if the current direction is W, NW, or SW
             1/x*2-1   //        If we're NOT at the left border:
                       //         Go one column to the left
                       //        Else:
                       //         Go one column to the right
            :          //       Else (directions N or S)
             0]        //        Stay in the same column
               =42)    //      And fill this new `x,y` cell with a '*'
      i=               //    Determine the new direction
        y<2&i<2|y>=n&i>2&i<5?4-i:x<2&i>4|x>=n&i>0&i<4?8-i:y<2&i>6?5:y<n|i!=5?i:7;
                       //     (See loose explanation below)
  r[y][x]=79;          //  And finally set the last `x,y` cell to 'O'
  return r;}           //  And return the result-matrix

y<2&i<2|y>=n&i>2&i<5?4-i:x<2&i>4|x>=n&i>0&i<4?8-i:y<2&i>6?5:y<n|i!=5?i:7を使用4-iして8-i、ほとんどの方向変更のために以下のゴルフバージョンです:

y<2?     // If we're at the top border
 i==0?   //  If the current direction is N
  4      //   Change it to direction S
 :i==1?  //  Else-if the current direction is NE
  3      //   Change it to SE
 :i==7?  //  Else-if the current direction is NW
  5      //   Change it to SW
 :       //  Else
  i      //   Leave the direction the same
:x<2?    // Else-if we're at the left border
 i==7?   //  If the current direction is NW
  1      //   Change it to NE
 :i==6?  //  Else-if the current direction is W
  2      //   Change it to E
 :i==5?  //  Else-if the current direction is SW
  3      //   Change it to SE
 :       //  Else
  i      //   Leave the direction the same
:y>=n?   // Else-if we're at the bottom border
 i==3?   //  If the current direction is SE
  1      //   Change it to NE
 :i==4?  //  Else-if the current direction is S
  0      //   Change it to N
 :i==5?  //  Else-if the current direction is SW
  7      //   Change it to NW
 :       //  Else
  i      //   Leave the direction the same
:x>=n?   // Else-if we're at the right border
 i==1?   //  If the current direction is NE
  7      //   Change it to NW
 :i==2?  //  Else-if the current direction is E
  6      //   Change it to W
 :i==3?  //  Else-if the current direction is SE
  5      //   Change it to SW
 :       //  Else
  i      //   Leave the direction the same
:        // Else
 i       //  Leave the direction the same

3

、74バイト

NθJ⊘⊕θ⊘⊕θUR±⊕⊕θJN±NFA«≔⊟ιζF⊟ι«≔ζδ↷δ¶F›⊗↔ⅈθ≦±ζF›⊗↔ⅉθ≦⁻⁴ζ≧﹪⁸ζ↷⁴¶↶⁴↶δ↷ζ*¶↶ζPo

オンラインでお試しください!リンクは、コードの詳細バージョンです。n、x、y、dの形式で入力します。dは[距離、方向]ペアの配列の配列で、方向は数値エンコード0 =南回り時計回りから7 =南東向きです。説明:

NθJ⊘⊕θ⊘⊕θUR±⊕⊕θ

n原点を中心とするサイズのボックスを入力して描画します。

JN±N

入力とジャンプするxy(ただし、ネゲートy炭のy軸はダウン増加しているため)。

FA«

のエントリをループしますd

≔⊟ιζ

初期方向を抽出します。

F⊟ι«

目的の距離で繰り返します。

≔ζδ

方向を保存します。

↷δ¶

その方向に実験的に移動します。

F›⊗↔ⅈθ≦±ζ

これが側面から外れたら、方向を水平に反転します。

F›⊗↔ⅉθ≦⁻⁴ζ

これが上部または下部から外れたら、方向を垂直に反転します。

≧﹪⁸ζ

8を法とする方向を減らします(ピボットコマンドは0〜7の値のみを受け入れます)。

↷⁴¶↶⁴

実験的な動きを元に戻します。

↶δ↷ζ*¶

正しい方向を向いて、トレースを印刷して移動します。

↶ζPo

デフォルトの方向に戻り、オブジェクトを現在の位置に印刷します。


2

JavaScript、206バイト

入力を(n、x、y、[[dir、len]、[dir、len]、...])として受け取ります。方向はビットマスクを使用してエンコードされます。

S : 1  
N : 2   
E : 4  
W : 8  
SE: 5 (1|4)  
SW: 9 (1|8)
NE: 6 (2|4)
NW:10 (2|8)

文字列を出力します

- 1 for top and bottom boundary
- 4 for left and right boundary 
- 5 for corners 
- 0 for trace
- 8 for the final position.

境界の異なる値は、次の方向を評価するために使用されます

(n,x,y,d)=>(Q=[e=`
5`+'1'[R='repeat'](n)+5,o=n+3,-o,c=[...e+(`
4`+' '[R](n)+4)[R](n)+e],1,1+o,1-o,,-1,o-1,~o],p=1-o*(~n/2+y)-~n/2+x,c[d.map(([q,s])=>{for(a=q;s--;p+=Q[a^=c[p+Q[a]]*3])c[p]=0}),p]=8,c.join``)

少ないゴルフ

F=(n,x,y,d) => (
  o = n+3, // vertical offset, accounting for boundaries and newline
  // Q = offsets for different directions, bitmask indexed 
  Q = [,  // 0000 no direction
     o,   // 0001 S
     -o,  // 0010 N
     ,    // 0011 NS - invalid
     1 ,  // 0100 E
     1+o, // 0101 SE
     1-o, // 0110 NE
     ,    // 0111 NSE - invalid
     -1,  // 1000 W
     o-1, // 1001 SW
    -o-1],// 1010 NW

  e = `\n5`+'1'.repeat(n)+5, // top and bottom boundary
  c = [...e + (`\n4` + ' '.repeat(n) + 4).repeat(n) + e], // canvas
  p = 1 - o*(~n/2+y) - ~n/2 + x, // start position
  d.map( ([q,s]) => { // repeat for each element in 'd'
    a = q; // starting offset pointer - will change when bounce
    while( s-- )
    {
      c[p] = 0; // trace
      b = c[p + Q[a]] // boundary value or 0 (space count 0)
      a ^= b * 3 // xor with 0 if no bounce, else 3 or 12 or 15
      p += Q[q]  // advance position
    }
  })
  c[p] = 8, // set end position
  c.join``
)

テスト

var F=
(n,x,y,d)=>(Q=[e=`
5`+'1'[R='repeat'](n)+5,o=n+3,-o,c=[...e+(`
4`+' '[R](n)+4)[R](n)+e],1,1+o,1-o,,-1,o-1,~o],p=1-o*(~n/2+y)-~n/2+x,c[d.map(([q,s])=>{for(a=q;s--;p+=Q[a^=c[p+Q[a]]*3])c[p]=0}),p]=8,c.join``)

var out=x=>O.textContent+=x

var test=(n,x,y,d)=>{
  var dd = d.map(([d,s])=>[,'S','N',,'E','SE','NE',,'W','SW','NW'][d]+' '+s)
  out([n,x,y]+' ['+dd+']')
  out(F(n,x,y,d))
  out('\n\n')
}

test(5,0,0,[[10,2],[1,2],[4,1]])
test(9,3,-1,[[2,2],[9,8],[5,3],[6,8]])
test(11,3,-5,[[10,8],[4,5],[5,2],[9,5],[2,6],[6,10]])
<pre id=O></pre>


2

C(gcc)352 323バイト

ceilingcatのおかげで29バイトのゴルフができました。

#define G(x,a)x+=a=x<2|x>m-3?-a:a
#define A(p)atoi(v[p])
m,r,c,x,y,s,a,b;main(q,v)int**v;{m=A(1)+2;int f[r=m*m];for(x=A(2)+m/2;r--;f[r]=32);for(y=A(s=3)+m/2;++s<q;)for(a=cos(A(s)*.8)*2,b=sin(A(s)*.8)*2,c=A(++s);c--;G(y,b),f[y*m+x]=42)G(x,a);for(f[y*m+x]=64;++r<m;puts(""))for(c=0;c<m;c++)putchar(c%~-m&&r%~-m?f[r*m+c]:35);}

オンラインでお試しください!

プログラムは入力をコマンドライン引数として受け取ります(などa.out 10 1 1 3 5 0 4 7 2):

  • 最初の引数はフィールドサイズです。
  • (x,y)
  • (d,t)dEt

説明

// Update the coordinate AND simultaneously modify the direction (if needed)
#define G (x, a) x += a = x < 2 || x >= m - 2 ? -a : a

// Get the numeric value of an argument
#define A (p) atoi (v[p])

// variables
m, // width and height of the array with field data
r, c, // helpers
x, y, // current coordinates of the actor
s, // helper
a, b; // current direction of the movement

main (q, v) char **v;
{
    // array size is field size + 2 (for borders)
    m = A (1) + 2;

    // allocate the array
    int f[r = m * m];

    // fill the array with spaces,
    for
    (
        // but first get x of the actor
        x = A (2) + m / 2;

        r--;

        f[r] = 32
    );

    // trace: iterate over remaining commandline argument pairs
    for
    (
        // but first get y of the actor
        y = A (s = 3) + m / 2;

        ++s < q; // loop until no args left
    )
        // for each such pair
        for
        (
            a = cos (A (s) * .8) * 2,  // get the x-increment
            b = sin (A (s) * .8) * 2, // get the y-increment
            c = A (++s);  // then get the number of steps

            c--;

            // after each step:
            G (y, b), // update y and maybe the y-direction
            f[y * m + x] = 42 // draw the trail
        )
            G (x, a); // update x and maybe the x-direction

   // output
   for
   (
       f[x * m + y] = 64; // put a @ to the current position of the actor
       ++r < m; // r == -1 at the beginning of the loop so preincrement

       puts("") // terminate each row with newline
   )
       // iterate over columns in the row
       for (c = 0; c < m; c++)
           putchar
           (
               c % ~ -m && r % ~ -m ? // if it is not a border cell,
               f[r * m + c] // output the character from the array
               : 35 // otherwise output the #
           );
}

1
追跡されたオブジェクトのトレースは、プレーン上のスペースを1つだけ占有し、オブジェクトと異なる限り、選択した任意の文字である可能性があるため、コードには終了位置のオブジェクト出力が欠落していると思いますそれ以外は、私には良さそうです。
J.サレ

おっと、J.Salléに気づいてくれてありがとう。幸いなことに、修正してもプログラムは長くなりませんでした。
マックスイェクラコフ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.