拯救机器人
题意:
给出一个n * m的矩形,现在有一个出口用'E'表示,'.'表示空地,'o'表示此处有一个机器人,现在你可以指挥所有的机器人往上下左右走一步,如果有一个机器人走到了出口'E',那么这个机器人就被拯救,如果有机器人走出了边界,那么这个机器人将会爆炸,问最多拯救多少个机器人。
题解:
目前还不是很懂,贴个代码挖个坑,以后来细细品味。
代码:
#include <bits/stdc++.h> using namespace std; const int N = 2e2 + 7; int sum[N][N], ai[N][N], aj[N][N], f[N][N], s[N][N]; int n, m, si, sj, ans; int getsum (int i1, int j1, int i2, int j2) { return sum[i2][j2] - sum[i1 - 1][j2] - sum[i2][j1 - 1] + sum[i1 - 1][j1 - 1]; } int main () { scanf ("%d%d", &n, &m); for (int i = 1; i <= n; ++ i) { char ch[N]; scanf ("%s", ch + 1); for (int j = 1; j <= m; ++j) { if (ch[j] == 'o') s[i][j] = 1; else if (ch[j] == '.') s[i][j] = 0; else si = i, sj = j; } } if (si > (n + 1) / 2) { si = n + 1 - si; for (int il = 1, ir = n; il < ir; ++ il, -- ir) for (int j = 1; j <= m; ++ j) swap(s[il][j], s[ir][j]); } if (sj > (m + 1) / 2) { sj = m + 1 - sj; for (int jl = 1, jr = m; jl < jr; ++ jl, -- jr) for (int i = 1; i <= n; ++ i) swap(s[i][jl], s[i][jr]); } for (int i = 1; i <= n; ++ i) { for (int j = 1; j <= m; ++ j) { ai[i][j] = ai[i][j - 1] + s[i][j]; aj[i][j] = aj[i - 1][j] + s[i][j]; sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + s[i][j]; } } for (int i = 1; i <= si; ++ i) { for (int j = 1; j <= sj; ++ j) { int pi = i + si - 1, pj = j + sj - 1; f[1][1] = getsum (i, j, pi, pj); for (int ii = 1; ii + pi - 1 <= n - (si - i); ++ ii) for (int ij = 1; ij + pj - 1 <= m - (sj - j); ++ ij) if (ii + ij != 2) { f[ii][ij] = 0; if (ii) f[ii][ij] = max (f[ii][ij], f[ii - 1][ij] + ai[ii + pi - 1][ij + pj - 1] - ai[ii + pi - 1][(j + ij - 1) - 1]); if (ij) f[ii][ij] = max (f[ii][ij], f[ii][ij - 1] + aj[ii + pi - 1][ij + pj - 1] - aj[(i + ii - 1) - 1][ij + pj - 1]); } ans = max (ans, f[n - pi + 1 - (si - i)][m - pj + 1 - (sj - j)]); } } cout << ans << endl; return 0; }