司令部的将军们打算在 N×M 的网格地图上部署他们的炮兵部队。
一个 N×M 的地图由 N 行 M 列组成,地图的每一格可能是山地(用 H
表示),也可能是平原(用 P
表示),如下图。
在每一格平原地形上最多可以布置一支炮兵部队(山地上不能够部署炮兵部队);一支炮兵部队在地图上的攻击范围如图中黑色区域所示:
如果在地图中的灰色所标识的平原上部署一支炮兵部队,则图中的黑色的网格表示它能够攻击到的区域:沿横向左右各两格,沿纵向上下各两格。
图上其它白色网格均攻击不到。
从图上可见炮兵的攻击范围不受地形的影响。
现在,将军们规划如何部署炮兵部队,在防止误伤的前提下(保证任何两支炮兵部队之间不能互相攻击,即任何一支炮兵部队都不在其他支炮兵部队的攻击范围内),在整个地图区域内最多能够摆放多少我军的炮兵部队。
输入格式
第一行包含两个由空格分割开的正整数,分别表示 NN 和 MM;
接下来的 N 行,每一行含有连续的 M 个字符(P
或者 H
),中间没有空格。按顺序表示地图中每一行的数据。
输出格式
仅一行,包含一个整数 K,表示最多能摆放的炮兵部队的数量。
数据范围
N≤100,M≤10
输入样例:
5 4
PHPP
PPHH
PPPP
PHPP
PHHP
输出样例:
6
输入样例:
100 10
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
PPPPPPPPPP
输出样例:
334
dp[i][j][k] 表示第 i 行炮兵的摆法是 j 第 i - 1 行炮兵的摆法是 k,总共内存为 100 * 1024 * 1024,超了
本来想着偷懒用stl节省内存没想到效率过于低下时间又超了代码如下:(超时)
#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
const int N = 100 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m;
int g[N], cnt[1 << 14];
vector<int> state, gg[1 << 14];
unordered_map<int, unordered_map<int, int>> dp[N];
bool check(int i, int j, int k) {
if (i & j) return false;
if (i & k) return false;
if (j & k) return false;
for (int ii = 0; ii < m; ii++) {
if ((i >> ii & 1) && ((i >> (ii + 1) & 1) || (i >> (ii + 2) & 1))) {
return false;
}
}
return true;
}
int count(int x) {
int cnt1 = 0;
while (x) {
cnt1 += x & 1;
x >>= 1;
}
return cnt1;
}
void init() {
cin >> n >> m;
char t;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
cin >> t;
if (t ^ 'P')
g[i] += 1 << j;
}
}
for (int i = 0; i < 1 << m; i++) {
if (check(i, 0, 0))
state.emplace_back(i), cnt[i] = count(i);
}
return;
}
void solve() {
for (int i = 1; i <= n + 2; i++) {
for (auto& ra : state) {
for (auto& rb : state) {
for (auto& rc : state) {
if (check(ra, rc, rb) && !(g[i - 1] & rb | g[i] & ra)) {
dp[i][ra][rb] = max(dp[i][ra][rb], dp[i - 1][rb][rc] + cnt[ra]);
}
}
}
}
}
//int res = 0;
//for (auto& ra : state) {
// for (auto& rb : state) {
// res = max(res, dp[n][ra][rb]);
// }
//}
//cout << res << endl;
cout << dp[n + 2][0][0];
return;
}
int main(void) {
ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
int TT = 1;
//cin >> TT;
for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}
return 0;
}
后面用滚动数组优化就没问题了
#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
const int M = 1 << 10, N = 100 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m;
int g[N], cnt[M];
vector<int> state, gg[M];
int dp[2][M][M];
bool check(int i, int j, int k) {
if (i & j) return false;
if (i & k) return false;
if (j & k) return false;
for (int ii = 0; ii < m; ii++) {
if ((i >> ii & 1) && ((i >> (ii + 1) & 1) || (i >> (ii + 2) & 1))) {
return false;
}
}
return true;
}
int count(int x) {
int cnt1 = 0;
while (x) {
cnt1 += x & 1;
x >>= 1;
}
return cnt1;
}
void init() {
cin >> n >> m;
char t;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
cin >> t;
if (t ^ 'P')
g[i] += 1 << j;
}
}
for (int i = 0; i < 1 << m; i++) {
if (check(i, 0, 0)) {
state.emplace_back(i), cnt[i] = count(i);
for (int j = 1; j <= n; j++) {
}
}
}
return;
}
void solve() {
for (int i = 1; i <= n + 2; i++) {
for (auto& ra : state) {
for (auto& rb : state) {
for (auto& rc : state) {
if (check(ra, rc, rb) && !(g[i - 1] & rb | g[i] & ra)) {
//auto& rd = dp[i][ra][rb], re = dp[i - 1][rb][rc];
dp[i & 1][ra][rb] = max(dp[i & 1][ra][rb], dp[i - 1 & 1][rb][rc] + cnt[ra]);
//rd = max(rd, re + cnt[ra]);
}
}
}
}
}
//int res = 0;
//for (auto& ra : state) {
// for (auto& rb : state) {
// res = max(res, dp[n][ra][rb]);
// }
//}
//cout << res << endl;
cout << dp[n + 2 & 1][0][0];
return;
}
int main(void) {
ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
int TT = 1;
//cin >> TT;
for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}
return 0;
}