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

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

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

            1. 深度優先搜索
             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. 寬度優先搜索
             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_搜索

            導航

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产成人99久久亚洲综合精品| 亚洲AV日韩精品久久久久| 亚洲午夜久久久精品影院| 久久久WWW成人免费毛片| 日本高清无卡码一区二区久久 | 久久99热这里只有精品国产| 久久久久久国产精品免费免费| 久久天天躁狠狠躁夜夜不卡| 久久精品国产亚洲麻豆| 午夜精品久久久久久影视riav| 国产精品99久久精品| 国内精品人妻无码久久久影院导航| 丰满少妇高潮惨叫久久久| 欧美一级久久久久久久大| 久久精品国产精品青草| 亚洲色欲久久久综合网| 久久精品中文字幕第23页| 97精品国产91久久久久久| 欧美国产成人久久精品| 久久久久久毛片免费看| 久久久亚洲欧洲日产国码二区| 免费一级欧美大片久久网 | 久久最新免费视频| 久久这里只有精品首页| 久久婷婷激情综合色综合俺也去| 久久亚洲中文字幕精品一区| 久久综合久久综合久久综合| 久久精品天天中文字幕人妻| 精品人妻伦九区久久AAA片69| 精品国产综合区久久久久久| 久久最新精品国产| 国产V亚洲V天堂无码久久久| 久久A级毛片免费观看| 亚洲av日韩精品久久久久久a| 久久99精品久久久大学生| 久久久久亚洲AV成人网人人网站| 色8激情欧美成人久久综合电| 无码精品久久一区二区三区| 少妇被又大又粗又爽毛片久久黑人 | 成人亚洲欧美久久久久| 久久久中文字幕|