A*

轉自http://www.mamicode.com/info-detail-1534200.html

康托展開

X = a[1]*(n-1)!+a[2]*(n-2)!+...+a[i]*(n-i)!+...+a[n-1]*1!+a[n]*0!
其中a[i]表示在num[i+1..n]中比num[i]小的數的數量

逆康托展開

由于:

a[i]≤n-i, a[i]*(n-i)!≤(n-i)*(n-i)!<(n-i+1)!

于是我們得到:

對于X=a[1]*(n-1)!+a[2]*(n-2)!+...+a[i]*(n-i)!+...+a[n-1]*1!+a[n]*0!

中的a[i]來說,無論a[i+1..n]的值為多少,其后面的和都不會超過(n-i)!

eg:當i=1? 所以X約等于a[1]*(n-1)! +后面

X除以(n-1)!,得到商c和余數r。其中c就等于a[1],r則等于后面的部分


這樣依次求解,就可以得到a[]數組了!

比如求解3的全排列中,編號為3的排列:

3 / 2! = 1 ... 1 => a[1] = 1
1 / 1! = 1 ... 0 => a[2] = 1
0 / 0! = 0 ... 0 => a[3] = 0

之后我們回想一下a[i]和含義??

a[i]表示在num[i+1..n]中比num[i]小的數的數量

對于已經排好序的序列比如

 {1, 2, 3},

a[] = {1, 1, 0}
unused = {1, 2, 3}, a[1] = 1, num[1] = 2
unused = {1, 3}, a[2] = 1, num[2] = 3
unused = {1}, a[3] = 0, num[3] = 1
=> 2, 3, 1

解釋看http://www.mamicode.com/info-detail-1534200.html?

講得很好

代碼也很易懂


#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>#define LL long long
#define N 100005
#define INF 0x3ffffffusing namespace std;int factor[]={1,1,2,6,24,120,720,5040,40320};
vector<int>stnum; //八數碼某個狀態序列
int destination; //目標狀態康托展開值struct state
{int f; //評估值和狀態值的總和。int g; //從起始狀態到當前狀態的最少步數int h; //評估函數int k; //該狀態康托展開值state(int f,int g,int h,int k):f(f),g(g),h(h),k(k){};friend bool operator<(state a,state b){if(a.f!=b.f)return a.f>b.f;else return a.g>b.g;}
};int cantor(vector<int>num) //康托展開
{int k = 0;int n = 9;for(int i=0;i<n;i++) {int tp = 0;for(int j = i+1; j < n; j++) {if(num[j] < num[i]) {tp++;}}k += tp * factor[n-1-i];}return k;
}vector<int> recantor(int k) //逆康托展開
{vector<int>num;int a[10];int n = 9;bool used[10];memset(used,false,sizeof(used));for(int i=0;i<n;i++){a[i] = k / factor[n-1-i];k %= factor[n-1-i];int cnt = 0;for(int j=0;j<n;j++){if(!used[j]) {cnt++;if(a[i] + 1==cnt) {num.push_back(j);used[j] = true;break;}}}}return num;
}int pos[]={8,0,1,2,3,4,5,6,7};int getdis(int a,int b) //曼哈頓距離
{return (abs(a/3-b/3)+abs(a%3-b%3));//a/3-b/3行差,a%3-b%3列差
}int get_evaluation(vector<int>num) //評估函數
{//評估函數設定為1-8八數字當前位置到目標位置的曼哈頓距離之和。 比如其中就有當前位置的1和目標位置的1 的曼哈頓距離之和//目標式1 2 3 4 5 6 7 8 0//此時 pos[1]=0 ,表示在目標式中1原本應該在第0位 從而推出0應該在第8位 對應pos[0] 所以pos的作用是 映射這個數字在目標式子中的位置,所以要偏移//當前   2 4 7 6 8 0 1 3 5//因此 i 表示 在當前式子中這個數字的位置  比如2 是第0位 4 是第1位int h=0;for(int i=;i<9;i++){h+=getdis(i,pos[num[i]]);}return h;
}void solve()
{priority_queue<state>openlist;set<int>closelist; //在closelist中,拋棄該狀態。while(!openlist.empty()) openlist.pop();closelist.clear();int h=get_evaluation(stnum);openlist.push(state(h,0,h,cantor(stnum)));int step=0; //統計步數bool flag=false; //標記是否能找到目標狀態while(!openlist.empty()){state cur=openlist.top(); //從openlist中取出F值最小的狀態openlist.pop();int k=cur.k;//cur的康托展開 closelist.insert(k); //將該狀態加入closelist if(destination==k) {//若該狀態為目標狀態,結束搜索flag=true; //找到目標狀態step=cur.g; //步數break;}vector<int> st = recantor(k); //當前狀態的八數碼序列for(int i=0;i<9;i++) if(st[i]==0) //找到0的位置{if(i%3!=2) { //不在行末,0位可以和右邊相鄰位置交換swap(st[i],st[i+1]);int x = cantor(st);int g = cur.g+1;int h = get_evaluation(st);int f = g + h;if(closelist.find(x)==closelist.end()) {openlist.push(state(f,g,h,x));}swap(st[i],st[i+1]);//又要換回來來進行下面的步驟}if(i%3!=0) { //不在某行開頭,可以和左邊相鄰位置交換swap(st[i],st[i-1]);int x = cantor(st);int g = cur.g+1;int h = get_evaluation(st);int f = g + h;if(closelist.find(x)==closelist.end()) {openlist.push(state(f,g,h,x));}swap(st[i],st[i-1]);}if(i<6) {swap(st[i],st[i+3]);int x = cantor(st);int g = cur.g+1;int h = get_evaluation(st);int f = g + h;if(closelist.find(x)==closelist.end()) {openlist.push(state(f,g,h,x));}swap(st[i],st[i+3]);}if(i>2) {swap(st[i],st[i-3]);int x = cantor(st);int g = cur.g+1;int h = get_evaluation(st);int f = g + h;if(closelist.find(x)==closelist.end()) {openlist.push(state(f,g,h,x));}swap(st[i],st[i-3]);}}}if(!flag) cout << "No Solution!" << endl;else cout<<step<<endl;}int main() {vector <int> des;for(int i=0;i<8;i++) des.push_back(i+1);des.push_back(0);destination=cantor(des);int T;cin >> T;while(T--){int a;stnum.clear();for(int i=0;i<9;i++) {cin>>a; stnum.push_back(a);}solve();}return 0;
}

通常A* 還有一個更新

對于第n個F

即 if(F>g(n)+1+H(n-1))? F=g(n)+1+H(n-1)






轉載于:https://www.cnblogs.com/LandingGuy/p/9280239.html

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

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

相關文章

unity第三人稱射擊游戲_在游戲上第3部分完美的信息游戲

unity第三人稱射擊游戲Previous article上一篇文章 The economics literature distinguishes the quality of a game’s information (perfect vs. imperfect) from the completeness of a game’s information (complete vs. incomplete). Perfect information means that ev…

JVM(2)--一文讀懂垃圾回收

與其他語言相比&#xff0c;例如c/c&#xff0c;我們都知道&#xff0c;java虛擬機對于程序中產生的垃圾&#xff0c;虛擬機是會自動幫我們進行清除管理的&#xff0c;而像c/c這些語言平臺則需要程序員自己手動對內存進行釋放。 雖然這種自動幫我們回收垃圾的策略少了一定的靈活…

2058. 找出臨界點之間的最小和最大距離

2058. 找出臨界點之間的最小和最大距離 鏈表中的 臨界點 定義為一個 局部極大值點 或 局部極小值點 。 如果當前節點的值 嚴格大于 前一個節點和后一個節點&#xff0c;那么這個節點就是一個 局部極大值點 。 如果當前節點的值 嚴格小于 前一個節點和后一個節點&#xff0c;…

tb計算機存儲單位_如何節省數TB的云存儲

tb計算機存儲單位Whatever cloud provider a company may use, costs are always a factor that influences decision-making, and the way software is written. As a consequence, almost any approach that helps save costs is likely worth investigating.無論公司使用哪種…

nginx簡單代理配置

原文&#xff1a;https://my.oschina.net/wangnian/blog/791294 前言 Nginx ("engine x") 是一個高性能的HTTP和反向代理服務器&#xff0c;也是一個IMAP/POP3/SMTP服務器。Nginx是由Igor Sysoev為俄羅斯訪問量第二的Rambler.ru站點開發的&#xff0c;第一個公開版本…

2059. 轉化數字的最小運算數

2059. 轉化數字的最小運算數 給你一個下標從 0 開始的整數數組 nums &#xff0c;該數組由 互不相同 的數字組成。另給你兩個整數 start 和 goal 。 整數 x 的值最開始設為 start &#xff0c;你打算執行一些運算使 x 轉化為 goal 。你可以對數字 x 重復執行下述運算&#xf…

Django Rest Framework(一)

一、什么是RESTful REST與技術無關&#xff0c;代表一種軟件架構風格&#xff0c;REST是Representational State Transfer的簡稱&#xff0c;中文翻譯為“表征狀態轉移”。 REST從資源的角度審視整個網絡&#xff0c;它將分布在網絡中某個節點的資源通過URL進行標識&#xff0c…

光落在你臉上,可愛一如往常

沙沙野 https://www.ssyer.com讓作品遇見全世界圖片來自&#xff1a;沙沙野沙沙野 https://www.ssyer.com讓作品遇見全世界圖片來自&#xff1a;沙沙野沙沙野 https://www.ssyer.com讓作品遇見全世界圖片來自&#xff1a;沙沙野沙沙野 https://www.ssyer.com讓作品遇見全世界圖…

數據可視化機器學習工具在線_為什么您不能跳過學習數據可視化

數據可視化機器學習工具在線重點 (Top highlight)There’s no scarcity of posts online about ‘fancy’ data topics like data modelling and data engineering. But I’ve noticed their cousin, data visualization, barely gets the same amount of attention. Among dat…

2047. 句子中的有效單詞數

2047. 句子中的有效單詞數 句子僅由小寫字母&#xff08;‘a’ 到 ‘z’&#xff09;、數字&#xff08;‘0’ 到 ‘9’&#xff09;、連字符&#xff08;’-’&#xff09;、標點符號&#xff08;’!’、’.’ 和 ‘,’&#xff09;以及空格&#xff08;’ &#xff09;組成。…

fa

轉載于:https://www.cnblogs.com/smallpigger/p/9546173.html

python中nlp的庫_用于nlp的python中的網站數據清理

python中nlp的庫The most important step of any data-driven project is obtaining quality data. Without these preprocessing steps, the results of a project can easily be biased or completely misunderstood. Here, we will focus on cleaning data that is composed…

51Nod 1043 幸運號碼

1 #include <stdio.h>2 #include <algorithm>3 using namespace std;4 5 typedef long long ll;6 const int mod 1e9 7;7 int dp[1010][10000];8 // dp[i][j] : i 個數&#xff0c;組成總和為j 的數量9 10 int main() 11 { 12 int n; 13 scanf("%d…

一張圖看懂云棲大會·上海峰會重磅產品發布

2018云棲大會上海峰會上&#xff0c;阿里云重磅發布一批產品并宣布了新一輪的價格調整&#xff0c;再次用科技普惠廣大開發者和用戶&#xff0c;詳情見長圖。 了解更多產品請戳&#xff1a;https://yunqi.aliyun.com/2018/shanghai/product?spm5176.8142029.759399.2.a7236d3e…

488. 祖瑪游戲

488. 祖瑪游戲 你正在參與祖瑪游戲的一個變種。 在這個祖瑪游戲變體中&#xff0c;桌面上有 一排 彩球&#xff0c;每個球的顏色可能是&#xff1a;紅色 ‘R’、黃色 ‘Y’、藍色 ‘B’、綠色 ‘G’ 或白色 ‘W’ 。你的手中也有一些彩球。 你的目標是 清空 桌面上所有的球。…

【黑客免殺攻防】讀書筆記14 - 面向對象逆向-虛函數、MFC逆向

虛函數存在是為了克服類型域解決方案的缺陷&#xff0c;以使程序員可以在基類里聲明一些能夠在各個派生類里重新定義的函數。 1 識別簡單的虛函數 代碼示例&#xff1a; #include "stdafx.h" #include <Windows.h>class CObj { public:CObj():m_Obj_1(0xAAAAAA…

怎么看另一個電腦端口是否通_誰一個人睡覺另一個看看夫妻的睡眠習慣

怎么看另一個電腦端口是否通In 2014, FiveThirtyEight took a survey of about 1057 respondents to get a look at the (literal) sleeping habits of the American public beyond media portrayal. Some interesting notices: first, that about 45% of all couples sleep to…

495. 提莫攻擊

495. 提莫攻擊 在《英雄聯盟》的世界中&#xff0c;有一個叫 “提莫” 的英雄。他的攻擊可以讓敵方英雄艾希&#xff08;編者注&#xff1a;寒冰射手&#xff09;進入中毒狀態。 當提莫攻擊艾希&#xff0c;艾希的中毒狀態正好持續 duration 秒。 正式地講&#xff0c;提莫在…

Java基礎之Collection和Map

List&#xff1a;實現了collection接口&#xff0c;list可以重復&#xff0c;有順序 實現方式&#xff1a;3種&#xff0c;分別為&#xff1a;ArrayList&#xff0c;LinkedList&#xff0c;Vector。 三者的比較&#xff1a; ArrayList底層是一個動態數組&#xff0c;數組是使用…

20155320《網絡對抗》Exp4 惡意代碼分析

20155320《網絡對抗》Exp4 惡意代碼分析 【系統運行監控】 使用schtasks指令監控系統運行 首先在C盤目錄下建立一個netstatlog.bat文件&#xff08;由于是系統盤&#xff0c;所以從別的盤建一個然后拷過去&#xff09;&#xff0c;用來將記錄的聯網結果格式化輸出到netstatlog.…