• <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...

            堅信:勤能補拙

            PKU 1753 Flip Game

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

            思路:
            首先,觀察題意,可以發(fā)現(xiàn)對于每個piece,最多只有兩種可能性: 翻轉(zhuǎn)一次或者不翻轉(zhuǎn),翻轉(zhuǎn)多次是沒有意義的
            另外,由于整個rectangular大小是4X4,因此可以使用位運算來提高性能

            對于位運算,需要知道的是如何反轉(zhuǎn)任意的某一位:
            巧妙的異或運算 1^x = ~x  0^x = x

            1. 深度優(yōu)先搜索
             1 #define SIZE 4
             2 #define BIT_MAX 16
             3 const int END_BLACK = 0;
             4 const int END_WHITE = (1<<(BIT_MAX))-1;
             5 int min;
             6 
             7 int
             8 flip(int init_state, int bit_num)
             9 {
            10     int x, y;
            11     x = bit_num / SIZE;
            12     y = bit_num % SIZE;
            13     init_state ^= (1<<bit_num);
            14     if(x == 0)
            15         init_state ^= (1<<(bit_num+SIZE));
            16     else if(x == 3)
            17         init_state ^= (1<<(bit_num-SIZE));
            18     else {
            19         init_state ^= (1<<(bit_num+SIZE));
            20         init_state ^= (1<<(bit_num-SIZE));
            21     }
            22     if(y==0)
            23         init_state ^= (1<<(bit_num+1));
            24     else if(y==3)
            25         init_state ^= (1<<(bit_num-1));
            26     else {
            27         init_state ^= (1<<(bit_num+1));
            28         init_state ^= (1<<(bit_num-1));
            29     }
            30     return init_state;
            31 }
            32 
            33 void 
            34 dfs(long state, int depth, int count)
            35 {
            36     if(state==END_BLACK || state==END_WHITE) {
            37         min = count < min ? count : min;
            38         return;
            39     }
            40     if(depth >= BIT_MAX || count >= min)
            41         return;
            42     dfs(state, depth+1, count);
            43     dfs(flip(state, depth), depth+1, count+1);
            44 }

            2. 寬度優(yōu)先搜索
             1 #define SIZE 4
             2 #define BIT_MAX 16
             3 const int END_BLACK = 0;
             4 const int END_WHITE = (1<<(BIT_MAX))-1;
             5 /* up, down, left, right */
             6 const int dx[] = {-1100};
             7 const int dy[] = {00-11};
             8 const int idx_diff[] = {-SIZE, SIZE, -11};
             9 struct State {
            10     long state;
            11     int count; /* minimum number of rounds needed to reach 'state' */
            12 };
            13 struct State queue[1<<BIT_MAX];
            14 int visited[1<<BIT_MAX];
            15 int head, tail;
            16 long state; /* the low 16 bits represents the current state */
            17 
            18 int
            19 is_valid(int x, int y)
            20 {
            21     return (x>=0 && x<SIZE && y>=0 && y<SIZE);
            22 }
            23 
            24 /*
            25  * tricky of bit-operation
            26  * 1 ^ x = ~x
            27  * 0 ^ x = x
            28  */
            29 long
            30 flip(long state, int x, int y)
            31 {
            32     int i, index = x*SIZE + y;
            33     state ^= (1<<index);
            34     for(i=0; i<4; i++) {
            35         if(is_valid(x+dx[i], y+dy[i]))
            36             state ^= (1<<(index+idx_diff[i]));
            37     }
            38     return state;
            39 }

            posted on 2010-07-05 10:51 simplyzhao 閱讀(123) 評論(0)  編輯 收藏 引用 所屬分類: B_搜索

            導(dǎo)航

            <2010年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产免费直播| 人妻精品久久久久中文字幕一冢本 | 久久午夜无码鲁丝片秋霞 | 久久综合亚洲色HEZYO社区| 三级片免费观看久久| 国产69精品久久久久APP下载| 久久国产AVJUST麻豆| 乱亲女H秽乱长久久久| 色综合久久久久| 人妻丰满?V无码久久不卡| 国产精品99久久久久久宅男小说| 中文字幕日本人妻久久久免费| 久久综合国产乱子伦精品免费| 久久精品这里热有精品| 看全色黄大色大片免费久久久| 老男人久久青草av高清| 91精品国产高清91久久久久久| 国产精品99久久久久久www| 人妻精品久久久久中文字幕| 午夜精品久久久久久久| 久久精品国产精品亚洲下载| 久久这里只有精品18| 久久婷婷五月综合色99啪ak| 国产精品久久久久久吹潮| 久久乐国产精品亚洲综合| 26uuu久久五月天| 一本一道久久综合狠狠老| 久久99精品免费一区二区| 亚洲AV无一区二区三区久久 | 久久最新免费视频| 国产精品99精品久久免费| 亚洲美日韩Av中文字幕无码久久久妻妇| 中文字幕久久波多野结衣av| 少妇久久久久久被弄到高潮 | 久久精品中文字幕大胸| 久久综合中文字幕| 久久精品aⅴ无码中文字字幕重口| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久精品国产sm调教网站 | 2020久久精品国产免费| 久久天天躁狠狠躁夜夜躁2014|