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. 文件缓存tmpfs简单使用

    文件缓存tmpfs基于内存的文件系统,直接使用ram(物理内存)+swap(交换分区) tmpfs缓存文件系统/dev/shm共享内存动态的使用虚拟内存,文件删除后释放内存 特性:1.动态空间使用和动 ...

  2. MySQL数据库的各种存储引擎详解

    原文来自:MySQL数据库的各种存储引擎详解   MySQL有多种存储引擎,每种存储引擎有各自的优缺点,大家可以择优选择使用: MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB ...

  3. 用Go的风格实现素数筛选

    package main import ( "fmt" "time" ) func source(ch chan<- int) { ; i < En ...

  4. Django makemigrations 不行时

  5. 公历和农历转换的JS代码(车)

    <!-- function CalConv(M) { FIRSTYEAR = 1936; LASTYEAR = 2031; LunarCal = [ new tagLunarCal(23, 3, ...

  6. 2018年Android的保活方案效果统计

    一.常见保活方案 1.监听广播:监听全局的静态广播,比如时间更新的广播.开机广播.解锁屏.网络状态.解锁加锁亮屏暗屏(3.1版本),高版本需要应用开机后运行一次才能监听这些系统广播,目前此方案失效.可 ...

  7. Java的家庭记账本程序(F)

    日期:2019.2.17 博客期:034 星期日 我先配置了Android的相关环境,先试着做了Hello World的测试,但是却出现了很严重的问题,问题如下: Unable to get curr ...

  8. day06 数字类型,字符串类型,列表类型

    一:整型int# ======================================基本使用======================================# 1.用途:记录年龄 ...

  9. LeetCode(123):买卖股票的最佳时机 III

    Hard! 题目描述: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必 ...

  10. 论坛IP地址追踪&路由器密码嗅探

    一.论坛IP地址查询 1.任何应用程序部可以调用一个标准的库函数来查看给定名称的主机IP地址.类似地,系统还提供一个逆函致—给定主机的IP地址,查看它所对应的主机名.大多数使用主机名作为参数的应用程序 ...