騎士游歷問題問題_騎士步行問題

騎士游歷問題問題

Problem Statement:

問題陳述:

There is a chessboard of size N×M and starting position (sx, sy) and destination position (dx,dy). You have to find out how many minimum numbers of moves a knight goes to that destination position?

有一個大小為N×M的棋盤,其起始位置(sx,sy)和目標位置(dx,dy) 。 您必須找出一個騎士去那個目的地位置的最小移動次數?

Example:

例:

Input:

輸入:

knight walk

Check the above example...

檢查上面的例子...

If the source position is (5,5) and the destination position is (3,2).
Then the path count is 3.

如果源位置是(5,5)而目標位置是(3,2)
那么路徑計數為3

Algorithm:

算法:

To solve this problem we follow this approach:

為了解決這個問題,我們采用以下方法:

  1. We use queue data structure and initialize it with the starting position and a map data structure to count the steps where the key is position and value is step count.

    我們使用隊列數據結構,并使用起始位置和地圖數據結構對其進行初始化,以計算步數,其中鍵是位置,值是步數。

  2. Then we pop the top position and push all the possible positions that are reached from that position (a knight moves 2 steps at any direction and then one step left or right) and increase the step count of the popped position by one.

    然后,我們彈出頂部位置,并從該位置推動到達所有可能的位置(騎士在任意方向上移動2步,然后向左或向右移動1步),并將彈出位置的步數增加1。

  3. We repeat step 2 until we reach the destination position and the first value is the minimum value.

    重復步驟2,直到到達目標位置,并且第一個值是最小值。

騎士步行問題的C ++實現 (C++ Implementation for Knight walk problem)

#include <bits/stdc++.h>
using namespace std;
int num_x[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
int num_y[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
bool isvalid(int r, int c, int row, int col)
{
return (row >= 0 && row < r && col >= 0 && col < c);
}
int count(int r, int c, int sx, int sy, int dx, int dy)
{
queue<pair<pair<int, int>, int> > q;
set<pair<int, int> > st;
q.push(make_pair(make_pair(sx, sy), 0));
while (!q.empty()) {
pair<pair<int, int>, int> p = q.front();
st.insert(make_pair(sx, sy));
if (p.first.first == dx && p.first.second == dy) {
return p.second;
}
q.pop();
for (int i = 0; i < 8; i++) {
int row = p.first.first + num_x[i];
int col = p.first.second + num_y[i];
if (isvalid(r, c, row, col) && st.find(make_pair(row, col)) == st.end()) {
st.insert(make_pair(row, col));
int temp = p.second + 1;
q.push(make_pair(make_pair(row, col), temp));
}
}
}
return -1;
}
int main()
{
int r, c;
cout << "Row: ";
cin >> r;
cout << "Col: ";
cin >> c;
int sx, sy, dx, dy;
cout << "Staring position : ";
cin >> sx >> sy;
cout << "Destination position : ";
cin >> dx >> dy;
cout << "Steps :";
cout << count(r, c, sx - 1, sy - 1, dx - 1, dy - 1) << endl;
return 0;
}

Output

輸出量

Row: 5
Col: 5
Staring position : 5 5
Destination position : 3 2
Steps :3

翻譯自: https://www.includehelp.com/icp/knight-walk-problem.aspx

騎士游歷問題問題

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377105.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377105.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377105.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android基礎之用Eclipse搭建Android開發環境和創建第一個Android項目(Windows平臺)...

一、搭建Android開發環境 準備工作&#xff1a;下載Eclipse、JDK、Android SDK、ADT插件 下載地址&#xff1a;Eclipse:http://www.eclipse.org/downloads/ JDK&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/jdk7u9-downloads-1859576.html Android SD…

《dp補卡——01背包問題》

目錄01背包[416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum/)[1049. 最后一塊石頭的重量 II](https://leetcode-cn.com/problems/last-stone-weight-ii/)[494. 目標和](https://leetcode-cn.com/problems/target-sum/)01背包 1、dp數組以及…

用JavaScript往DIV動態添加內容

參考&#xff1a;http://zhidao.baidu.com/link?url6jSchyqPiEYCBoKdOmv52YHz9r7MTBms2pK1N6ptOX1kaR2eg320mlW1Sr6n36hpOeOadBxC2rWWGuhZPbms-K <div id"show"></div>要填充的數據為: 這是一個測試例子.jquery&#xff1a;$(function(){ var data …

《dp補卡——完全背包問題》

N件物品和一個最多能背重量為W的背包。第i件物品的重量為weight[i]&#xff0c;得到的價值是value[i]。每件物品都有無限個(可以放入背包多次)&#xff0c;求解將哪些物品裝入背包里物品價值總和最大。 01背包和完全背包唯一不同在于遍歷順序上。 01背包的核心代碼&#xff1a…

Java中的類型轉換

類型轉換 (Typecasting) Typecasting is a term which is introduced in all the language similar to java. Typecasting是一個用與Java類似的所有語言引入的術語。 When we assign primitive datatype to another datatype. 當我們將原始數據類型分配給另一個數據類型時。 I…

讓crash文件中的內存地址變成函數名稱,

假如程序員編譯了inhouse給測試。 如果在測試過程中出現奔潰現象&#xff0c;我想程序員一般會來看Device Log 也就是 crash文件 如果crash文件遇到如下的情況&#xff0c;在重要的地方看不到函數名稱。我想是一件很奔潰的事情。 1 Exception Type: EXC_BAD_ACCESS (SIGSEGV)2…

《dp補卡——多重背包》

多重背包簡介&#xff1a; 有N種物品和一個容量為V的背包。第i種物品最多有Mi件可用&#xff0c;每件耗費的空間為Ci&#xff0c;價值為Wi。求解將哪些物品裝入背包可使得這些物品耗費的空間總和不超過背包容量&#xff0c;且價值總和最大。 將Mi件攤開&#xff0c;就是一個01背…

kafka消息確認ack_什么是確認(ACK)? ACK代表什么?

kafka消息確認ackACK&#xff1a;致謝 (ACK: Acknowledgment) An acknowledgment (ACK) is a signal that is passed among the communicating processes, computers, or devices to indicate acknowledgment, or delivery of the message, as a component of a communications…

CocoaAsyncSocket 套接字

CocoaAsyncSocket 套接字 https://github.com/robbiehanson/CocoaAsyncSocket Asynchronous socket networking library for Mac and iOS 用于iOS以及Mac的異步套接字網絡庫。 TCP GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries. Here are the key…

谷歌瀏覽器設置緩存方法

谷歌瀏覽器設置緩存方法&#xff1a; 1、在桌面Google Chrome快捷方式&#xff0c;目標&#xff1a;找到 C:\Users\Splendid\AppData\Local\…\Application\chrome.exe 在這后面加上-Disk-Cache-Dir”Z:\TEMP” 注意: -Disk前面有空格&#xff0c;”Z:\TEMP” 是文件存放在Z盤T…

《dp補卡——買賣股票問題》

目錄121. 買賣股票的最佳時機貪心dp思路滾動數組優化122. 買賣股票的最佳時機 II123. 買賣股票的最佳時機 III188. 買賣股票的最佳時機 IV309. 最佳買賣股票時機含冷凍期714. 買賣股票的最佳時機含手續費121. 買賣股票的最佳時機 貪心 取最左最小值&#xff0c;取最右最大值&…

oo0ooo0ooo0oo_OoO的完整形式是什么?

oo0ooo0ooo0ooOoO&#xff1a;外出 (OoO: Out of Office) OoO is an abbreviation of "Out of Office". OoO是“不在辦公室”的縮寫。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email…

SP2010開發和VS2010專家食譜--第三章節--高級工作流(2)--為沙盒解決方案創建自定義活動...

盡管沙河解決方案功能有限&#xff0c;你仍然可以開發自定義活動&#xff0c;在SharePoint Designer中使用而不用改變web.config或添加.ACTION文件到根文件夾。 轉載于:https://www.cnblogs.com/crazygolf/p/3856795.html

sql where 1=1和 0=1 的作用

where 11; 這個條件始終為True&#xff0c;在不定數量查詢條件情況下&#xff0c;11可以很方便的規范語句。 一、不用where 11 在多條件查詢中的困擾 舉個例子&#xff0c;如果您做查詢頁面&#xff0c;并且&#xff0c;可查詢的選項有多個&#xff0c;同時&#xff0c;還讓用戶…

j@2ff4f00f_J4F的完整形式是什么?

j2ff4f00fJ4F&#xff1a;只是為了好玩 (J4F: Just For Fun) J4F is an abbreviation of "Just For Fun". J4F是“ Just For Fun”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceboo…

《dp補卡——子序列問題》

目錄300. 最長遞增子序列674. 最長連續遞增序列718. 最長重復子數組1143. 最長公共子序列53. 最大子序和392. 判斷子序列115. 不同的子序列583. 兩個字符串的刪除操作72. 編輯距離647. 回文子串 &#xff08;與 5.最長回文子串思路差不多&#xff09;516. 最長回文子序列300. 最…

[LeetCode] Maximal Rectangle

Given a 2D binary matrix filled with 0s and 1s, find the largest rectangle containing all ones and return its area. 在做 Largest Rectangle in Histogram的時候有人說可以用在這題&#xff0c;看了一下還真是&#xff0c;以每行為x軸&#xff0c;每列往上累計的連續的…

什么是alpha測試_什么是ALPHA?

什么是alpha測試Α (ALPHA) Alpha is the first and foremost letter of the Greek alphabet. In the classification of Greek numerals or numbers, it constitutes a value of 1. Alpha是希臘字母的第一個也是最重要的字母 。 在希臘數字或希臘數字的分類中&#xff0c;它的…

《leetcode : 647. 回文子串 思考分析雙指針解法》

647. 回文子串 如何確定是回文串&#xff1a; 找中心然后往兩邊擴散&#xff0c;判斷是否對稱即可。 在遍歷中心點的時候&#xff0c;注意中心點可以是一個元素也可以是兩個元素。 class Solution { public:int cal_two_extend(const string& s,int i,int j,int n){int re…

天草初級班(3)

算術運算指令算術運算指令是反映CPU計算能力的一組指令&#xff0c;也是編程時經常使用的一組指令。它包括&#xff1a;加、減、乘、除及其相關的輔助指令。 該組指令的操作數可以是8位、16位和32位(80386)。當存儲單元是該類指令的操作數時&#xff0c;該操作數的尋址方式可以…