{"id":17654,"date":"2024-10-25T16:35:19","date_gmt":"2024-10-25T08:35:19","guid":{"rendered":"http:\/\/192.168.10.115\/?p=17654"},"modified":"2024-10-25T16:35:19","modified_gmt":"2024-10-25T08:35:19","slug":"2024-10-25-%e8%b7%af%e5%be%84%e8%a7%84%e5%88%92%e7%ae%97%e6%b3%95-a-%e6%90%9c%e7%b4%a2%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"http:\/\/222.128.65.89:18086\/index.php\/2024\/10\/25\/17654\/","title":{"rendered":"2024-10-25 \u8def\u5f84\u89c4\u5212\u7b97\u6cd5 | A* \u641c\u7d22\u7b97\u6cd5"},"content":{"rendered":"\n<p>\u52a8\u673a\uff1a\u4e3a\u4e86\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u8fd1\u4f3c\u6c42\u89e3\u6700\u77ed\u8def\u5f84\uff0c\u4f8b\u5982\u5730\u56fe\u3001\u6e38\u620f\u7b49\u5b58\u5728\u8bb8\u591a\u969c\u788d\u7269\u7684\u60c5\u51b5\u3002\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4e00\u4e2a\u542b\u6709\u591a\u4e2a\u969c\u788d\u7269\u7684\u4e8c\u7ef4\u7f51\u683c\u56fe\uff0c\u6211\u4eec\u4ece\u8d77\u59cb\u5355\u5143\u683c\uff08\u4e0b\u65b9\u7ea2\u8272\u6807\u8bb0\uff09\u5f00\u59cb\uff0c\u671d\u7740\u76ee\u6807\u5355\u5143\u683c\uff08\u4e0b\u65b9\u7eff\u8272\u6807\u8bb0\uff09\u524d\u8fdb\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"679\" height=\"358\" src=\"http:\/\/192.168.10.115\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-374.png\" alt=\"\" class=\"wp-image-17656\" srcset=\"http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-374.png 679w, http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-374-300x158.png 300w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><\/figure>\n\n\n\n<p><br>01 \u4ec0\u4e48\u662fA*\u641c\u7d22\u7b97\u6cd5<\/p>\n\n\n\n<p>A*\u641c\u7d22\u7b97\u6cd5\u662f\u4e00\u79cd\u7528\u4e8e\u8def\u5f84\u641c\u7d22\u548c\u56fe\u904d\u5386\u7684\u6548\u679c\u5f88\u597d\u3001\u4e3b\u6d41\u7684\u6280\u672f\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p>1.1 \u4e3a\u4ec0\u4e48\u9009\u62e9A*\u641c\u7d22\u7b97\u6cd5\uff1f<\/p>\n\n\n\n<p>\u7b80\u5355\u5730\u8bf4\uff0cA*\u641c\u7d22\u7b97\u6cd5\u4e0e\u5176\u4ed6\u904d\u5386\u6280\u672f\u4e0d\u540c\uff0c\u5b83\u5177\u6709\u201c\u667a\u80fd\u201d\u3002\u8fd9\u610f\u5473\u7740\u5b83\u662f\u4e00\u79cd\u975e\u5e38\u667a\u80fd\u7684\u7b97\u6cd5\uff0c\u4e0e\u5176\u4ed6\u4f20\u7edf\u7b97\u6cd5\u6709\u6240\u533a\u522b\u3002\u4e0b\u9762\u7684\u90e8\u5206\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u8bb8\u591a\u6e38\u620f\u548c\u57fa\u4e8eWeb\u7684\u5730\u56fe\u4f7f\u7528\u8fd9\u4e2a\u7b97\u6cd5\u6765\u9ad8\u6548\u5730\u627e\u5230\u6700\u77ed\u8def\u5f84\uff08\u8fd1\u4f3c\uff09\u3002<\/p>\n\n\n\n<p>1.2 \u89e3\u91ca<\/p>\n\n\n\n<p>\u8003\u8651\u4e00\u4e2a\u6709\u8bb8\u591a\u969c\u788d\u7269\u7684\u6b63\u65b9\u5f62\u7f51\u683c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u8d77\u59cb\u5355\u5143\u683c\u548c\u4e00\u4e2a\u76ee\u6807\u5355\u5143\u683c\u3002\u6211\u4eec\u5e0c\u671b\u5c3d\u5feb\u4ece\u8d77\u59cb\u5355\u5143\u683c\u5230\u8fbe\u76ee\u6807\u5355\u5143\u683c\uff08\u5982\u679c\u53ef\u80fd\uff09\u3002\u8fd9\u65f6A*\u641c\u7d22\u7b97\u6cd5\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002<\/p>\n\n\n\n<p>A*\u641c\u7d22\u7b97\u6cd5\u5728\u6bcf\u4e00\u6b65\u4e2d\u9009\u62e9\u4e00\u4e2a\u8282\u70b9\uff0c\u6839\u636e\u4e00\u4e2a\u503cf\u6765\u786e\u5b9a\uff0c\u8be5\u503c\u662f\u4e24\u4e2a\u5176\u4ed6\u53c2\u6570g\u548ch\u7684\u51fd\u6570\u3002\u5728\u6bcf\u4e00\u6b65\u4e2d\uff0c\u5b83\u9009\u62e9\u5177\u6709\u6700\u4f4ef\u503c\u7684\u8282\u70b9\/\u5355\u5143\u683c\uff0c\u5e76\u5904\u7406\u8be5\u8282\u70b9\/\u5355\u5143\u683c\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06g\u548ch\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>g\uff1a\u4ece\u8d77\u70b9\u5230\u7f51\u683c\u4e0a\u7684\u67d0\u4e2a\u7ed9\u5b9a\u65b9\u683c\u7684\u79fb\u52a8\u6210\u672c\uff0c\u6cbf\u7740\u751f\u6210\u7684\u8def\u5f84\u8fdb\u884c\u79fb\u52a8\u3002<\/p>\n\n\n\n<p>h\uff1a\u4ece\u7ed9\u5b9a\u65b9\u683c\u5230\u6700\u7ec8\u76ee\u7684\u5730\u7684\u4f30\u8ba1\u79fb\u52a8\u6210\u672c\u3002\u8fd9\u901a\u5e38\u88ab\u79f0\u4e3a\u542f\u53d1\u5f0f\uff0c\u5b83\u53ea\u662f\u4e00\u79cd\u806a\u660e\u7684\u731c\u6d4b\u3002\u5728\u627e\u5230\u8def\u5f84\u4e4b\u524d\uff0c\u6211\u4eec\u771f\u7684\u4e0d\u77e5\u9053\u5b9e\u9645\u8ddd\u79bb\uff0c\u56e0\u4e3a\u5404\u79cd\u4e1c\u897f\u53ef\u80fd\u963b\u4f1a\u59a8\u788d\u89c4\u5212\u7684\u8def\u5f84\uff08\u5899\u58c1\u3001\u6c34\u7b49\uff09\u3002\u6709\u8bb8\u591a\u8ba1\u7b97\u8fd9\u4e2ah\u503c\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5728\u540e\u9762\u7684\u90e8\u5206\u4e2d\u8fdb\u884c\u4e86\u8ba8\u8bba\u3002<br>02 \u7b97\u6cd5<\/p>\n\n\n\n<p>\u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u5217\u8868 &#8211; \u5f00\u653e\u5217\u8868\uff08Open list)\u548c\u5c01\u95ed\u5217\u8868(Closed list)\uff08\u5c31\u50cfDijkstra\u7b97\u6cd5\u4e00\u6837\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ A* Search Algorithm\n1.  Initialize the open list\n2.  Initialize the closed list\n    put the starting node on the open\n    list (you can leave its f at zero)\n3.  while the open list is not empty\n    a) find the node with the least f on\n       the open list, call it \"q\"\n    b) pop q off the open list\n\n    c) generate q's 8 successors and set their\n       parents to q\n\n    d) for each successor\n        i) if successor is the goal, stop search\n\n        ii) else, compute both g and h for successor\n          successor.g = q.g + distance between\n                              successor and q\n          successor.h = distance from goal to\n          successor (This can be done using many\n          ways, we will discuss three heuristics-\n          Manhattan, Diagonal and Euclidean\n          Heuristics)\n\n          successor.f = successor.g + successor.h\n        iii) if a node with the same position as\n            successor is in the OPEN list which has a\n           lower f than successor, skip this successor\n        iV) if a node with the same position as\n            successor  is in the CLOSED list which has\n            a lower f than successor, skip this successor\n            otherwise, add  the node to the open list\n     end (for loop)\n\n    e) push q on the closed list\n    end (while loop) <\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u5047\u8bbe\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u4ece\u8d77\u59cb\u5355\u5143\u683c\u5230\u8fbe\u76ee\u6807\u5355\u5143\u683c\uff0cA*\u641c\u7d22\u7b97\u6cd5\u5c06\u6309\u7167\u4e0b\u56fe\u6240\u793a\u7684\u8def\u5f84\u8fdb\u884c\u641c\u7d22\u3002\u8bf7\u6ce8\u610f\uff0c\u4e0b\u56fe\u662f\u6839\u636e\u6b27\u51e0\u91cc\u5fb7\u8ddd\u79bb\u4f5c\u4e3a\u542f\u53d1\u5f0f\u7b97\u6cd5\u751f\u6210\u7684\u3002<br>03 \u542f\u53d1\u5f0f\u7b97\u6cd5<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97g\uff0c\u4f46\u5982\u4f55\u8ba1\u7b97h\u5462\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u65b9\u6cd5\uff1aA) \u8981\u4e48\u8ba1\u7b97h\u7684\u7cbe\u786e\u503c\uff08\u8fd9\u80af\u5b9a\u662f\u8017\u65f6\u7684\uff09\uff0c\u6216\u8005 B) \u4f7f\u7528\u67d0\u4e9b\u542f\u53d1\u5f0f\u65b9\u6cd5\u6765\u8fd1\u4f3c\u8ba1\u7b97h\uff08\u65f6\u95f4\u6d88\u8017\u8f83\u5c11\uff09\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u8ba8\u8bba\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>3.1 \u7cbe\u786e\u542f\u53d1\u5f0f<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u627e\u5230h\u7684\u7cbe\u786e\u503c\uff0c\u4f46\u901a\u5e38\u8fd9\u9700\u8981\u5f88\u957f\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8ba1\u7b97h\u7cbe\u786e\u503c\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>1) \u5728\u8fd0\u884cA*\u641c\u7d22\u7b97\u6cd5\u4e4b\u524d\uff0c\u9884\u5148\u8ba1\u7b97\u6bcf\u5bf9\u5355\u5143\u683c\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n\n\n\n<p>2) \u5982\u679c\u6ca1\u6709\u963b\u585e\u5355\u5143\u683c\uff08\u969c\u788d\u7269\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8ddd\u79bb\u516c\u5f0f\/\u6b27\u51e0\u91cc\u5fb7\u8ddd\u79bb\uff0c\u5728\u4e0d\u8fdb\u884c\u4efb\u4f55\u9884\u5148\u8ba1\u7b97\u7684\u60c5\u51b5\u4e0b\u627e\u5230h\u7684\u7cbe\u786e\u503c\u3002<\/p>\n\n\n\n<p>3.2 \u8fd1\u4f3c\u542f\u53d1\u5f0f<\/p>\n\n\n\n<p>\u901a\u5e38\u6709\u4e09\u79cd\u8fd1\u4f3c\u542f\u53d1\u5f0f\u65b9\u6cd5\u6765\u8ba1\u7b97h\uff1a<\/p>\n\n\n\n<p>1) \u66fc\u54c8\u987f\u8ddd\u79bb\uff1a<\/p>\n\n\n\n<p>\u00b7 \u5b83\u662f\u76ee\u6807\u70b9\u7684x\u5750\u6807\u548cy\u5750\u6807\u4e0e\u5f53\u524d\u5355\u5143\u683c\u7684x\u5750\u6807\u548cy\u5750\u6807\u4e4b\u95f4\u5dee\u503c\u7684\u7edd\u5bf9\u503c\u4e4b\u548c\uff0c\u5373\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h = abs (current_cell.x \u2013 goal.x) + abs (current_cell.y \u2013 goal.y)<\/code><\/pre>\n\n\n\n<p>\u00b7 \u5f53\u53ea\u5141\u8bb8\u5728\u56db\u4e2a\u65b9\u5411\u4e0a\u79fb\u52a8\uff08\u53f3\u3001\u5de6\u3001\u4e0a\u3001\u4e0b\uff09\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u542f\u53d1\u5f0f\u7b97\u6cd5\u3002\u66fc\u54c8\u987f\u8ddd\u79bb\u542f\u53d1\u5f0f\u7b97\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e0b\u56fe\u8868\u793a\uff08\u5047\u8bbe\u7ea2\u70b9\u4e3a\u8d77\u59cb\u5355\u5143\u683c\uff0c\u7eff\u70b9\u4e3a\u76ee\u6807\u5355\u5143\u683c\uff09\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"366\" src=\"http:\/\/192.168.10.115\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-375.png\" alt=\"\" class=\"wp-image-17657\" srcset=\"http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-375.png 685w, http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-375-300x160.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure>\n\n\n\n<p>2) \u5bf9\u89d2\u7ebf\u8ddd\u79bb\uff1a<\/p>\n\n\n\n<p>\u00b7 \u5b83\u662f\u76ee\u6807\u70b9\u7684x\u5750\u6807\u548cy\u5750\u6807\u4e0e\u5f53\u524d\u5355\u5143\u683c\u7684x\u5750\u6807\u548cy\u5750\u6807\u4e4b\u95f4\u5dee\u503c\u7684\u7edd\u5bf9\u503c\u7684\u6700\u5927\u503c\uff0c\u5373\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dx = abs(current_cell.x \u2013 goal.x)\ndy = abs(current_cell.y \u2013 goal.y)\nh = D * (dx + dy) + (D2 - 2 * D) * min(dx, dy)\nwhere D is length of each node(usually = 1) and D2 is diagonal distance between each node (usually = sqrt(2) ).<\/code><\/pre>\n\n\n\n<p>\u00b7 \u5f53\u53ea\u5141\u8bb8\u5728\u516b\u4e2a\u65b9\u5411\u4e0a\u79fb\u52a8\u65f6\uff08\u7c7b\u4f3c\u4e8e\u56fd\u9645\u8c61\u68cb\u4e2d\u7684\u56fd\u738b\u79fb\u52a8\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u542f\u53d1\u5f0f\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>\u5bf9\u89d2\u7ebf\u8ddd\u79bb\u542f\u53d1\u5f0f\u7b97\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e0b\u56fe\u8868\u793a\uff08\u5047\u8bbe\u7ea2\u70b9\u4e3a\u8d77\u59cb\u5355\u5143\u683c\uff0c\u7eff\u70b9\u4e3a\u76ee\u6807\u5355\u5143\u683c\uff09\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"366\" src=\"http:\/\/192.168.10.115\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-376.png\" alt=\"\" class=\"wp-image-17658\" srcset=\"http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-376.png 686w, http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-376-300x160.png 300w\" sizes=\"(max-width: 686px) 100vw, 686px\" \/><\/figure>\n\n\n\n<p>3) \u6b27\u51e0\u91cc\u5fb7\u8ddd\u79bb\uff1a<\/p>\n\n\n\n<p>\u00b7 \u987e\u540d\u601d\u4e49\uff0c\u5b83\u5c31\u662f\u4f7f\u7528\u8ddd\u79bb\u516c\u5f0f\u8ba1\u7b97\u5f53\u524d\u5355\u5143\u683c\u4e0e\u76ee\u6807\u5355\u5143\u683c\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h = sqrt ( (current_cell.x \u2013 goal.x)2 + (current_cell.y \u2013 goal.y)2 )<\/code><\/pre>\n\n\n\n<p>\u00b7 \u8fd9\u4e2a\u542f\u53d1\u5f0f\u7b97\u6cd5\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u5462\uff1f- \u5f53\u6211\u4eec\u88ab\u5141\u8bb8\u5728\u4efb\u610f\u65b9\u5411\u4e0a\u79fb\u52a8\u65f6\u3002<\/p>\n\n\n\n<p>\u6b27\u51e0\u91cc\u5fb7\u8ddd\u79bb\u542f\u53d1\u5f0f\u7b97\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e0b\u56fe\u8868\u793a\uff08\u5047\u8bbe\u7ea2\u70b9\u4e3a\u8d77\u59cb\u5355\u5143\u683c\uff0c\u7eff\u70b9\u4e3a\u76ee\u6807\u5355\u5143\u683c\uff09\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"365\" src=\"http:\/\/192.168.10.115\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-377.png\" alt=\"\" class=\"wp-image-17659\" srcset=\"http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-377.png 685w, http:\/\/222.128.65.89:18086\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-377-300x160.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure>\n\n\n\n<p>\u4e0e\u5176\u4ed6\u7b97\u6cd5\u7684\u5173\u7cfb\uff08\u76f8\u4f3c\u6027\u548c\u5dee\u5f02\uff09\uff1aDijkstra\u7b97\u6cd5\u662fA*\u641c\u7d22\u7b97\u6cd5\u7684\u7279\u4f8b\uff0c\u5176\u4e2d\u6240\u6709\u8282\u70b9\u7684h\u503c\u90fd\u4e3a0\u3002<br>04 \u5b9e\u73b0<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u5f00\u653e\u5217\u8868\u548c\u5c01\u95ed\u5217\u8868\uff0c\u4f46\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u6027\u80fd\uff0c\u6211\u4eec\u4f7f\u7528C++ STL\u4e2d\u7684\u96c6\u5408\u6570\u636e\u7ed3\u6784\uff08\u5b9e\u73b0\u4e3a\u7ea2\u9ed1\u6811\uff09\u548c\u4e00\u4e2a\u5e03\u5c14\u54c8\u5e0c\u8868\u7528\u4e8e\u5c01\u95ed\u5217\u8868\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u4e0eDijkstra\u7b97\u6cd5\u7c7b\u4f3c\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u6590\u6ce2\u90a3\u5951\u5806\u6765\u5b9e\u73b0\u5f00\u653e\u5217\u8868\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u4e8c\u53c9\u5806\/\u81ea\u5e73\u8861\u6811\uff0c\u90a3\u4e48\u6027\u80fd\u5c06\u4f1a\u66f4\u597d\uff08\u56e0\u4e3a\u6590\u6ce2\u90a3\u5951\u5806\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u9700\u8981O(1)\u7684\u65f6\u95f4\u6765\u63d2\u5165\u5230\u5f00\u653e\u5217\u8868\u5e76\u51cf\u5c0f\u952e\u503c\uff09\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u4e3a\u4e86\u51cf\u5c11\u8ba1\u7b97g\u6240\u9700\u7684\u65f6\u95f4\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u3002<\/p>\n\n\n\n<p>\/\/ A C++ Program to implement A* Search Algorithm<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<p>using namespace std;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">define ROW 9<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">define COL 10<\/h1>\n\n\n\n<p>\/\/ Creating a shortcut for int, int pair type<br>typedef pair Pair;<\/p>\n\n\n\n<p>\/\/ Creating a shortcut for pair&gt; type<br>typedef pair &gt; pPair;<\/p>\n\n\n\n<p>\/\/ A structure to hold the necessary parameters<br>struct cell {<br>\/\/ Row and Column index of its parent<br>\/\/ Note that 0 &lt;= i &lt;= ROW-1 &amp; 0 &lt;= j &lt;= COL-1<br>int parent_i, parent_j;<br>\/\/ f = g + h<br>double f, g, h;<br>};<\/p>\n\n\n\n<p>\/\/ A Utility Function to check whether given cell (row, col)<br>\/\/ is a valid cell or not.<br>bool isValid(int row, int col)<br>{<br>\/\/ Returns true if row number and column number<br>\/\/ is in range<br>return (row &gt;= 0) &amp;&amp; (row &lt; ROW) &amp;&amp; (col &gt;= 0)<br>&amp;&amp; (col &lt; COL);<br>}<\/p>\n\n\n\n<p>\/\/ A Utility Function to check whether the given cell is<br>\/\/ blocked or not<br>bool isUnBlocked(int grid[][COL], int row, int col)<br>{<br>\/\/ Returns true if the cell is not blocked else false<br>if (grid[row][col] == 1)<br>return (true);<br>else<br>return (false);<br>}<\/p>\n\n\n\n<p>\/\/ A Utility Function to check whether destination cell has<br>\/\/ been reached or not<br>bool isDestination(int row, int col, Pair dest)<br>{<br>if (row == dest.first &amp;&amp; col == dest.second)<br>return (true);<br>else<br>return (false);<br>}<\/p>\n\n\n\n<p>\/\/ A Utility Function to calculate the &#8216;h&#8217; heuristics.<br>double calculateHValue(int row, int col, Pair dest)<br>{<br>\/\/ Return using the distance formula<br>return ((double)sqrt(<br>(row &#8211; dest.first) * (row &#8211; dest.first)<br>+ (col &#8211; dest.second) * (col &#8211; dest.second)));<br>}<\/p>\n\n\n\n<p>\/\/ A Utility Function to trace the path from the source<br>\/\/ to destination<br>void tracePath(cell cellDetails[][COL], Pair dest)<br>{<br>printf(&#8220;\\nThe Path is &#8220;);<br>int row = dest.first;<br>int col = dest.second;<\/p>\n\n\n\n<p>stack Path;<\/p>\n\n\n\n<p>while (!(cellDetails[row][col].parent_i == row<br>&amp;&amp; cellDetails[row][col].parent_j == col)) {<br>Path.push(make_pair(row, col));<br>int temp_row = cellDetails[row][col].parent_i;<br>int temp_col = cellDetails[row][col].parent_j;<br>row = temp_row;<br>col = temp_col;<br>}<\/p>\n\n\n\n<p>Path.push(make_pair(row, col));<br>while (!Path.empty()) {<br>pair p = Path.top();<br>Path.pop();<br>printf(&#8220;-&gt; (%d,%d) &#8220;, p.first, p.second);<br>}<\/p>\n\n\n\n<p>return;<br>}<\/p>\n\n\n\n<p>\/\/ A Function to find the shortest path between<br>\/\/ a given source cell to a destination cell according<br>\/\/ to A* Search Algorithm<br>void aStarSearch(int grid[][COL], Pair src, Pair dest)<br>{<br>\/\/ If the source is out of range<br>if (isValid(src.first, src.second) == false) {<br>printf(&#8220;Source is invalid\\n&#8221;);<br>return;<br>}<\/p>\n\n\n\n<p>\/\/ If the destination is out of range<br>if (isValid(dest.first, dest.second) == false) {<br>printf(&#8220;Destination is invalid\\n&#8221;);<br>return;<br>}<\/p>\n\n\n\n<p>\/\/ Either the source or the destination is blocked<br>if (isUnBlocked(grid, src.first, src.second) == false<br>|| isUnBlocked(grid, dest.first, dest.second)<br>== false) {<br>printf(&#8220;Source or the destination is blocked\\n&#8221;);<br>return;<br>}<\/p>\n\n\n\n<p>\/\/ If the destination cell is the same as source cell<br>if (isDestination(src.first, src.second, dest)<br>== true) {<br>printf(&#8220;We are already at the destination\\n&#8221;);<br>return;<br>}<\/p>\n\n\n\n<p>\/\/ Create a closed list and initialise it to false which<br>\/\/ means that no cell has been included yet This closed<br>\/\/ list is implemented as a boolean 2D array<br>bool closedList[ROW][COL];<br>memset(closedList, false, sizeof(closedList));<\/p>\n\n\n\n<p>\/\/ Declare a 2D array of structure to hold the details<br>\/\/ of that cell<br>cell cellDetails[ROW][COL];<\/p>\n\n\n\n<p>int i, j;<\/p>\n\n\n\n<p>for (i = 0; i &lt; ROW; i++) {<br>for (j = 0; j &lt; COL; j++) {<br>cellDetails[i][j].f = FLT_MAX;<br>cellDetails[i][j].g = FLT_MAX;<br>cellDetails[i][j].h = FLT_MAX;<br>cellDetails[i][j].parent_i = -1;<br>cellDetails[i][j].parent_j = -1;<br>}<br>}<\/p>\n\n\n\n<p>\/\/ Initialising the parameters of the starting node<br>i = src.first, j = src.second;<br>cellDetails[i][j].f = 0.0;<br>cellDetails[i][j].g = 0.0;<br>cellDetails[i][j].h = 0.0;<br>cellDetails[i][j].parent_i = i;<br>cellDetails[i][j].parent_j = j;<\/p>\n\n\n\n<p>\/*<br>Create an open list having information as-<br>&gt;<br>where f = g + h,<br>and i, j are the row and column index of that cell<br>Note that 0 &lt;= i &lt;= ROW-1 &amp; 0 &lt;= j &lt;= COL-1 This open list is implemented as a set of pair of pair.*\/ set openList;<\/p>\n\n\n\n<p>\/\/ Put the starting cell on the open list and set its<br>\/\/ &#8216;f&#8217; as 0<br>openList.insert(make_pair(0.0, make_pair(i, j)));<\/p>\n\n\n\n<p>\/\/ We set this boolean value as false as initially<br>\/\/ the destination is not reached.<br>bool foundDest = false;<\/p>\n\n\n\n<p>while (!openList.empty()) {<br>pPair p = *openList.begin();<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Remove this vertex from the open list\nopenList.erase(openList.begin());\n\n\/\/ Add this vertex to the closed list\ni = p.second.first;\nj = p.second.second;\nclosedList&#91;i]&#91;j] = true;\n\n\/*\nGenerating all the 8 successor of this cell\n\n  N.W N N.E\n  \\ | \/\n    \\ | \/\n  W----Cell----E\n    \/ | \\\n    \/ | \\\n  S.W S S.E\n\nCell--&gt;Popped Cell (i, j)\nN --&gt; North   (i-1, j)\nS --&gt; South   (i+1, j)\nE --&gt; East   (i, j+1)\nW --&gt; West     (i, j-1)\nN.E--&gt; North-East (i-1, j+1)\nN.W--&gt; North-West (i-1, j-1)\nS.E--&gt; South-East (i+1, j+1)\nS.W--&gt; South-West (i+1, j-1)*\/\n\n\/\/ To store the 'g', 'h' and 'f' of the 8 successors\ndouble gNew, hNew, fNew;\n\n\/\/----------- 1st Successor (North) ------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i - 1, j) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i - 1, j, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i - 1]&#91;j].parent_i = i;\n    cellDetails&#91;i - 1]&#91;j].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i - 1]&#91;j] == false\n      &amp;&amp; isUnBlocked(grid, i - 1, j)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.0;\n    hNew = calculateHValue(i - 1, j, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i - 1]&#91;j].f == FLT_MAX\n      || cellDetails&#91;i - 1]&#91;j].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i - 1, j)));\n\n      \/\/ Update the details of this cell\n      cellDetails&#91;i - 1]&#91;j].f = fNew;\n      cellDetails&#91;i - 1]&#91;j].g = gNew;\n      cellDetails&#91;i - 1]&#91;j].h = hNew;\n      cellDetails&#91;i - 1]&#91;j].parent_i = i;\n      cellDetails&#91;i - 1]&#91;j].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 2nd Successor (South) ------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i + 1, j) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i + 1, j, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i + 1]&#91;j].parent_i = i;\n    cellDetails&#91;i + 1]&#91;j].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i + 1]&#91;j] == false\n      &amp;&amp; isUnBlocked(grid, i + 1, j)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.0;\n    hNew = calculateHValue(i + 1, j, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i + 1]&#91;j].f == FLT_MAX\n      || cellDetails&#91;i + 1]&#91;j].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i + 1, j)));\n      \/\/ Update the details of this cell\n      cellDetails&#91;i + 1]&#91;j].f = fNew;\n      cellDetails&#91;i + 1]&#91;j].g = gNew;\n      cellDetails&#91;i + 1]&#91;j].h = hNew;\n      cellDetails&#91;i + 1]&#91;j].parent_i = i;\n      cellDetails&#91;i + 1]&#91;j].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 3rd Successor (East) ------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i, j + 1) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i, j + 1, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i]&#91;j + 1].parent_i = i;\n    cellDetails&#91;i]&#91;j + 1].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i]&#91;j + 1] == false\n      &amp;&amp; isUnBlocked(grid, i, j + 1)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.0;\n    hNew = calculateHValue(i, j + 1, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i]&#91;j + 1].f == FLT_MAX\n      || cellDetails&#91;i]&#91;j + 1].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i, j + 1)));\n\n      \/\/ Update the details of this cell\n      cellDetails&#91;i]&#91;j + 1].f = fNew;\n      cellDetails&#91;i]&#91;j + 1].g = gNew;\n      cellDetails&#91;i]&#91;j + 1].h = hNew;\n      cellDetails&#91;i]&#91;j + 1].parent_i = i;\n      cellDetails&#91;i]&#91;j + 1].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 4th Successor (West) ------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i, j - 1) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i, j - 1, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i]&#91;j - 1].parent_i = i;\n    cellDetails&#91;i]&#91;j - 1].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i]&#91;j - 1] == false\n      &amp;&amp; isUnBlocked(grid, i, j - 1)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.0;\n    hNew = calculateHValue(i, j - 1, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i]&#91;j - 1].f == FLT_MAX\n      || cellDetails&#91;i]&#91;j - 1].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i, j - 1)));\n\n      \/\/ Update the details of this cell\n      cellDetails&#91;i]&#91;j - 1].f = fNew;\n      cellDetails&#91;i]&#91;j - 1].g = gNew;\n      cellDetails&#91;i]&#91;j - 1].h = hNew;\n      cellDetails&#91;i]&#91;j - 1].parent_i = i;\n      cellDetails&#91;i]&#91;j - 1].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 5th Successor (North-East)\n\/\/------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i - 1, j + 1) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i - 1, j + 1, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i - 1]&#91;j + 1].parent_i = i;\n    cellDetails&#91;i - 1]&#91;j + 1].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i - 1]&#91;j + 1] == false\n      &amp;&amp; isUnBlocked(grid, i - 1, j + 1)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.414;\n    hNew = calculateHValue(i - 1, j + 1, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i - 1]&#91;j + 1].f == FLT_MAX\n      || cellDetails&#91;i - 1]&#91;j + 1].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i - 1, j + 1)));\n\n      \/\/ Update the details of this cell\n      cellDetails&#91;i - 1]&#91;j + 1].f = fNew;\n      cellDetails&#91;i - 1]&#91;j + 1].g = gNew;\n      cellDetails&#91;i - 1]&#91;j + 1].h = hNew;\n      cellDetails&#91;i - 1]&#91;j + 1].parent_i = i;\n      cellDetails&#91;i - 1]&#91;j + 1].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 6th Successor (North-West)\n\/\/------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i - 1, j - 1) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i - 1, j - 1, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i - 1]&#91;j - 1].parent_i = i;\n    cellDetails&#91;i - 1]&#91;j - 1].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i - 1]&#91;j - 1] == false\n      &amp;&amp; isUnBlocked(grid, i - 1, j - 1)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.414;\n    hNew = calculateHValue(i - 1, j - 1, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i - 1]&#91;j - 1].f == FLT_MAX\n      || cellDetails&#91;i - 1]&#91;j - 1].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i - 1, j - 1)));\n      \/\/ Update the details of this cell\n      cellDetails&#91;i - 1]&#91;j - 1].f = fNew;\n      cellDetails&#91;i - 1]&#91;j - 1].g = gNew;\n      cellDetails&#91;i - 1]&#91;j - 1].h = hNew;\n      cellDetails&#91;i - 1]&#91;j - 1].parent_i = i;\n      cellDetails&#91;i - 1]&#91;j - 1].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 7th Successor (South-East)\n\/\/------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i + 1, j + 1) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i + 1, j + 1, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i + 1]&#91;j + 1].parent_i = i;\n    cellDetails&#91;i + 1]&#91;j + 1].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i + 1]&#91;j + 1] == false\n      &amp;&amp; isUnBlocked(grid, i + 1, j + 1)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.414;\n    hNew = calculateHValue(i + 1, j + 1, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i + 1]&#91;j + 1].f == FLT_MAX\n      || cellDetails&#91;i + 1]&#91;j + 1].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i + 1, j + 1)));\n\n      \/\/ Update the details of this cell\n      cellDetails&#91;i + 1]&#91;j + 1].f = fNew;\n      cellDetails&#91;i + 1]&#91;j + 1].g = gNew;\n      cellDetails&#91;i + 1]&#91;j + 1].h = hNew;\n      cellDetails&#91;i + 1]&#91;j + 1].parent_i = i;\n      cellDetails&#91;i + 1]&#91;j + 1].parent_j = j;\n    }\n  }\n}\n\n\/\/----------- 8th Successor (South-West)\n\/\/------------\n\n\/\/ Only process this cell if this is a valid one\nif (isValid(i + 1, j - 1) == true) {\n  \/\/ If the destination cell is the same as the\n  \/\/ current successor\n  if (isDestination(i + 1, j - 1, dest) == true) {\n    \/\/ Set the Parent of the destination cell\n    cellDetails&#91;i + 1]&#91;j - 1].parent_i = i;\n    cellDetails&#91;i + 1]&#91;j - 1].parent_j = j;\n    printf(\"The destination cell is found\\n\");\n    tracePath(cellDetails, dest);\n    foundDest = true;\n    return;\n  }\n\n  \/\/ If the successor is already on the closed\n  \/\/ list or if it is blocked, then ignore it.\n  \/\/ Else do the following\n  else if (closedList&#91;i + 1]&#91;j - 1] == false\n      &amp;&amp; isUnBlocked(grid, i + 1, j - 1)\n          == true) {\n    gNew = cellDetails&#91;i]&#91;j].g + 1.414;\n    hNew = calculateHValue(i + 1, j - 1, dest);\n    fNew = gNew + hNew;\n\n    \/\/ If it isn\u2019t on the open list, add it to\n    \/\/ the open list. Make the current square\n    \/\/ the parent of this square. Record the\n    \/\/ f, g, and h costs of the square cell\n    \/\/       OR\n    \/\/ If it is on the open list already, check\n    \/\/ to see if this path to that square is\n    \/\/ better, using 'f' cost as the measure.\n    if (cellDetails&#91;i + 1]&#91;j - 1].f == FLT_MAX\n      || cellDetails&#91;i + 1]&#91;j - 1].f &gt; fNew) {\n      openList.insert(make_pair(\n        fNew, make_pair(i + 1, j - 1)));\n\n      \/\/ Update the details of this cell\n      cellDetails&#91;i + 1]&#91;j - 1].f = fNew;\n      cellDetails&#91;i + 1]&#91;j - 1].g = gNew;\n      cellDetails&#91;i + 1]&#91;j - 1].h = hNew;\n      cellDetails&#91;i + 1]&#91;j - 1].parent_i = i;\n      cellDetails&#91;i + 1]&#91;j - 1].parent_j = j;\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/ When the destination cell is not found and the open<br>\/\/ list is empty, then we conclude that we failed to<br>\/\/ reach the destination cell. This may happen when the<br>\/\/ there is no way to destination cell (due to<br>\/\/ blockages)<br>if (foundDest == false)<br>printf(&#8220;Failed to find the Destination Cell\\n&#8221;);<\/p>\n\n\n\n<p>return;<br>}<\/p>\n\n\n\n<p>\/\/ Driver program to test above function<br>int main()<br>{<br>\/* Description of the Grid-<br>1&#8211;&gt; The cell is not blocked<br>0&#8211;&gt; The cell is blocked *\/<br>int grid[ROW][COL]<br>= { { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },<br>{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },<br>{ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },<br>{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 },<br>{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },<br>{ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },<br>{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 },<br>{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },<br>{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 } };<\/p>\n\n\n\n<p>\/\/ Source is the left-most bottom-most corner<br>Pair src = make_pair(8, 0);<\/p>\n\n\n\n<p>\/\/ Destination is the left-most top-most corner<br>Pair dest = make_pair(0, 0);<\/p>\n\n\n\n<p>aStarSearch(grid, src, dest);<\/p>\n\n\n\n<p>return (0);<br>}<\/p>\n\n\n\n<p>\u9650\u5236\uff1a\u5c3d\u7ba1A*\u641c\u7d22\u7b97\u6cd5\u662f\u76ee\u524d\u6700\u597d\u7684\u8def\u5f84\u641c\u7d22\u7b97\u6cd5\uff0c\u4f46\u5b83\u5e76\u4e0d\u603b\u662f\u80fd\u591f\u4ea7\u751f\u6700\u77ed\u8def\u5f84\uff0c\u56e0\u4e3a\u5b83\u5728\u8ba1\u7b97h\u503c\u65f6\u4e25\u91cd\u4f9d\u8d56\u542f\u53d1\u5f0f\/\u8fd1\u4f3c\u65b9\u6cd5\u3002<br>05 \u5e94\u7528<\/p>\n\n\n\n<p>\u8fd9\u662fA*\u641c\u7d22\u7b97\u6cd5\u6700\u6709\u8da3\u7684\u90e8\u5206\u3002\u5b83\u4eec\u88ab\u7528\u5728\u6e38\u620f\u4e2d\uff01\u4f46\u662f\u5982\u4f55\u4f7f\u7528\u5462\uff1f<\/p>\n\n\n\n<p>\u4f60\u73a9\u8fc7\u5854\u9632\u6e38\u620f\u5417\uff1f<\/p>\n\n\n\n<p>\u5854\u9632\u662f\u4e00\u79cd\u7b56\u7565\u7c7b\u89c6\u9891\u6e38\u620f\uff0c\u76ee\u6807\u662f\u901a\u8fc7\u963b\u6321\u654c\u4eba\u7684\u653b\u51fb\u6765\u4fdd\u536b\u73a9\u5bb6\u7684\u9886\u571f\u6216\u8d22\u4ea7\uff0c\u901a\u5e38\u662f\u901a\u8fc7\u5728\u654c\u4eba\u7684\u653b\u51fb\u8def\u5f84\u4e0a\u6216\u6cbf\u7740\u5176\u653b\u51fb\u8def\u5f84\u4e0a\u653e\u7f6e\u9632\u5fa1\u7ed3\u6784\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>A*\u641c\u7d22\u7b97\u6cd5\u7ecf\u5e38\u7528\u4e8e\u627e\u5230\u4ece\u4e00\u4e2a\u70b9\u5230\u53e6\u4e00\u4e2a\u70b9\u7684\u6700\u77ed\u8def\u5f84\u3002\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u654c\u4eba\u4f7f\u7528\u5b83\u6765\u627e\u5230\u901a\u5411\u76ee\u6807\u7684\u8def\u5f84\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u4e00\u4e2a\u4f8b\u5b50\u662f\u975e\u5e38\u6d41\u884c\u7684\u6e38\u620f\u300a\u9b54\u517d\u4e89\u9738III\u300b\u3002<\/p>\n\n\n\n<p>5.1 \u5982\u679c\u641c\u7d22\u7a7a\u95f4\u4e0d\u662f\u4e00\u4e2a\u7f51\u683c\u800c\u662f\u4e00\u4e2a\u56fe\uff0c\u8be5\u600e\u4e48\u529e\uff1f<\/p>\n\n\n\n<p>\u76f8\u540c\u7684\u89c4\u5219\u4e5f\u9002\u7528\u4e8e\u56fe\u3002\u9009\u62e9\u7f51\u683c\u4f5c\u4e3a\u4f8b\u5b50\u662f\u4e3a\u4e86\u7b80\u5355\u7406\u89e3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528A*\u641c\u7d22\u7b97\u6cd5\u5728\u56fe\u4e2d\u627e\u5230\u6e90\u8282\u70b9\u548c\u76ee\u6807\u8282\u70b9\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\uff0c\u5c31\u50cf\u6211\u4eec\u5728\u4e8c\u7ef4\u7f51\u683c\u4e2d\u505a\u7684\u90a3\u6837\u3002<\/p>\n\n\n\n<p>5.2 \u65f6\u95f4\u590d\u6742\u5ea6<\/p>\n\n\n\n<p>\u8003\u8651\u5230\u56fe\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u904d\u5386\u6240\u6709\u7684\u8fb9\u624d\u80fd\u4ece\u6e90\u8282\u70b9\u5230\u8fbe\u76ee\u6807\u8282\u70b9\uff08\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a\u56fe\uff0c\u6e90\u8282\u70b9\u548c\u76ee\u6807\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u4e00\u7cfb\u5217\u8fb9\u8fde\u63a5\uff0c\u59820\uff08\u6e90\uff09-&gt;1-&gt;2-&gt;3\uff08\u76ee\u6807\uff09\uff09\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6700\u574f\u60c5\u51b5\u4e0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(E)\uff0c\u5176\u4e2dE\u662f\u56fe\u4e2d\u7684\u8fb9\u6570\u3002<\/p>\n\n\n\n<p>\u8f85\u52a9\u7a7a\u95f4 \u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u6240\u6709\u7684\u8fb9\u5b58\u50a8\u5728\u5f00\u653e\u5217\u8868\u4e2d\uff0c\u56e0\u6b64\u6700\u574f\u60c5\u51b5\u4e0b\u6240\u9700\u7684\u8f85\u52a9\u7a7a\u95f4\u662fO(V)\uff0c\u5176\u4e2dV\u662f\u9876\u70b9\u7684\u603b\u6570\u3002<br>06 \u603b\u7ed3<\/p>\n\n\n\n<p>\u90a3\u4e48\u4f55\u65f6\u4f7f\u7528\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\u800c\u4e0d\u662fA<em>\u7b97\u6cd5\uff0c\u4f55\u65f6\u4f7f\u7528Dijkstra\u7b97\u6cd5\u800c\u4e0d\u662fA<\/em>\u7b97\u6cd5\u6765\u5bfb\u627e\u6700\u77ed\u8def\u5f84\u5462\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1\uff09\u4e00\u4e2a\u8d77\u70b9\u548c\u4e00\u4e2a\u76ee\u7684\u5730\uff1a<\/p>\n\n\n\n<p>\u00b7 \u4f7f\u7528A*\u641c\u7d22\u7b97\u6cd5\uff08\u9002\u7528\u4e8e\u65e0\u6743\u56fe\u548c\u52a0\u6743\u56fe\uff09\u3002<\/p>\n\n\n\n<p>2\uff09\u4e00\u4e2a\u8d77\u70b9\uff0c\u591a\u4e2a\u76ee\u7684\u5730\uff1a<\/p>\n\n\n\n<p>\u00b7 \u5bf9\u4e8e\u65e0\u6743\u56fe\uff1a\u4f7f\u7528\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\u3002<\/p>\n\n\n\n<p>\u00b7 \u5bf9\u4e8e\u975e\u8d1f\u6743\u503c\u7684\u52a0\u6743\u56fe\uff1a\u4f7f\u7528Dijkstra\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>\u00b7 \u5bf9\u4e8e\u5e26\u6709\u8d1f\u6743\u503c\u7684\u52a0\u6743\u56fe\uff1a\u4f7f\u7528Bellman Ford\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>3\uff09\u4efb\u610f\u4e24\u4e2a\u8282\u70b9\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\uff1a<\/p>\n\n\n\n<p>\u00b7 \u4f7f\u7528Floyd-Warshall\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>\u00b7 \u4f7f\u7528Johnson\u7b97\u6cd5\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u673a\uff1a\u4e3a\u4e86\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u8fd1\u4f3c\u6c42\u89e3\u6700\u77ed\u8def\u5f84\uff0c\u4f8b\u5982\u5730 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[],"class_list":["post-17654","post","type-post","status-publish","format-standard","hentry","category-technology-frontier","category-home"],"views":0,"_links":{"self":[{"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/posts\/17654"}],"collection":[{"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/comments?post=17654"}],"version-history":[{"count":1,"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/posts\/17654\/revisions"}],"predecessor-version":[{"id":17660,"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/posts\/17654\/revisions\/17660"}],"wp:attachment":[{"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/media?parent=17654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/categories?post=17654"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/222.128.65.89:18086\/index.php\/wp-json\/wp\/v2\/tags?post=17654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}