POJ 1185 炮兵阵地 (状压DP)
Posted dwtfukgv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1185 炮兵阵地 (状压DP)相关的知识,希望对你有一定的参考价值。
题意:中文题。
析:dp[i][s][t] 表示第 i 行状态为 s, 第 i-1 行为 t,然后就很简单了,但是要超内存,实际上状态最多才60个,所以后两维开60就好,
然后又超时间,就一直加优化,提前预处理。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e6 + 10; const int mod = 100000000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int dp[105][65][65]; int st[102], sold[1100]; vector<int> al; int id[1100]; vector<int> state[65]; vector<int> thr[65][65]; bool judge(int s){ if(m == 1) return true; if((s&1) && (s&2)) return false; for(int i = 2; i < m; ++i) if((s&(1<<i)) && ((s&(1<<i-1))||(s&(1<<i-2)))) return false; return true; } int calc(int s){ int ans = 0; for(int i = 0; i < m; ++i) if(s & (1<<i)) ++ans; return ans; } bool judge1(int s, int t){ for(int i = 0; i < m; ++i) if(!(s&(1<<i)) && (t&(1<<i))) return false; return true; } bool judge2(int s, int t){ for(int i = 0; i < m; ++i) if(s&(1<<i) && (t&(1<<i))) return false; return true; } void solve(vector<int> &v, int s){ for(int i = 0; i < al.size(); ++i) if(judge2(s, al[i])) v.push_back(al[i]); } int main(){ while(scanf("%d %d", &n, &m) == 2){ for(int i = 1; i <= n; ++i){ char s[15]; st[i] = 0; scanf("%s", s); for(int j = 0; j < m; ++j) if(s[j] == ‘P‘) st[i] |= 1<<j; } int all = 1 << m; al.clear(); int cnt = 0; for(int i = 0; i < all; ++i) if(judge(i)){ al.push_back(i); sold[i] = calc(i); id[i] = cnt++; } memset(dp, 0, sizeof dp); int ans = 0; for(int i = 0; i < al.size(); ++i){ state[i].clear(); solve(state[i], al[i]); if(judge1(st[1], al[i])){ int idd = id[al[i]]; dp[1][idd][0] = calc(al[i]); ans = max(ans, dp[1][idd][0]); } } for(int i = 0; i < 65; ++i) for(int j = 0; j < 65; ++j) thr[i][j].clear(); for(int i = 0; i < al.size(); ++i) for(int j = 0; j < state[i].size(); ++j) for(int k = 0; k < al.size(); ++k) if(judge2(al[i], al[k]) && judge2(state[i][j], al[k])){ thr[i][id[state[i][j]]].push_back(al[k]); thr[id[state[i][j]]][i].push_back(al[k]); } for(int i = 2; i <= n; ++i) for(int ii = 0; ii < al.size(); ++ii){ int j = al[ii]; if(!judge1(st[i], j)) continue; int idj = id[j]; for(int k = 0; k < state[idj].size(); ++k){ int kk = state[idj][k]; if(!judge1(st[i-1], kk)) continue; int idk = id[kk]; int &res = dp[i][idj][idk]; for(int l = 0; l < thr[idj][idk].size(); ++l){ int ll = thr[idj][idk][l]; int idl = id[ll]; res = max(res, dp[i-1][idk][idl]); } res += sold[j]; ans = max(ans, res); } } printf("%d\n", ans); } return 0; }
以上是关于POJ 1185 炮兵阵地 (状压DP)的主要内容,如果未能解决你的问题,请参考以下文章