【解決方法】C で動的メモリを使用する練習を行います。


世界が球形ではないことは誰もが知っています。 世界が平らではないことも知っている人もいます。 の
世界は実際にはトーラス (ドーナツ型) です。
私たちの船長は、世界を平らな 2D グリッドとして想像します。
2D グリッドの上部から外れると、グリッドの下部に再表示されます。
グリッド、および下部から移動すると、同様に、グリッドに再表示されます
グリッドの上部。 両方の再出現は、同じ x を持つことになります。
ポジション。 さらに、左側または右側から移動すると、
同じ y で、それぞれ右側または左側に再出現
ポジション。
艦隊は拡大し続けているため、船を追跡する必要があります
あなたの艦隊に加わる。 参加する各船は、特定の x から始まります。
2D グリッド上の y 座標に移動し、1 分間に 1 単位の一定速度で移動します。
固定方向: 左 (負の x)、右 (正の x)、上 (正の y)、下 (負の y) のいずれか。
さらに悪いことに、さまざまな時点で小さな嵐が発生し、船が混乱します。 幸いなことに
嵐が発生する場所を事前に決定できます。目的は、警告を次の場所に送信することです。
影響を受ける正確な船。
問題
トロイダル グリッド上の船のリストが時間の経過とともに増加し、嵐が発生した場所が与えられた場合、どの船がどの船であるかを判断します。
警告する必要があります。
(標準) 入力
入力は、2 つの整数 W と H (20 ≤ W、H ≤ 500,000) を含む行で始まり、
それぞれグリッドの高さと幅。 最初は追跡しているボートはありません。
この行の後にコマンドのリストが続きます。 コマンドは、その順序で処理する必要があります
与えられた。 各コマンドは数字で始まり、4 つのタイプのいずれかになります。 種類は以下の通り、
● 1 xydn
○ このコマンドは、艦隊に新しい船が追加されたことを知らせてくれます。 船は場所から出発します
(x, y) (1 ≤ x ≤ W; 1 ≤ y ≤ H)。 値 d は、’L’、’R’、
新しい船が移動する方向を表す「D」または「U」(明確にするために引用符) (左、
それぞれ右、下、または上)。 船の名前は文字列 n で示されます。 の
文字列 n には、少なくとも 1 文字、最大で 20 文字が含まれます。 n のすべての文字は
大文字または小文字のいずれか。 同じ名前の船は 2 つとありません。
●2t
○ このコマンドは、時刻が t 分経過していることを通知します。 すべての船
それぞれの方向に t (1 ≤ t ≤ 525,600) 単位移動します。 しようとしていることに注意してください
この時点ですべての船を更新すると、場合によっては時間的に法外な費用がかかります。
プログラム全体の時間の合計は 10 億以下になります。
(10
9
)。
● 3 xyw
○このコマンドは、嵐が来ることを知らせてくれます。 影響を受ける「左下」のセル
嵐は位置 (x, y) (1 ≤ x ≤ W; 1 ≤ y ≤ H) にあり、嵐は w 単位になります。
幅 (x 方向) と h 単位の高さ (y 方向) (1 ≤ w ≤ 10; 1 ≤ h ≤ 10)。 船に注意
場所 (x + w, y) は嵐の影響を受けません。 さらに、嵐はまた、
2D グリッドをラップします。
● 4
○このコマンドは、プログラムの終了を要求します (会社が存続していることを願っています)。
(標準) 出力
3 つのコマンド (ストーム通知) ごとに、最初に負でない値を 1 行に出力する必要があります。
現在嵐の中にいるボートの数を表す整数 (n)。 次の n 行はそれぞれ
嵐の影響を受けた船の名前が含まれています。
サンプル入力 サンプル出力
20 25
1 4 14 L ボートワン
1 20 25 D ボートツー
3 2 12 2 10
3 2 12 10 2
1 2 12 R ボートスリー
2 1
3 2 12 2 10
3 2 12 10 2
4
0
0
2
ボートスリー
ボートワン
1
ボートスリー
20 20
1 20 1 L ボートワン
1 20 2 D ボートツー
1 20 20 D ボートスリー
1 19 20 R ボートフォー
1 2 1 L ボートファイブ
3 20 20 2 2
2 1
3 20 20 2 2
4
2
ボートスリー
ボートワン
3
ボートツー
ボートフォー
ボートファイブ

私が試したこと:

typedef struct ボート ボート;
typedef struct ArrayList ArrayList;
構造体ボート{
int start_x、start_y、delta_x、delta_y、start_time;
文字名[NAME_LEN + 1];
};
struct ArrayList {
struct Cage * ケージ;
int numCages;
};

解決策 1

私たちは立ち往生している人々を喜んで助けますが、それは私たちがあなたのためにすべてをするためにここにいるという意味ではありません! 私たちがすべての作業を行うことはできません。あなたはこれに対して報酬を受け取っているか、またはそれはあなたの成績の一部であり、私たちがあなたのためにすべてを行うことはまったく公平ではありません.

だから私たちはあなたが仕事をする必要があり、あなたが行き詰まったときにあなたを助けます. それは、あなたが提出できる段階的な解決策を提供するという意味ではありません!
現在の状況と、プロセスの次のステップを説明することから始めます。 次に、その次のステップを機能させるために何を試みたか、またその際に何が起こったかを教えてください。

開始するのに問題がある場合は、これが役立つ場合があります。 問題を解決するためのコードの書き方、初心者向けガイド[^]

解決策 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
typedef struct Boat Boat;
typedef struct ArrayList ArrayList;
struct Boat {
int start_x, start_y, delta_x, delta_y, start_time;
char name[NAME_LEN + 1];
};
struct ArrayList {
struct Boat * boats;
int numBoats;
};  ()
// Given a point to a list initialize the list
void initArrayList(ArrayList * list);
// Given a list and a boat tot add to it, add the boat to the end
// of the list
// Expand the list if necessary
void appendToList(ArrayList * list, Boat newBoat);
    I ried to include most of it , my input is scanning but not printing anything.
*/
typedef struct Boat Boat;
typedef struct ArrayList ArrayList;
int start_x, start_y, delta_x, delta_y, start_time;
// Up,Down,Left,Right / change positions 
struct Boat {
  char boat[21];
} boat;
// not sure if i need more variables
// organize by section
struct Section {
  struct Boat *boats;
  int numBoats;
} *m;
int main() {
  int nSections, cmd, l, r , u ,d ;
  char boatName[21];
  struct Section *m;
  scanf("%d", &nSections);
  // Dynamically allocate memory
         m = malloc (sizeof (struct Section) * nSections);
// set all the boats ,numBoats=0
for (int i = 0; i < nSections; i++) {
    m[i].boats = NULL;
    m[i].numBoats = 0;
}

do {
    scanf("%d", &cmd); 
    switch (cmd) { 
    case 1:
      scanf("%d %d", &l, &r); // what do i replace s and c with??
      if (l > nSections) continue; // ignore the inferior section number
      // Allocate the space for the boats
        m[l - 1].boats = realloc(m[l - 1].boats, r * sizeof(struct Section));
      // Initialize "NO boats found" as a command.
      for (int i = m[l - 1].numBoats; i < r; i++)
        strcpy(m[l - 1].boats[i].boat, "0");
      m[l - 1].numBoats = r;
      break;

    case 2:
      scanf("%d %d", &l, &r);// what do i replace s and c with??
      if (l > nSections)
        continue; // ignore the inferior section number
      if (r > m[l- 1].numBoats)
        continue; // ignore inferior numbers
      //
      scanf("%l", m[l - 1].boats[r - 1].boat);
      break;
      
    case 3:
      scanf("%d %d", &l, &r);
      if ((l > nSections) || (r> m[l - 1].numBoats)) { // wrong number
        printf("0\n");
        continue;
      }
      printf("%l\n", m[l - 1].boats[r - 1].boat);
      break;
    }
}while (cmd !=4);

void appendToList(ArrayList * list, Boat newBoat);
// what should I do ?



//clean up
for (int i =0; i< nSections; i++){
    free(m[i].boats);
    }
      //free(main)[i].boats;

      free(m);

return 0;
}

コメント

タイトルとURLをコピーしました