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 ;
} /*
*/

Codeforces 354B dp Game with Strings dp的更多相关文章

  1. Codeforces 682 D. Alyona and Strings (dp)

    题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...

  2. codeforces 682D D. Alyona and Strings(dp)

    题目链接: D. Alyona and Strings time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp

    题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...

  4. Codeforces 467C George and Job(DP)

    题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...

  5. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  6. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  7. Codeforces 118 D. Caesar's Legions (dp)

    题目链接:http://codeforces.com/contest/118/problem/D 有n个步兵和m个骑兵要排成一排,其中连续的步兵不能超过k1个,连续的骑兵不能超过k2个. dp[i][ ...

  8. Codeforces 710 E. Generate a String (dp)

    题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. d ...

  9. Codeforces 148D Bag of mice 概率dp(水

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...

随机推荐

  1. boost.Asio lib

    Documentation for Boost.Asio http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio.html https://w ...

  2. Ex 2_34 线性3SAT..._第四次作业

  3. java8 lambda方法引用

    注意引用方法的参数列表与返回值类型要与函数式接口中的抽象方法的参数列表与返回值类型保持一致 主要有三种语法格式: * * 对象::实例方法名 * * 类::静态方法名 * * 类::实例方法名 pub ...

  4. 06 元祖 字典 集合set

    元组 定义: ti=() print(ti,type(ti)) 参数:for可以循环的对象(可迭代对象) t2=tuple(") # ('1', '2', '3') <class 't ...

  5. 支付宝调用错误:Call to undefined function openssl_sign()

    打开php.ini,找到这一行 ;extension=php_openssl.dll,将前面的“;”去掉:重启服务器

  6. mysql 定期删除表中无用数据

    MySQL5.1.x版本中引入了一项新特性EVENT,定期执行某些事物,这可以帮助我们实现定期执行某个小功能,不在依赖代码去实现. 我现在有一张表,这张表中的数据有个特点,每天都会有大量数据插入,但是 ...

  7. swift 实践- 02 -- 自定义cell 的简单使用

    import UIKit class MyTableViewCell: UITableViewCell { var imageV: UIImageView? var titleLabel: UILab ...

  8. Confluence 6 从关闭的连接中恢复

    当数据库服务器进行重启或者因为网络问题导致连接中断.所有在数据库连接池中的连接都会被中断.希望处理这个问题,通常需要 Confluence 进行重启. 但是,数据库连接池中的连接可以通过运行 SQL ...

  9. vue之自行实现派发与广播-dispatch与broadcast

    要解决的问题 主要针对组件之间的跨级通信 为什么要自己实现dispatch与broadcast? 因为在做独立组件开发或库时,最好是不依赖第三方库 为什么不使用provide与inject? 因为它的 ...

  10. React基础知识备忘

    section-1 //react组件 export class Halo extends React.Component{ constructor(...args){ super(...args); ...