• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            A Za, A Za, Fighting...

            堅信:勤能補(bǔ)拙

            PKU 1077 Eight

            問題:
            http://acm.pku.edu.cn/JudgeOnline/problem?id=1077

            思路:
            傳說中經(jīng)典的經(jīng)典
            解法有: 單向BFS,雙向BFS,還有A*等啟發(fā)式搜索算法
            今天先寫了前兩種方法的代碼,至于啟發(fā)式算法待續(xù)

            判重: 全排列的哈希,詳見: http://www.shnenglu.com/Joe/archive/2010/08/06/122410.html

            代碼(單向BFS,110MS):
              1 #define RC 3
              2 #define STR_LEN 9
              3 #define HASH_LEN 362880 /* 9! */
              4 const int facs[] = {12624120720504040320};
              5 const char letters[] = "udlr";
              6 const int dx[] = {-1100};
              7 const int dy[] = {00-11};
              8 char success[] = "123456780";
              9 char begin[STR_LEN+1];
             10 int success_hash, begin_position;
             11 int hash[HASH_LEN];
             12 struct EACH {
             13     char str[STR_LEN+1];
             14     int position;
             15     int pre;
             16     int dir;
             17 } queue[HASH_LEN];
             18 
             19 /* permutation -> number */
             20 int 
             21 hash_func(char *str)
             22 {
             23     int i, j, cnt, result = 0;
             24     for(i=1; i<STR_LEN; i++) {
             25         cnt = 0;
             26         for(j=0; j<i; j++)
             27             if(str[j] > str[i])
             28                 ++cnt;
             29         result += (cnt*facs[i-1]);
             30     }
             31     return result;
             32 }
             33 
             34 void
             35 init()
             36 {
             37     int i;
             38     char input[2];
             39     memset(hash, 0sizeof(hash));
             40     for(i=0; i<STR_LEN; i++) {
             41         scanf("%s", input);
             42         begin[i] = input[0];
             43         if(begin[i] == 'x') {
             44             begin[i] = '0';
             45             begin_position = i;
             46         }
             47     }
             48     begin[STR_LEN] = '\0';
             49     success_hash = hash_func(success);
             50 }
             51 
             52 void
             53 output(int index)
             54 {
             55     if(queue[index].pre == -1)
             56         return;
             57     output(queue[index].pre);
             58     printf("%c", letters[queue[index].dir]);
             59 }
             60 
             61 #define ADD(tail, s, pos, p, d) strcpy(queue[tail].str, s); \
             62     queue[tail].position = pos; \
             63     queue[tail].pre = p; \
             64     queue[tail].dir = d;
             65 
             66 #define CHECK(h) if(h == success_hash) { \
             67     output(tail); \
             68     printf("\n"); \
             69     return; \
             70 }
             71 
             72 void
             73 bfs()
             74 {
             75     int i, h, head, tail;
             76     char nxt[STR_LEN+1];
             77     int curx, cury, nxtx, nxty;
             78     head = -1;
             79     tail = 0;
             80     ADD(tail, begin, begin_position, -1-1);
             81     h = hash_func(begin);
             82     hash[h] = 1;
             83     CHECK(h);
             84     while(head < tail) {
             85         ++head;
             86         curx = queue[head].position/RC;
             87         cury = queue[head].position%RC;
             88         for(i=0; i<4; i++) {
             89             nxtx = curx + dx[i];
             90             nxty = cury + dy[i];
             91             if(nxtx>=0 && nxtx<RC && nxty>=0 && nxty<RC) {
             92                 strcpy(nxt, queue[head].str);
             93                 nxt[queue[head].position] = nxt[nxtx*RC+nxty];
             94                 nxt[nxtx*RC+nxty] = '0';
             95                 h = hash_func(nxt);
             96                 if(!hash[h]) {
             97                     ++tail;
             98                     ADD(tail, nxt, nxtx*RC+nxty, head, i);
             99                     hash[h] = 1;
            100                     CHECK(h);
            101                 }
            102             }
            103         }
            104     }
            105     printf("unsolvable\n");
            106 }

            代碼(雙向BFS,16MS):
              1 #define RC 3
              2 #define STR_LEN 9
              3 #define HASH_LEN 362880 /* 9! */
              4 const int facs[] = {12624120720504040320};
              5 const char letters[] = "udlr";
              6 const int pos[] = {-33-11}; /* up, down, left, right */
              7 const int movable[][4= {0,1,0,10,1,1,10,1,1,01,1,0,11,1,1,11,1,1,01,0,0,11,0,1,11,0,1,0};
              8 int hash[2][HASH_LEN];
              9 struct EACH {
             10     char str[STR_LEN+1];
             11     int position, pre, dir, hval;
             12 } queue[2][HASH_LEN];
             13 
             14 /* permutation -> number */
             15 int 
             16 hash_func(char *str)
             17 {
             18     int i, j, cnt, result = 0;
             19     for(i=1; i<STR_LEN; i++) {
             20         cnt = 0;
             21         for(j=0; j<i; j++)
             22             if(str[j] > str[i])
             23                 ++cnt;
             24         result += (cnt*facs[i-1]);
             25     }
             26     return result;
             27 }
             28 
             29 void
             30 output(int hash_value)
             31 {
             32     int i, j;
             33     char tmp[250];
             34     for(i=0; i<HASH_LEN; i++)
             35         if(queue[0][i].hval == hash_value)
             36             break;
             37     j = 0;
             38     while(queue[0][i].pre != -1) {
             39         tmp[j++= letters[queue[0][i].dir];
             40         i = queue[0][i].pre;
             41     }
             42     for(i=j-1; i>=0; i--)
             43         printf("%c", tmp[i]);
             44 
             45     for(i=0; i<HASH_LEN; i++)
             46         if(queue[1][i].hval == hash_value)
             47             break;
             48     while(queue[1][i].pre != -1) {
             49         printf("%c", queue[1][i].dir%2==0 ? letters[queue[1][i].dir+1] : letters[queue[1][i].dir-1]);
             50         i = queue[1][i].pre;
             51     }
             52 }
             53 
             54 #define ADD(index, tail, s, pos, p, d, h) strcpy(queue[index][tail].str, s); \
             55     queue[index][tail].position = pos; \
             56     queue[index][tail].pre = p; \
             57     queue[index][tail].dir = d; \
             58     queue[index][tail].hval = h;
             59 
             60 int
             61 bfs(char *start_str, int start_pos)
             62 {
             63     int i, index, h, curp, nxtp, head[2], tail[2];
             64     char suc[] = "123456780";
             65     char nxt[STR_LEN+1];
             66     head[0= head[1= -1;
             67     tail[0= tail[1= 0;
             68     h = hash_func(start_str);
             69     hash[0][h] = 1;
             70     ADD(0, tail[0], start_str, start_pos, -1-1, h);
             71     h = hash_func(suc);
             72     hash[1][h] = 1;
             73     ADD(1, tail[1], suc, 8-1-1, h);
             74     while(head[0]<tail[0&& head[1]<tail[1]) {
             75         for(index=0; index<2; index++) {
             76             ++head[index];
             77             curp = queue[index][head[index]].position;
             78             for(i=0; i<4; i++) {
             79                 if(movable[curp][i]) {
             80                     nxtp = curp + pos[i];
             81                     strcpy(nxt, queue[index][head[index]].str);
             82                     nxt[curp] = nxt[nxtp];
             83                     nxt[nxtp] = '0';
             84                     h = hash_func(nxt);
             85                     if(!hash[index][h]) {
             86                         ++tail[index];
             87                         ADD(index, tail[index], nxt, nxtp, head[index], i, h);
             88                         hash[index][h] = 1;
             89                     }
             90                     if(hash[1-index][h])
             91                         return h;
             92                 }
             93             }
             94         }
             95     }
             96     return -1;
             97 }
             98 
             99 int
            100 main(int argc, char **argv)
            101 {
            102     int i, sp, h;
            103     char input[2], num[STR_LEN+1];
            104     for(i=0; i<STR_LEN; i++) {
            105         scanf("%s", input);
            106         num[i] = input[0];
            107         if(num[i]=='x') {
            108             num[i] = '0';
            109             sp = i;
            110         }
            111     }
            112     num[STR_LEN] = '\0';
            113     h = bfs(num, sp);
            114     if(h==-1)
            115         printf("unsolvable\n");
            116     else {
            117         output(h);
            118         printf("\n");
            119     }
            120     return 0;
            121 }

            posted on 2010-08-06 16:36 simplyzhao 閱讀(200) 評論(0)  編輯 收藏 引用 所屬分類: B_搜索

            導(dǎo)航

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            热久久视久久精品18| 无夜精品久久久久久| 国内精品久久久久国产盗摄| 污污内射久久一区二区欧美日韩| 久久人人爽人人澡人人高潮AV | 亚洲级αV无码毛片久久精品 | 久久久这里有精品中文字幕| 亚洲人成无码www久久久| 日日噜噜夜夜狠狠久久丁香五月| 久久国产精品久久精品国产| 亚洲日本久久久午夜精品| 韩国三级大全久久网站| 亚洲AV无一区二区三区久久| 亚洲国产精品婷婷久久| 日韩人妻无码精品久久免费一| 久久AAAA片一区二区| 丁香五月网久久综合| 国内精品伊人久久久久妇| 久久精品无码av| 精品无码久久久久久久久久 | 亚洲国产精品久久久久婷婷老年| 大香伊人久久精品一区二区| 四虎影视久久久免费观看| 久久亚洲国产精品123区| 久久99久久成人免费播放| 91精品免费久久久久久久久| 久久精品国内一区二区三区| 潮喷大喷水系列无码久久精品| 久久久精品国产sm调教网站 | 婷婷久久五月天| 久久久久久久久久久精品尤物| 久久精品中文字幕一区| 亚洲午夜无码AV毛片久久| 热久久最新网站获取| 久久综合国产乱子伦精品免费| 国产精品视频久久久| 国产午夜精品久久久久九九电影| 午夜精品久久久久成人| 精品久久久中文字幕人妻| 久久香蕉一级毛片| 精品久久久一二三区|