Vijos 1193 扫雷(状态压缩DP)
思路很简单。。。但是细节啊。。。。WA了好多次。。。
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <string> 5 #include <ctime> 6 #include <cstdlib> 7 using namespace std; 8 int p[10001]; 9 int dp1[10],dp2[10],o[3]; 10 int main() 11 { 12 int i,j,n,k; 13 scanf("%d",&n); 14 for(i = 1;i <= n;i ++) 15 { 16 scanf("%d",&p[i]); 17 } 18 if(p[1] == 1) 19 { 20 dp1[1] = 1; 21 dp1[2] = 1; 22 } 23 else if(p[1] == 0) 24 dp1[0] = 1; 25 else if(p[1] == 2) 26 { 27 dp1[3] = 1; 28 } 29 if(n == 1) 30 { 31 if(p[1] == 1||p[1] == 0) 32 printf("%d\n",1); 33 else 34 printf("0\n"); 35 return 0; 36 } 37 else if(n == 2) 38 { 39 if(p[1] == 0&&p[2] == 0) 40 printf("1\n"); 41 else if(p[1] == 1&&p[2] == 1) 42 printf("2\n"); 43 else if(p[1] == 2&&p[2] == 2) 44 printf("1\n"); 45 else 46 printf("0\n"); 47 return 0; 48 } 49 for(i = 3;i <= n;i ++) 50 { 51 for(j = 0;j < 4;j ++) 52 { 53 o[0] = o[1] = 0; 54 for(k = 0;k <= 1;k ++) 55 { 56 if(j&(1<<k)) 57 o[k] ++; 58 } 59 if(o[0]+o[1] == p[i-1]) 60 { 61 dp2[o[1]] += dp1[j]; 62 } 63 else if(o[0]+o[1]+1 == p[i-1]) 64 { 65 dp2[o[1]+2] += dp1[j]; 66 } 67 } 68 for(j = 0;j < 4;j ++) 69 { 70 dp1[j] = dp2[j]; 71 dp2[j] = 0; 72 } 73 } 74 if(p[n] == 2) 75 printf("%d\n",dp1[3]); 76 else if(p[n] == 0) 77 printf("%d\n",dp1[0]); 78 else if(p[n] == 1) 79 printf("%d\n",dp1[1]+dp1[2]); 80 else 81 printf("0\n"); 82 return 0; 83 }