From bdec4b3db82f9f589687157a0c24861ac4f8072e Mon Sep 17 00:00:00 2001 From: Camille <59353274+Camille0512@users.noreply.github.com> Date: Wed, 13 Nov 2024 23:55:41 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Update=200126.=E9=AA=91=E5=A3=AB=E7=9A=84?= =?UTF-8?q?=E6=94=BB=E5=87=BBastar.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加了一个C++的懒人写法,其实效率还高一些。 --- ...7\232\204\346\224\273\345\207\273astar.md" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 6ea6ca8346..14ea023431 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -333,6 +333,76 @@ IDA * 算法 对这一空间增长问题进行了优化,关于 IDA * 算法, ## 其他语言版本 +### C++ 其他写法 +```C++ +// 懒人写法 +#include +#include +#include + +using namespace std; + +int b1, b2; +int dir[8][2] = { + -1, -2, -2, -1, 1, -2, -2, 1, -1, 2, 2, -1, 1, 2, 2, 1 +}; + +struct Knight { + int x, y; + int fromStart; // 开始到当前坐标的距离 + int toEnd; // 当前坐标到目标坐标的距离 + int total; // 总距离 + int cnt; // 把计数放在Knight里面,一边计算距离一边计数就省了申请矩阵的空间了 + + bool operator < (const Knight& k) const { + return k.total < total; // 在priority_queue里用的比较符号 + } +}; + +int Heuristic(const Knight& k) { + return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); +} + +int astart(const Knight& k) { + priority_queue que; // 把queue在这里申请就省了删掉里面的元素的麻烦 + int res; // 把结果存这里然后返回,直接打印出来 + que.push(k); + Knight cur, next; + while (!que.empty()) { + cur = que.top(); que.pop(); + if (cur.x == b1 && cur.y == b2) { res = cur.cnt; break; } // 保存找到的节点的步数用作返回 + + for (int i = 0; i < 8; i++) { + next.x = cur.x + dir[i][0], next.y = cur.y + dir[i][1]; + if (next.x < 1 || next.x > 1000 || next.y < 1 || next.y > 1000) continue; // 超标的不需要考虑 + + next.fromStart = cur.fromStart + 5; + next.toEnd = Heuristic(next); + next.total = next.fromStart + next.toEnd; + next.cnt = cur.cnt + 1; // 下一个点的步数是当前点的步数+1 + que.push(next); + } + } + return res; +} + +int main() { + int n, a1, a2; cin >> n; + + for (int i = 0; i < n; i++) { + cin >> a1 >> a2 >> b1 >> b2; + Knight start; start.x = a1, start.y = a2; + start.fromStart = 0, start.toEnd = Heuristic(start); + start.total = start.fromStart + start.toEnd, start.cnt = 0; + + int res = astart(start); + cout << res << endl; + } + + return 0; +} +``` + ### Java ### Python From d84c30b30e33c5bee8bdae6861969bc2dbe3774f Mon Sep 17 00:00:00 2001 From: Camille <59353274+Camille0512@users.noreply.github.com> Date: Wed, 13 Nov 2024 23:58:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Revert=20"Update=200126.=E9=AA=91=E5=A3=AB?= =?UTF-8?q?=E7=9A=84=E6=94=BB=E5=87=BBastar.md"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\232\204\346\224\273\345\207\273astar.md" | 70 ------------------- 1 file changed, 70 deletions(-) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 14ea023431..6ea6ca8346 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -333,76 +333,6 @@ IDA * 算法 对这一空间增长问题进行了优化,关于 IDA * 算法, ## 其他语言版本 -### C++ 其他写法 -```C++ -// 懒人写法 -#include -#include -#include - -using namespace std; - -int b1, b2; -int dir[8][2] = { - -1, -2, -2, -1, 1, -2, -2, 1, -1, 2, 2, -1, 1, 2, 2, 1 -}; - -struct Knight { - int x, y; - int fromStart; // 开始到当前坐标的距离 - int toEnd; // 当前坐标到目标坐标的距离 - int total; // 总距离 - int cnt; // 把计数放在Knight里面,一边计算距离一边计数就省了申请矩阵的空间了 - - bool operator < (const Knight& k) const { - return k.total < total; // 在priority_queue里用的比较符号 - } -}; - -int Heuristic(const Knight& k) { - return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); -} - -int astart(const Knight& k) { - priority_queue que; // 把queue在这里申请就省了删掉里面的元素的麻烦 - int res; // 把结果存这里然后返回,直接打印出来 - que.push(k); - Knight cur, next; - while (!que.empty()) { - cur = que.top(); que.pop(); - if (cur.x == b1 && cur.y == b2) { res = cur.cnt; break; } // 保存找到的节点的步数用作返回 - - for (int i = 0; i < 8; i++) { - next.x = cur.x + dir[i][0], next.y = cur.y + dir[i][1]; - if (next.x < 1 || next.x > 1000 || next.y < 1 || next.y > 1000) continue; // 超标的不需要考虑 - - next.fromStart = cur.fromStart + 5; - next.toEnd = Heuristic(next); - next.total = next.fromStart + next.toEnd; - next.cnt = cur.cnt + 1; // 下一个点的步数是当前点的步数+1 - que.push(next); - } - } - return res; -} - -int main() { - int n, a1, a2; cin >> n; - - for (int i = 0; i < n; i++) { - cin >> a1 >> a2 >> b1 >> b2; - Knight start; start.x = a1, start.y = a2; - start.fromStart = 0, start.toEnd = Heuristic(start); - start.total = start.fromStart + start.toEnd, start.cnt = 0; - - int res = astart(start); - cout << res << endl; - } - - return 0; -} -``` - ### Java ### Python From 59b3a5aaab741649f854c7a2d9557c131c838a99 Mon Sep 17 00:00:00 2001 From: Camille <59353274+Camille0512@users.noreply.github.com> Date: Thu, 14 Nov 2024 00:02:07 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Revert=20"Update=200126.=E9=AA=91=E5=A3=AB?= =?UTF-8?q?=E7=9A=84=E6=94=BB=E5=87=BBastar.md"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From ead59f2343cb897024916363e00bf3c635c49a41 Mon Sep 17 00:00:00 2001 From: Camille <59353274+Camille0512@users.noreply.github.com> Date: Thu, 14 Nov 2024 00:04:42 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Update=200126.=E9=AA=91=E5=A3=AB=E7=9A=84?= =?UTF-8?q?=E6=94=BB=E5=87=BBastar.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C++懒人写法,效率会高一些 --- ...7\232\204\346\224\273\345\207\273astar.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 6ea6ca8346..0743c142fc 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -333,6 +333,70 @@ IDA * 算法 对这一空间增长问题进行了优化,关于 IDA * 算法, ## 其他语言版本 +### C++ 其他写法 +```C++ +// 懒人写法 +#include +#include +#include +using namespace std; +int b1, b2; +int dir[8][2] = { + -1, -2, -2, -1, 1, -2, -2, 1, -1, 2, 2, -1, 1, 2, 2, 1 +}; +struct Knight { + int x, y; + int fromStart; // 开始到当前坐标的距离 + int toEnd; // 当前坐标到目标坐标的距离 + int total; // 总距离 + int cnt; // 把计数放在Knight里面,一边计算距离一边计数就省了申请矩阵的空间了 + + bool operator < (const Knight& k) const { + return k.total < total; // 在priority_queue里用的比较符号 + } +}; +int Heuristic(const Knight& k) { + return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); +} +int astart(const Knight& k) { + priority_queue que; // 把queue在这里申请就省了删掉里面的元素的麻烦 + int res; // 把结果存这里然后返回,直接打印出来 + que.push(k); + Knight cur, next; + while (!que.empty()) { + cur = que.top(); que.pop(); + if (cur.x == b1 && cur.y == b2) { res = cur.cnt; break; } // 保存找到的节点的步数用作返回 + + for (int i = 0; i < 8; i++) { + next.x = cur.x + dir[i][0], next.y = cur.y + dir[i][1]; + if (next.x < 1 || next.x > 1000 || next.y < 1 || next.y > 1000) continue; // 超标的不需要考虑 + + next.fromStart = cur.fromStart + 5; + next.toEnd = Heuristic(next); + next.total = next.fromStart + next.toEnd; + next.cnt = cur.cnt + 1; // 下一个点的步数是当前点的步数+1 + que.push(next); + } + } + return res; +} +int main() { + int n, a1, a2; cin >> n; + + for (int i = 0; i < n; i++) { + cin >> a1 >> a2 >> b1 >> b2; + Knight start; start.x = a1, start.y = a2; + start.fromStart = 0, start.toEnd = Heuristic(start); + start.total = start.fromStart + start.toEnd, start.cnt = 0; + + int res = astart(start); + cout << res << endl; + } + + return 0; +} +``` + ### Java ### Python