搜索
首页后端开发C++C++程序用于找出机器人在网格中到达特定单元所需的跳跃次数

C++程序用于找出机器人在网格中到达特定单元所需的跳跃次数

假设我们有一个 h x w 的网格。网格在一个名为 'initGrid' 的二维数组中表示,其中网格中的每个单元格都用 '#' 或 '.' 表示。 '#' 表示网格中有障碍物,'.' 表示该单元格上有一条路径。现在,一个机器人被放置在网格上的一个单元格 'c' 上,该单元格具有行号 x 和列号 y。机器人必须从一个具有行号 p 和列号 q 的单元格 'd' 移动到另一个单元格。单元格坐标 c 和 d 都以整数对的形式给出。现在,机器人可以按以下方式从一个单元格移动到另一个单元格:

  • 如果机器人想要移动到的单元格位于当前单元格的垂直或水平相邻位置,机器人可以直接从一个单元格走到另一个单元格。

  • 机器人可以跳到以其当前位置为中心的 5x5 区域中的任何单元格。

  • 机器人只能移动到不包含障碍物的网格中的另一个单元格。机器人也不能离开网格。

我们需要找出机器人到达目标所需的跳数。

因此,如果输入为 h = 4,w = 4,c = {2, 1},d = {4, 4},initGrid = {"#...", ".##.", "...#", "..#."},那么输出将为 1。机器人只需要一次跳跃就可以到达目的地。

为了解决这个问题,我们将按照以下步骤进行:

N:= 100
Define intger pairs s and t.
Define an array grid of size: N.
Define an array dst of size: N x N.
Define a struct node that contains integer values a, b, and e.
Define a function check(), this will take a, b,
   return a >= 0 AND a < h AND b >= 0 AND b < w
Define a function bfs(), this will take a, b,
   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:
      dst[i, j] := infinity
   dst[a, b] := 0
   Define one deque doubleq
   Insert a node containing values {a, b, and dst[a, b]} at the end of doubleq
   while (not doubleq is empty), do:
      nd := first element of doubleq
      if e value of nd > dst[a value of nd, b value of nd], then:
         Ignore the following part, skip to the next iteration
   for initialize diffx := -2, when diffx <= 2, update (increase diffx by 1), do:
   for initialize diffy := -2, when diffy <= 2, update (increase diffy by 1), do:
      tm := |diffx + |diffy||
      nx := a value of nd + diffx, ny = b value of nd + diffy
      if check(nx, ny) and grid[nx, ny] is same as &#39;.&#39;, then:
         w := (if tm > 1, then 1, otherwise 0)
         if dst[a value of nd, b value of nd] + w < dst[nx, ny], then:
            dst[nx, ny] := dst[a value of nd, b value of nd] + w
            if w is same as 0, then:
               insert node containing values ({nx, ny, dst[nx, ny]}) at the beginning of doubleq.
          Otherwise
insert node containing values ({nx, ny, dst[nx, ny]}) at the end of doubleq.
s := c
t := d
(decrease first value of s by 1)
(decrease second value of s by 1)
(decrease first value of t by 1)
(decrease second value of t by 1)
for initialize i := 0, when i < h, update (increase i by 1), do:
grid[i] := initGrid[i]
bfs(first value of s, second value of s)
print(if dst[first value of t, second value of t] is same as infinity, then -1, otherwise dst[first value of t, second value of t])

Example

让我们看下面的实现以获得更好的理解−

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
#define N 100
int h, w;
pair<int, int> s, t;
string grid[N];
int dst[N][N];
struct node {
   int a, b, e;
};
bool check(int a, int b) {
   return a >= 0 && a < h && b >= 0 && b < w;
}
void bfs(int a, int b) {
   for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
         dst[i][j] = INF;
   }
   dst[a][b] = 0;
   deque<node> doubleq;
   doubleq.push_back({a, b, dst[a][b]});

   while (!doubleq.empty()) {
      node nd = doubleq.front();
      doubleq.pop_front();
      if (nd.e > dst[nd.a][nd.b])
         continue;
      for (int diffx = -2; diffx <= 2; diffx++) {
         for (int diffy = -2; diffy <= 2; diffy++) {
            int tm = abs(diffx) + abs(diffy);
            int nx = nd.a + diffx, ny = nd.b + diffy;
            if (check(nx, ny) && grid[nx][ny] == &#39;.&#39;) {
               int w = (tm > 1) ? 1 : 0;
               if (dst[nd.a][nd.b] + w < dst[nx][ny]) {
                  dst[nx][ny] = dst[nd.a][nd.b] + w;
                  if (w == 0)
                     doubleq.push_front({nx, ny, dst[nx][ny]});
                  else
                     doubleq.push_back({nx, ny, dst[nx][ny]});
               }
            }
         }
      }
   }
}
void solve(pair<int,int> c, pair<int, int> d, string initGrid[]){
   s = c;
   t = d;
   s.first--, s.second--, t.first--, t.second--;
   for(int i = 0; i < h; i++)
      grid[i] = initGrid[i];
   bfs(s.first, s.second);
   cout << (dst[t.first][t.second] == INF ? -1 :
      dst[t.first][t.second]) << &#39;\n&#39;;
}
int main() {
   h = 4, w = 4;
   pair<int,int> c = {2, 1}, d = {4, 4};
   string initGrid[] = {"#...", ".##.", "...#", "..#."};
   solve(c, d, initGrid);
   return 0;
}

输入

4, 4, {2, 1}, {4, 4}, {"#...", ".##.", "...#", "..#."}

输出

1

以上是C++程序用于找出机器人在网格中到达特定单元所需的跳跃次数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
继续使用C:耐力的原因继续使用C:耐力的原因Apr 11, 2025 am 12:02 AM

C 持续使用的理由包括其高性能、广泛应用和不断演进的特性。1)高效性能:通过直接操作内存和硬件,C 在系统编程和高性能计算中表现出色。2)广泛应用:在游戏开发、嵌入式系统等领域大放异彩。3)不断演进:自1983年发布以来,C 持续增加新特性,保持其竞争力。

C和XML的未来:新兴趋势和技术C和XML的未来:新兴趋势和技术Apr 10, 2025 am 09:28 AM

C 和XML的未来发展趋势分别为:1)C 将通过C 20和C 23标准引入模块、概念和协程等新特性,提升编程效率和安全性;2)XML将继续在数据交换和配置文件中占据重要地位,但会面临JSON和YAML的挑战,并朝着更简洁和易解析的方向发展,如XMLSchema1.1和XPath3.1的改进。

现代C设计模式:构建可扩展和可维护的软件现代C设计模式:构建可扩展和可维护的软件Apr 09, 2025 am 12:06 AM

现代C 设计模式利用C 11及以后的新特性实现,帮助构建更灵活、高效的软件。1)使用lambda表达式和std::function简化观察者模式。2)通过移动语义和完美转发优化性能。3)智能指针确保类型安全和资源管理。

C多线程和并发:掌握并行编程C多线程和并发:掌握并行编程Apr 08, 2025 am 12:10 AM

C 多线程和并发编程的核心概念包括线程的创建与管理、同步与互斥、条件变量、线程池、异步编程、常见错误与调试技巧以及性能优化与最佳实践。1)创建线程使用std::thread类,示例展示了如何创建并等待线程完成。2)同步与互斥使用std::mutex和std::lock_guard保护共享资源,避免数据竞争。3)条件变量通过std::condition_variable实现线程间的通信和同步。4)线程池示例展示了如何使用ThreadPool类并行处理任务,提高效率。5)异步编程使用std::as

C深度潜水:掌握记忆管理,指针和模板C深度潜水:掌握记忆管理,指针和模板Apr 07, 2025 am 12:11 AM

C 的内存管理、指针和模板是核心特性。1.内存管理通过new和delete手动分配和释放内存,需注意堆和栈的区别。2.指针允许直接操作内存地址,使用需谨慎,智能指针可简化管理。3.模板实现泛型编程,提高代码重用性和灵活性,需理解类型推导和特化。

C和系统编程:低级控制和硬件交互C和系统编程:低级控制和硬件交互Apr 06, 2025 am 12:06 AM

C 适合系统编程和硬件交互,因为它提供了接近硬件的控制能力和面向对象编程的强大特性。1)C 通过指针、内存管理和位操作等低级特性,实现高效的系统级操作。2)硬件交互通过设备驱动程序实现,C 可以编写这些驱动程序,处理与硬件设备的通信。

使用C的游戏开发:构建高性能游戏和模拟使用C的游戏开发:构建高性能游戏和模拟Apr 05, 2025 am 12:11 AM

C 适合构建高性能游戏和仿真系统,因为它提供接近硬件的控制和高效性能。1)内存管理:手动控制减少碎片,提高性能。2)编译时优化:内联函数和循环展开提升运行速度。3)低级操作:直接访问硬件,优化图形和物理计算。

C语言文件操作难题的幕后真相C语言文件操作难题的幕后真相Apr 04, 2025 am 11:24 AM

文件操作难题的真相:文件打开失败:权限不足、路径错误、文件被占用。数据写入失败:缓冲区已满、文件不可写、磁盘空间不足。其他常见问题:文件遍历缓慢、文本文件编码不正确、二进制文件读取错误。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境