A* \IDA* 分析總結,ida分析總結

來源:互聯網
上載者:User

A* \IDA* 分析總結,ida分析總結

   

經典八皇后問題:

 

#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <cstdio>#include <cstring>using namespace std;struct node{    int tab[3][3];    int r,c;    int hash_val;    //當前排列在全排列時候的大小位置    node* pre;    int op;    int f,g;         //f:估計值 g:從起始點到當前點的花費    bool operator < (const node& rhs)const{         return f > rhs.f;    }}st,ed;node t[370000];int tot;int hash_tab[370000];priority_queue<node> open;int fn[10];int ed_map[10][2];int dir_i[4] = {0,0,1,-1};int dir_j[4] = {1,-1,0,0};char print_op[4] = {'r','l','d','u'};//處理輸入的字元void In(char s[],node& t){    char ch;    int next = 0;    for(int i = 0;i < 3;++i){        for(int j = 0;j < 3;++j){            while(s[next] == ' ') ++next;            ch = s[next++];            if(isdigit(ch))                st.tab[i][j] = ch - '0';            else {          // x 的位置                st.r = i;                st.c = j;                st.tab[i][j] = 0;            }        }    }}//擷取估價函數值int get_f(node a,int g){    int h = 0;    for(int i = 0;i < 3;++i)        for(int j = 0;j < 3;++j)           if(a.tab[i][j])     //距離目標點的曼哈頓距離              h += abs(i - ed_map[a.tab[i][j]][0]) + abs(j - ed_map[a.tab[i][j]][1]);    return g + h;}//康拓展開,判斷該組合在所有排列中的位置int get_hash(node a){    int ret;    ret = 0;    int num = 8;    for(int i = 0;i < 3;++i){        for(int j = 0;j < 3;++j){            int x = 0;            //當前這個數後有多少個比他小            for(int jj = j + 1;jj < 3;++jj)                if(a.tab[i][jj] < a.tab[i][j]) x++;            for(int ii = i + 1;ii < 3;++ii)                for(int jj = 0;jj < 3;++jj)                   if(a.tab[ii][jj] < a.tab[i][j]) x++;            ret += fn[num] * x;             //康拓展開            num--;        }    }    return ret;}//預先處理void init(){     memset(hash_tab,0,sizeof(hash_tab));     while(!open.empty()) open.pop();     tot = 0;     st.f = get_f(st,0);       //擷取估價函數值     st.g = 0;     st.hash_val = get_hash(st);  //擷取該組合在全排列的位置     open.push(st);     hash_tab[st.hash_val] = 1;    //當前的組合排列已經遍曆過了}void pre(){    //預先處理階乘   fn[0] = 1;   for(int i = 1;i < 9;++i) fn[i] = i * fn[i - 1];   //預先處理結果   for(int i = 0;i < 3;++i)      for(int j = 0;j < 3;++j)         ed.tab[i][j] = (i * 3) + j + 1;   ed.tab[2][2] = 0;   ed.hash_val = get_hash(ed);   //預先處理結果位置的映射   for(int i = 0;i < 3;++i)     for(int j = 0;j < 3;++j)        if(ed.tab[i][j]){            ed_map[ed.tab[i][j]][0] = i;            ed_map[ed.tab[i][j]][1] = j;        }}//判斷逆序數的奇偶性int get_preval(node a){   int ret = 0;   for(int i = 0;i < 3;++i)     for(int j = 0;j < 3;++j){        if(!a.tab[i][j]) continue;        int x = 0;        for(int jj = j + 1;jj < 3;++jj)            if(a.tab[i][jj] && a.tab[i][jj] < a.tab[i][j]) x++;        for(int ii = i + 1;ii < 3;++ii)            for(int jj = 0;jj < 3;++jj)               if(a.tab[ii][jj] && a.tab[ii][jj] < a.tab[i][j]) x++;        ret += x;     }     return ret & 1;}bool pre_solve(){    return(get_preval(st)^(get_preval(ed)));}void change(node &tmp,node a,int nextr,int nextc,int _i,int idx){    for(int i = 0;i < 3;++i)        for(int j = 0;j < 3;++j)           tmp.tab[i][j] = a.tab[i][j];    swap(tmp.tab[nextr][nextc],tmp.tab[a.r][a.c]);    tmp.hash_val = get_hash(tmp);    tmp.r = nextr;    tmp.c = nextc;    tmp.pre = &t[idx];    tmp.op = _i;              //移動的方向    tmp.g = a.g + 1;          //當前的步數    tmp.f = get_f(tmp,tmp.g);}bool check(int i,int j){    if(i > 2 || i < 0 || j > 2 || j < 0) return false;    return true;}//列印路徑void path(node* a){    if(a->hash_val == st.hash_val) return ;    path(a->pre);    printf("%c",print_op[a->op]);}//A*void Astar(){    int nextr,nextc;    node ans = st;    int fla = 0;    if(st.hash_val != ed.hash_val)    while(!open.empty()){        node a = open.top();        open.pop();        t[++tot] = a;        for(int i = 0;i < 4;++i){            nextr = a.r + dir_i[i];            nextc = a.c + dir_j[i];            if(check(nextr,nextc)){                node tmp;                change(tmp,a,nextr,nextc,i,tot);                if(hash_tab[tmp.hash_val]) continue;                if(tmp.hash_val == ed.hash_val){                    fla = 1;                    ans = tmp;                    break;                }                open.push(tmp);            }        }        if(fla) break;        hash_tab[a.hash_val] = 1;    }    path(&ans);    puts("");}int main(){   // freopen("Input.txt","r",stdin);    char ss[12];    while(gets(ss)){        pre();        In(ss,st);        init();        if(pre_solve())            puts("unsolvable");        else            Astar();    }    return 0;}


 

 

 

對於空格(0)的左移/右移操作,對應序列不變(逆序數也就不變) 
對於空格(0)的上移/下移操作,相當於序列的某個數字前移/後移兩位,該序列的逆序數奇偶性不變。 
所以求初始狀態與目標狀態的逆序數可作出判斷 
例中前者為奇,後者為偶,因此無解

利用奇偶性判斷所給出的初始狀態有無解. 

判別方法是: 
以數組為一維的舉例子. 
將八數位的一個結點表示成一個數組a[9],空格用0表示,設臨時函數p(x)定義為:x數所在位置前面的數比x小的數的個數, 
其中0空格不算在之內,那設目標狀態為b[9],那r=sigma(p(x)) sigma()表示取所有的x:1-8並求和, 
那對於初始狀態a[9],t=sigma(p(x)),如果r和t同為奇數或者同為偶數,那麼該狀態有解,否則無解。 

考慮到四種移動方法對sigma(p(x))的影響,左移和右移是不會影響它的值的, 
更不會影響奇偶性,如果是上移或者下移就會影響: 
上移:一次上移會使一個元素向前跳兩個數位位置,設這兩個數字為a1,a2, 
不妨設a1<a2,移的這個數字設為a0,那無非只有以下三次情況: 
1,a0<a1<a2,考慮它們三者的p(x)值,p(a0)不變,p(a1)++,p(a2)++,總體增加了2 
2,a1<a0<a2,p(a0)--,p(a1)不變,p(a2)++,總體不變 
3,a1<a2<a0,p(a0)-=2,p(a1),p(a2)不變,總體減小了2 

綜合起來的結論就是不會影響sigma(p(x))的奇偶性。

 

 


總結組合邏輯電路的分析方法

基本的方法是列真值表化卡諾圖得出最簡與或形式,這樣便可以用兩級與非門實現了。(與或形式可以寫為兩級與非的形式)
以X'表示變數X的反,利用X+Y=(X'Y')'
設加數被加數分別為A1A0,B1B0;和為D1D0;進位為C,則:
D1=A1'A0'B1+A1'B1B0'+A1'A0B1'B0+A1A0B1B0+A1A0'B1'
+A1B1'B0'
=((A1'A0'B1)'(A1'B1B0')'(A1'A0B1'B0)'(A1A0B1B0)'(A1A0'B1')'(A1B1'B0')' )'
D0=A0'B0+A0B0'=( (A0'B0)'(A0B0')' )'
C=A1B1+A1A0B0+A1B1B0=( (A1B1)'(A1A0B0)'(A1B1B0)' )'

如此便可用與非門實現。
 
總結一般是對本單位或個人的實踐活動加以總結,因此總結主要採用第一人稱的寫 (A) 對 (B) 錯

總結,是對過去一定時期的工作、學習或思想情況進行回顧、分析,並做出客觀評價的書面材料。按內容分,有學習總結、工作總結、思想總結等,按時間分,有年度總結、季度總結、月份總結等。
對
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.