假設我們有一個 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 '.', 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] == '.') { 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]) << '\n'; } 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中文網其他相關文章!

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

C#和C 的学习曲线和开发者体验有显著差异。1)C#的学习曲线较平缓,适合快速开发和企业级应用。2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C#和C 在面向对象编程(OOP)中的实现方式和特性上有显著差异。1)C#的类定义和语法更为简洁,支持如LINQ等高级特性。2)C 提供更细粒度的控制,适用于系统编程和高性能需求。两者各有优势,选择应基于具体应用场景。

從XML轉換到C 並進行數據操作可以通過以下步驟實現:1)使用tinyxml2庫解析XML文件,2)將數據映射到C 的數據結構中,3)使用C 標準庫如std::vector進行數據操作。通過這些步驟,可以高效地處理和操作從XML轉換過來的數據。

C#使用自動垃圾回收機制,而C 採用手動內存管理。 1.C#的垃圾回收器自動管理內存,減少內存洩漏風險,但可能導致性能下降。 2.C 提供靈活的內存控制,適合需要精細管理的應用,但需謹慎處理以避免內存洩漏。

C 在現代編程中仍然具有重要相關性。 1)高性能和硬件直接操作能力使其在遊戲開發、嵌入式系統和高性能計算等領域佔據首選地位。 2)豐富的編程範式和現代特性如智能指針和模板編程增強了其靈活性和效率,儘管學習曲線陡峭,但其強大功能使其在今天的編程生態中依然重要。

C 學習者和開發者可以從StackOverflow、Reddit的r/cpp社區、Coursera和edX的課程、GitHub上的開源項目、專業諮詢服務以及CppCon等會議中獲得資源和支持。 1.StackOverflow提供技術問題的解答;2.Reddit的r/cpp社區分享最新資訊;3.Coursera和edX提供正式的C 課程;4.GitHub上的開源項目如LLVM和Boost提陞技能;5.專業諮詢服務如JetBrains和Perforce提供技術支持;6.CppCon等會議有助於職業

C#適合需要高開發效率和跨平台支持的項目,而C 適用於需要高性能和底層控制的應用。 1)C#簡化開發,提供垃圾回收和豐富類庫,適合企業級應用。 2)C 允許直接內存操作,適用於遊戲開發和高性能計算。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境