POJ1753 Flip Game 翻轉棋盤 方法:bit + BFS

來源:互聯網
上載者:User

題目描述:4*4黑白棋盤,點擊一個棋子,則它的四周及本身變化顏色,求變為純色的最短步數。

方法:用BFS求最短步數,bit儲存狀態,共有2^16=65536種狀態。

 

  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <queue>  5   6 using namespace std;  7   8 bool visit[65536]; // 共有2^16個狀態  9  10 int dir[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}}; 11 int change[16]; // 根據dir[4][2]確定變化位狀態, 通過異或 12  13 struct Node { 14     int state; 15     int step;   16 }; 17  18 void initChange()  19 { 20     int x, y, state, temp; 21      22     for (int i = 0; i < 4; ++i) { 23         for (int j = 0; j < 4; ++j) { 24             state = 0; 25             state = 1 << (3-i)*4+3-j; 26  27             for (int t = 0; t < 4; ++t) { 28                 x = i + dir[t][0]; 29                 y = j + dir[t][1]; 30  31                 if (x < 0 || x > 3 || y < 0 || y > 3) { 32                     continue;     33                 } else { 34                     state ^= 1 << (3-x)*4+3-y;      35                 } 36             } 37             change[i*4+j] = state; 38         } 39     }     40 } 41  42 int bfs(int state) 43 { 44     queue<Node> q; 45     Node cur, next; 46     cur.state = state; 47     cur.step = 0; 48     q.push(cur); 49     memset(visit, false, sizeof(visit)); 50  51     if (cur.state == 0 || cur.state == 0xffff) // 初始就是全w或b的情況 52         return cur.step;     53  54     while (!q.empty()) { 55         cur = q.front(); 56         q.pop(); 57  58         for (int i = 0; i < 16; ++i) {   // bfs 59             next.state = cur.state ^ change[i]; 60             next.step = cur.step + 1; 61  62             if (visit[next.state])  63                 continue; 64  65             if (next.state == 0 || next.state == 0xffff)  66                 return next.step;     67          68             visit[next.state] = true; 69             q.push(next);     70         } 71     } 72  73     return -1; 74 } 75  76 int main() 77 { 78     int state, ans; 79     char ch[4][4]; 80  81     // freopen("temp.txt", "r", stdin); 82     initChange(); 83     state = 0;  84     for (int i = 0; i < 4; ++i) { 85         scanf("%s", ch[i]);     86         for (int j = 0; j < 4; ++j) { 87             if (ch[i][j] == 'b') { 88                 state ^= 1 << (3-i)*4+3-j; 89             } 90         } 91     } 92  93     ans = bfs(state); 94  95     if (ans == -1)  96         puts("Impossible"); 97     else  98         cout << ans << endl; 99     100     return 0;101 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.