POJ 1753 位运算+枚举

题意:

  给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样。

  游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反。

 

思路:

  都是暴搜枚举

  第一种方法:暴力dfs枚举

     棋子只有最多翻一次,因为翻两次后结果和不翻是一样的,所以整个棋盘最多翻16次。

     用step代表翻转的次数,当翻转了step次时,就看一下整个棋盘是否是清一色的。

       当棋盘是清一色的时候就直接输出step,得到的就是最少翻转次数使棋盘清一色。

 

  第二种方法:利用位运算来优化

     因为棋子不是白就是黑,所以可以用0和1来表示。

     然后为每一个棋子编号,并计算出该棋子若翻转会影响到的棋子的位置,可以把它们都看成是二进制的。

    例如 棋子位置是 第二行第二列                     则翻转该棋子会影响到的棋子位置是

                0 0 0 0                      0 1 0 0

                0 1 0 0                      1 0 1 0

                0 0 0 0                      0 1 0 0

                0 0 0 0                      0 0 0 0

    二进制表示        0000 0100 0000 0000              0100 1010 0100 0000

        

    当计算最小值时可以预处理一下,即计算出由全白到该方案最少需要步数以及全黑到该方案需要的最少步数。

    然后每一种方案都可以通过枚举 dp[j] = min(dp[j], dp[i]+1)来得到,dp[i]表示左上的状态,dp[j]表示右上的状态。

    j = 影响的棋子位置^i

    

    再优化之:其实POJ 1753 位运算+枚举,所以用dp[(1<<16-1)^i]得到的状态j即是状态i到全黑的最少步数, 这样就可以减少一个数组的开销了。

    第三种方法:利用棋子翻转的奇偶次数

      预处理时计算出改变该棋子会影响的棋子位置

      然后得到棋盘状态的二进制形式记做p

      然后枚举翻棋子的情况,总共有2^16-1种,看一看如果翻这些牌并且改变了相应受影响位置的牌棋盘后会不会变成清一色

      并记录最少的步数

Tips:

  位运算的优先级比较小,所以应该st应该= (1<<16)-1,而不是1<<16-1

 

Code:

 1 #include <stdio.h>

 2 #include <cstring>

 3 

 4 int dir[5][2] = {0, 0, -1, 0, 0, 1, 1, 0, 0, -1};

 5 

 6 bool G[6][6] = {false};

 7 bool flag;

 8 int step;

 9 void flip(int r, int c)

10 {

11     for (int i = 0; i < 5; ++i) {

12         int rr = dir[i][0]+r, cc = dir[i][1]+c;

13         G[rr][cc] = !G[rr][cc];

14     }

15 }

16 

17 bool check()

18 {

19     for (int i = 1; i <= 4; ++i)

20         for (int j = 1; j <= 4; ++j)

21             if (G[i][j] != G[1][1]) return false;

22     return true;

23 }

24 

25 void dfs(int x, int y, int depth)

26 {

27     if (depth == step) {

28         flag = check();

29         return;

30     }

31     if (flag || y == 5) return;

32 

33     flip(x, y);

34     if (x < 4)

35         dfs(x+1, y, depth+1);

36     else dfs(1, y+1, depth+1);

37 

38     flip(x, y);

39     if (x < 4)

40         dfs(x+1, y, depth);

41     else dfs(1, y+1, depth);

42 }

43 

44 int main()

45 {

46     //freopen("in.txt", "r", stdin);

47     char c;

48     for (int i = 1; i <= 4; ++i) {

49         for (int j = 1; j <= 4; ++j) {

50             scanf("%c", &c);

51             if (c == 'w') G[i][j] = true;

52             else G[i][j] = false;

53         }

54         getchar();

55     }

56     for (step = 0; step <= 16; ++step) {

57         dfs(1, 1, 0);

58         if (flag) break;

59     }

60     if (flag) printf("%d\n", step);

61     else puts("Impossible");

62     return 0;

63 }
第一种方法:暴力dfs+枚举

 

 1 #include <stdio.h>

 2 #include <cstring>

 3 #include <algorithm>

 4 using namespace std;

 5 #define INF 0x1f1f1f1f

 6 

 7 int dir[5][2] = {0, 0, 0, 1, 1, 0, 0, -1, -1, 0};

 8 int eff[16];

 9 int dp[1<<16|1];

10 const int st = (1<<16)-1;

11 

12 bool check(int x, int y)

13 {

14     return x >= 0 && x < 4 && y >= 0 && y < 4;

15 }

16 

17 void init()

18 {

19     int tot = 0;

20     for (int i = 0; i < 4; ++i) {

21         for (int j = 0; j < 4; ++j) {

22             int sta = 0;

23             for (int k = 0; k < 5; ++k) {

24                 int x = i+dir[k][0], y = j+dir[k][1];

25                 if (check(x, y)) sta |= (1<<(x*4+y));///!!!

26             }

27             eff[tot++] = sta;

28         }

29     }

30 }

31 

32 void bfs()

33 {

34     for (int i = 0; i <= st; ++i) {

35         if (dp[i] == INF) continue;   ///!!!

36         for (int j = 0; j < 16; ++j) {

37             int sta = i^eff[j];

38             dp[sta] = min(dp[sta], dp[i]+1);

39         }

40     }

41 }

42 

43 int main()

44 {

45     //printf("__%d\n", st);

46    // freopen("in.txt", "r", stdin);

47     memset(dp, INF, sizeof(dp));

48     dp[0] = 0;

49     init();

50     bfs();

51     char c[5];

52     int p = 0;

53     for (int i = 0; i < 4; ++i) {

54         scanf("%s", c);

55         for (int j = 0; j < 4; ++j) {

56             if (c[j] == 'w') p ^= (1<<(i*4+j));

57         }

58     }

59     if (dp[p] == INF && dp[p^st] == INF) puts("Impossible");

60     else printf("%d\n", min(dp[p], dp[p^st]));

61     return 0;

62 }
第二种方法:利用位运算存状态,并预处理

 

 1 #include <stdio.h>

 2 #include <cstring>

 3 #include <algorithm>

 4 using namespace std;

 5 

 6 const int INF = 0x1f1f1f1f;

 7 const int st = (1<<16)-1;

 8 int dir[5][2] = {0, 0, 0, 1, 1, 0, 0, -1, -1, 0};

 9 int eff[16];

10 

11 bool check(int x, int y)

12 {

13     return x >= 0 && x < 4 && y >= 0 && y < 4;

14 }

15 

16 void init()

17 {

18     int tot = 0;

19     for (int i = 0; i < 4; ++i) {

20         for (int j = 0; j < 4; ++j) {

21             int sta = 0;

22             for (int k = 0; k < 5; ++k) {

23                 int x = i+dir[k][0], y = j+dir[k][1];

24                 if (check(x, y)) sta |= 1<<(4*x+y);

25             }

26             eff[tot++] = sta;

27         }

28     }

29 }

30 

31 int main()

32 {

33     //freopen("in.txt", "r", stdin);

34     init();

35     int ans = INF;

36     char c[6];

37     int p = 0;

38     for (int i = 0; i < 4; ++i) {

39         scanf("%s", c);

40         for (int j = 0; j < 4; ++j) {

41             if (c[j] == 'b') p |= 1<<(i*4+j);

42         }

43     }

44     for (int i = 0; i <= st; ++i) {

45         int sum = 0;

46         int pp = p;

47         for (int j = 0; j < 16; ++j) {

48             if ((1<<j)&i) {

49                 sum++;

50                 pp ^= eff[j];///!!!

51             }

52         }

53         if (pp == 0 || pp == st) ans = min(ans, sum);

54     }

55     if (ans == INF) puts("Impossible");

56     else printf("%d\n", ans);

57     return 0;

58 }
第三种方法:利用枚举翻哪些牌

 

链接:http://poj.org/problem?id=1753

你可能感兴趣的:(poj)