回溯算法和貪心算法_回溯算法介紹

回溯算法和貪心算法

回溯算法 (Backtracking Algorithms)

Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems. It incrementally builds candidates to the solutions, and abandons each partial candidate (“backtracks”) as soon as it determines that the candidate cannot possibly be completed to a valid solution.

回溯是一種通用算法,用于查找某些計算問題(尤其是約束滿足問題)的所有(或某些)解決方案。 它逐步為解決方案構建候選對象,并在確定候選對象不可能完成有效的解決方案后立即放棄每個部分候選對象(“回溯”)

示例問題(騎士的旅行問題) (Example Problem (The Knight’s tour problem))

The knight is placed on the first block of an empty board and, moving according to the rules of chess, must visit each square exactly once.

騎士被放置在一個空棋盤的第一塊上,并且根據國際象棋的規則移動,必須對每個廣場精確地訪問一次。

騎士走過的路覆蓋了所有牢房 (Path followed by Knight to cover all the cells)

Following is chessboard with 8 x 8 cells. Numbers in cells indicate move number of Knight.

以下是帶有8 x 8格的棋盤。 單元格中的數字表示騎士的移動次數。

騎士之旅的樸素算法 (Naive Algorithm for Knight’s tour)

The Naive Algorithm is to generate all tours one by one and check if the generated tour satisfies the constraints.

樸素算法是一一生成所有巡視,并檢查生成的巡視是否滿足約束條件。

while there are untried tours
{ generate the next tour if this tour covers all squares { print this path;}
}

騎士之旅的回溯算法 (Backtracking Algorithm for Knight’s tour)

Following is the Backtracking algorithm for Knight’s tour problem.

以下是騎士巡回問題的回溯算法。

If all squares are visited print the solution
Elsea) Add one of the next moves to solution vector and recursively check if this move leads to a solution. (A Knight can make maximum eight moves. We choose one of the 8 moves in this step).b) If the move chosen in the above step doesn't lead to a solutionthen remove this move from the solution vector and try other alternative moves.c) If none of the alternatives work then return false (Returning false will remove the previously added item in recursion and if false is returned by the initial call of recursion then "no solution exists" )

翻譯自: https://www.freecodecamp.org/news/backtracking-algorithms-explained/

回溯算法和貪心算法

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

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

相關文章

alpha沖刺day8

項目進展 李明皇 昨天進展 編寫完個人中心頁面今天安排 編寫首頁邏輯層問題困難 開始編寫數據傳遞邏輯,要用到列表渲染和條件渲染心得體會 小程序框架設計的內容有點忘了,而且比較抽象,需要理解文檔舉例和具體案例林翔 昨天進展 黑名單用戶的…

增加 processon 免費文件數

github 地址:github.com/96chh/Upgra… 關于 ProcessOn 非常好用的思維導圖網站,不僅支持思維導圖,還支持流程圖、原型圖、UML 等。比我之前用的百度腦圖強多了。 直接登錄網站就可以編輯,非常適合我在圖書館公用電腦學習使用。 但…

uni-app清理緩存數據_數據清理-從哪里開始?

uni-app清理緩存數據It turns out that Data Scientists and Data Analysts will spend most of their time on data preprocessing and EDA rather than training a machine learning model. As one of the most important job, Data Cleansing is very important indeed.事實…

高級人工智能之群體智能:蟻群算法

群體智能 鳥群: 魚群: 1.基本介紹 蟻群算法(Ant Colony Optimization, ACO)是一種模擬自然界螞蟻覓食行為的優化算法。它通常用于解決路徑優化問題,如旅行商問題(TSP)。 蟻群算法的基本步驟…

JavaScript標準對象:地圖

The Map object is a relatively new standard built-in object that holds [key, value] pairs in the order that theyre inserted. Map對象是一個相對較新的標準內置對象,按插入順序保存[key, value]對。 The keys and values in the Map object can be any val…

leetcode 483. 最小好進制

題目 對于給定的整數 n, 如果n的k(k>2)進制數的所有數位全為1,則稱 k(k>2)是 n 的一個好進制。 以字符串的形式給出 n, 以字符串的形式返回 n 的最小好進制。 示例 1: 輸入:“13” 輸…

圖像灰度變換及圖像數組操作

Python圖像灰度變換及圖像數組操作 作者:MingChaoSun 字體:[增加 減小] 類型:轉載 時間:2016-01-27 我要評論 這篇文章主要介紹了Python圖像灰度變換及圖像數組操作的相關資料,需要的朋友可以參考下使用python以及numpy通過直接操…

npx npm區別_npm vs npx —有什么區別?

npx npm區別If you’ve ever used Node.js, then you must have used npm for sure.如果您曾經使用過Node.js ,那么一定要使用npm 。 npm (node package manager) is the dependency/package manager you get out of the box when you install Node.js. It provide…

找出性能消耗是第一步,如何解決問題才是關鍵

作者最近剛接手一個新項目,在首頁列表滑動時就感到有點不順暢,特別是在滑動到有 ViewPager 部分的時候,如果是熟悉的項目,可能會第一時間會去檢查代碼,但前面說到這個是剛接手的項目,同時首頁的代碼邏輯比較…

bigquery_如何在BigQuery中進行文本相似性搜索和文檔聚類

bigqueryBigQuery offers the ability to load a TensorFlow SavedModel and carry out predictions. This capability is a great way to add text-based similarity and clustering on top of your data warehouse.BigQuery可以加載TensorFlow SavedModel并執行預測。 此功能…

bzoj 1996: [Hnoi2010]chorus 合唱隊

Description 為了在即將到來的晚會上有吏好的演出效果&#xff0c;作為AAA合唱隊負責人的小A需要將合唱隊的人根據他們的身高排出一個隊形。假定合唱隊一共N個人&#xff0c;第i個人的身髙為Hi米(1000<Hi<2000),并已知任何兩個人的身高都不同。假定最終排出的隊形是A 個人…

移動應用程序開發_什么是移動應用程序開發?

移動應用程序開發One of the most popular forms of coding in the last decade has been the creation of apps, or applications, that run on mobile devices.在過去的十年中&#xff0c;最流行的編碼形式之一是創建在移動設備上運行的應用程序。 Today there are two main…

leetcode 1600. 皇位繼承順序(dfs)

題目 一個王國里住著國王、他的孩子們、他的孫子們等等。每一個時間點&#xff0c;這個家庭里有人出生也有人死亡。 這個王國有一個明確規定的皇位繼承順序&#xff0c;第一繼承人總是國王自己。我們定義遞歸函數 Successor(x, curOrder) &#xff0c;給定一個人 x 和當前的繼…

vlookup match_INDEX-MATCH — VLOOKUP功能的升級

vlookup match電子表格/索引匹配 (SPREADSHEETS / INDEX-MATCH) In a previous article, we discussed about how and when to use VLOOKUP functions and what are the issues that we might face while using them. This article, on the other hand, will take you to a jou…

java基礎-BigDecimal類常用方法介紹

java基礎-BigDecimal類常用方法介紹 作者&#xff1a;尹正杰 版權聲明&#xff1a;原創作品&#xff0c;謝絕轉載&#xff01;否則將追究法律責任。 一.BigDecimal類概述 我們知道浮點數的計算結果是未知的。原因是計算機二進制中&#xff0c;表示浮點數不精確造成的。這個時候…

節點對象轉節點_節點流程對象說明

節點對象轉節點The process object in Node.js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node.js and process is one of them. It is an essential component in the …

PAT——1018. 錘子剪刀布

大家應該都會玩“錘子剪刀布”的游戲&#xff1a;兩人同時給出手勢&#xff0c;勝負規則如圖所示&#xff1a; 現給出兩人的交鋒記錄&#xff0c;請統計雙方的勝、平、負次數&#xff0c;并且給出雙方分別出什么手勢的勝算最大。 輸入格式&#xff1a; 輸入第1行給出正整數N&am…

leetcode 1239. 串聯字符串的最大長度

題目 二進制手表頂部有 4 個 LED 代表 小時&#xff08;0-11&#xff09;&#xff0c;底部的 6 個 LED 代表 分鐘&#xff08;0-59&#xff09;。每個 LED 代表一個 0 或 1&#xff0c;最低位在右側。 例如&#xff0c;下面的二進制手表讀取 “3:25” 。 &#xff08;圖源&am…

flask redis_在Flask應用程序中將Redis隊列用于異步任務

flask redisBy: Content by Edward Krueger and Josh Farmer, and Douglas Franklin.作者&#xff1a; 愛德華克魯格 ( Edward Krueger) 和 喬什法默 ( Josh Farmer )以及 道格拉斯富蘭克林 ( Douglas Franklin)的內容 。 When building an application that performs time-co…

CentOS7下分布式文件系統FastDFS的安裝 配置 (單節點)

背景 FastDFS是一個開源的輕量級分布式文件系統&#xff0c;為互聯網量身定制&#xff0c;充分考慮了冗余備份、負載均衡、線性擴容等機制&#xff0c;并注重高可用、高性能等指標&#xff0c;解決了大容量存儲和負載均衡的問題&#xff0c;特別適合以文件為載體的在線服務&…