首页 > 代码库 > [POJ1185]炮兵阵地(状压DP)
[POJ1185]炮兵阵地(状压DP)
题目链接:http://poj.org/problem?id=1185
这个和之前的不一样,在于某个点影响的范围是两格。那么dp(cur,pre,i)表示第i行状态为cur,i-1行状态为pre时可以有多少种放法。转移的时候枚举ppre,就是i-2行即可。照葫芦画瓢
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%I64d", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onecnt(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 const int maxn = 101; 72 const int maxm = 11; 73 char tmp[maxm]; 74 int dp[1<<maxm][1<<maxm][maxn]; 75 int G[maxn]; 76 int n, m; 77 78 bool ok(int x, int i) { 79 if((x & G[i]) != x) return 0; 80 if((x << 1) & x != 0) return 0; 81 if((x << 2) & x != 0) return 0; 82 return 1; 83 } 84 85 signed main() { 86 // FRead(); 87 while(~scanf("%d%d",&n,&m)) { 88 Cls(G); 89 int mm = 1 << m; 90 For(i, 1, n+1) { 91 Rs(tmp); 92 for(int j = m - 1; j >= 0; j--) { 93 G[i] <<= 1; 94 G[i] |= (tmp[j] == ‘P‘ ? 1 : 0); 95 } 96 } 97 Rep(i, mm) { if(!ok(i, 1)) continue; 98 dp[i][0][1] = __builtin_popcount(i); 99 }100 Rep(i, mm) { if(!ok(i, 1)) continue;101 Rep(j, mm) { if(!ok(j, 2)) continue;102 if(i & j) continue;103 dp[i][j][2] = max(dp[i][j][2], dp[i][0][1] + __builtin_popcount(i));104 }105 }106 For(i, 3, n+1) {107 Rep(cur, mm) { if(!ok(cur, i)) continue;108 Rep(pre, mm) { if(!ok(pre, i-1)) continue;109 if(cur & pre) continue;110 Rep(ppre, mm) {111 if((cur & pre) || (cur & ppre) || (pre & ppre)) continue;112 dp[cur][pre][i] = max(dp[cur][pre][i], dp[pre][ppre][i-1] + __builtin_popcount(cur));113 }114 }115 }116 }117 int ret = 0;118 Rep(i, mm) {119 Rep(j, mm) {120 ret = max(ret, dp[i][j][n]);121 }122 }123 printf("%d\n", ret);124 }125 RT 0;126 }
[POJ1185]炮兵阵地(状压DP)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。