hihoCoder#1048 状态压缩·二

原题地址

 

位运算的状态压缩太操蛋了,很容易出错。。。又是数组没开够导致诡异现象(明明某个值是1,莫名其妙就变成0了),害我debug一整天!fuck

 

代码:

 1 #include <iostream>

 2 #include <cstring>

 3 

 4 using namespace std;

 5 

 6 #define MAX_N 1024

 7 #define MAX_M 8

 8 #define MAX_S 4096

 9 #define MOD 1000000007

10 

11 int f[MAX_N][MAX_M][MAX_S];

12 int N, M;

13 

14 bool freep(int s, int o) {

15   return !(s & (1 << o));

16 }

17 

18 int mark(int s, int o) {

19   return s |= (1 << o);

20 }

21 

22 int mark(int s, int o1, int o2) {

23   return mark(s, o1) | mark(s, o2);

24 }

25 

26 int main() {

27   cin >> N >> M;

28   memset(f, 0, sizeof(f));

29 

30   for (int i = 1; i <= N; i++) {

31     for (int s = 0; s < (1 << (2 * M + 1)); s++)

32       f[i][M + 1][s] = 1;

33   }

34   for (int j = 1; j <= M; j++) {

35     for (int s = 0; s < (1 << (2 * M + 1)); s++)

36       f[N + 1][j][s] = 1;

37   }

38 

39   for (int i = N; i >= 1; i--) {

40     for (int j = M; j >= 1; j--) {

41       for (int k = (1 << (2 * M)) - 1; k >= 0; k--) {

42         int s = k << 1;

43         // pass

44         if (!freep(s, j)) {

45           if (j < M)

46             f[i][j][s] = f[i][j + 1][s];

47           if (j == M)

48             f[i][j][s] = f[i + 1][1][(s >> M) >> 1 << 1];

49         }

50         if (freep(s, j)) {

51           // impossible

52           if ((j == M || !freep(s, j + 1)) && (i == N || !freep(s, j + M)))

53             f[i][j][s] = 0;

54           // right

55           if (j < M && freep(s, j + 1) && (i == N || !freep(s, j + M)))

56             f[i][j][s] += f[i][j][mark(s, j, j + 1)];

57           // down

58           if (i < N && freep(s, j + M) && (j == M || !freep(s, j + 1)))

59             f[i][j][s] += f[i][j][mark(s, j, j + M)];

60           // right & down

61           if (j < M && freep(s, j + 1) && i < N && freep(s, j + M))

62             f[i][j][s] = (f[i][j][mark(s, j, j + 1)] + f[i][j][mark(s, j, j + M)]) % MOD;

63         }

64       }

65     }

66   }

67 

68   cout << f[1][1][0] << endl;

69 

70   return 0;

71 }

 

你可能感兴趣的:(code)