ホームページ  >  記事  >  バックエンド開発  >  ロボットがグリッド内を移動するのに必要な総コストを計算する C++ プログラム

ロボットがグリッド内を移動するのに必要な総コストを計算する C++ プログラム

WBOY
WBOY転載
2023-08-25 16:53:171282ブラウズ

ロボットがグリッド内を移動するのに必要な総コストを計算する C++ プログラム

サイズ h x w のグリッドがあるとします。グリッド内の各セルには正の整数が含まれています。ここで、特定のセル (p, q) (p は行番号、q は列番号) に経路検索ロボットが配置され、セル (i, j) に移動できます。移動操作には、|p - i| |q - j| に等しい特定のコストがあります。現在、次のプロパティを持つ q 個の旅行があります。

  • 各旅行には 2 つの値 (x, y) があり、共通の値 d があります。

  • ロボットは値 x のセルに配置され、値 x d の別のセルに移動します。

  • 次に、値 x d d を持つ別のセルに移動します。このプロセスは、ロボットが y 以上の値を持つセルに到達するまで続きます。

  • y - x は d の倍数です。

これらの旅行を考えると、各旅行の合計費用を見つける必要があります。ロボットが移動できない場合、移動コストは 0 になります。

入力が h = 3、w = 3、d = 3、q = 1、grid = {{2, 6, 8}, {7, 3, 4}, {5 の場合, 1 , 9}}、trips = {{3, 9}} の場合、出力は 4 になります。

3 セル (2, 2) 上

6 セル (1, 2) 上

9 セル (3, 3) 上

合計コスト= | (1 - 2) (2 - 2) | | (3 - 1) (3 - 2) | = 4。

この問題を解決するには、次の手順に従います。

Define one map loc
for initialize i := 0, when i < h, update (increase i by 1), do:
   for initialize j := 0, when j < w, update (increase j by 1), do:
      loc[grid[i, j]] := new pair(i, j)
Define an array dp[d + 1]
for initialize i := 1, when i <= d, update (increase i by 1), do:
   j := i
   while j < w * h, do:
      n := j + d
      if j + d > w * h, then:
      Come out from the loop
   dx := |first value of loc[n] - first value of loc[j]|
   dy := |second value of loc[n] - second value of loc[j]|
   j := j + d
   insert dx + dy at the end of dp[i]
for initialize j := 1, when j < size of dp[i], update (increase j by 1), do:
   dp[i, j] := dp[i, j] + dp[i, j - 1]
for initialize i := 0, when i < q, update (increase i by 1), do:
   tot := 0
   le := first value of trips[i]
   ri := second value of trips[i]
   if ri mod d is same as 0, then:
      f := d
   Otherwise,
         f := ri mod d
   pxl := (le - f) / d
   pxr := (ri - f) / d
   if le is same as f, then:
    if ri is same as f, then:
      tot := 0
   Otherwise
      tot := tot + (dp[f, pxr - 1] - 0)
   Otherwise
      if ri is same as f, then:
            tot := 0
  Otherwise
tot := tot + dp[f, pxr - 1] - dp[f, pxl - 1]
print(tot)

理解を深めるために、以下の実装を見てみましょう −

Example

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
void solve(int h, int w, int d, int q, vector<vector<int>> grid,
vector<pair<int, int>> trips) {
   map<int, pair<int, int>> loc;
   for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
         loc[grid[i][j]] = make_pair(i, j);
   }
   vector<int> dp[d + 1];
   for (int i = 1; i <= d; i++) {
      int j = i;
      while (j < w * h) {
         int n = j + d;
          if (j + d > w * h)
             break;
             int dx = abs(loc[n].first - loc[j].first);
             int dy = abs(loc[n].second - loc[j].second);
             j += d;
             dp[i].push_back(dx + dy);
      }
      for (j = 1; j < dp[i].size(); j++)
        dp[i][j] += dp[i][j - 1];
   }
   for (int i = 0; i < q; i++) {
      int tot = 0;
      int le, ri;
      le = trips[i].first;
      ri = trips[i].second;
      int f;
      if (ri % d == 0)
         f = d;
      else
         f = ri % d;
      int pxl, pxr;
      pxl = (le - f) / d;
      pxr = (ri - f) / d;
      if (le == f){
         if (ri == f)
            tot = 0;
         else
            tot += (dp[f][pxr - 1] - 0);
      } else {
         if (ri == f)
            tot = 0;
         else
            tot += dp[f][pxr - 1] - dp[f][pxl - 1];
      }
      cout<< tot << endl;
    }
}
int main() {
   int h = 3, w = 3, d = 3, q = 1;
   vector<vector<int>> grid = {{2, 6, 8}, {7, 3, 4}, {5, 1, 9}};
   vector<pair<int, int>> trips = {{3, 9}};
   solve(h, w, d, q, grid, trips);
   return 0;
}

Input

3, 3, 3, 1, {{2, 6, 8}, {7, 3, 4}, {5, 1, 9}}, {{3, 9}}

出力

4

以上がロボットがグリッド内を移動するのに必要な総コストを計算する C++ プログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。