Codeforces 354B dp Game with Strings dp

Game with Strings

题意并不是在图上走,看了好久才看出来。。

dp[ i ][ mask ]表示从 i 层开始走,起点有mask个, a的个数-b的个数的  最大值或者最小值。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n;
int dp[ * ][ << ];
bool vis[ * ][ << ];
char Map[][];
int id[][]; struct Point {
int x, y;
}; vector<Point> vc[]; int dfs(int d, int mask, int op) {
if(d == n * - ) return ;
int& ans = dp[d][mask];
if(vis[d][mask]) return ans;
ans = op ? inf : -inf;
int up = SZ(vc[d]);
for(char c = 'a'; c <= 'z'; c++) {
int nxtmask = , sco = ;
if(c == 'a') sco = ;
else if(c == 'b') sco = -;
for(int i = ; i < up; i++) {
if(mask >> i & ) {
int x = vc[d][i].x, y = vc[d][i].y + ;
if(y < n && Map[x][y] == c) {
nxtmask |= << id[x][y];
}
x = vc[d][i].x + , y = vc[d][i].y;
if(x < n && Map[x][y] == c) {
nxtmask |= << id[x][y];
}
}
}
if(nxtmask) {
if(op) chkmin(ans, dfs(d + , nxtmask, op ^ ) + sco);
else chkmax(ans, dfs(d + , nxtmask, op ^ ) + sco);
}
}
vis[d][mask] = true;
return ans;
} int main() {
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%s", Map[i]);
for(int i = ; i < n; i++) {
int x = i, y = ;
while(x < n && y < n && x >= && y >= ) {
id[x][y] = SZ(vc[i]);
vc[i].push_back(Point{x, y}), x--, y++;
}
}
for(int i = n; i < * n; i++) {
int x = n - , y = i - n + ;
while(x < n && y < n && x >= && y >= ) {
id[x][y] = SZ(vc[i]);
vc[i].push_back(Point{x, y}), x--, y++;
}
}
int judge = dfs(, , );
if(Map[][] == 'a') judge++;
else if(Map[][] == 'b') judge--;
if(judge > ) puts("FIRST");
else if(judge < ) puts("SECOND");
else puts("DRAW");
return ;
} /*
*/
上一篇:话说GET与POST那点恩怨


下一篇:linux 变量定义