CF 354B - Game with Strings(状压+博弈)
来源:互联网 发布:哪个数据恢复软件好 编辑:程序博客网 时间:2024/05/28 05:18
题意:给出一个n×n的表格,每个位置有一个字母,有两个人轮流选,每次从当前以走的字符串的状态中选择向下或者向右走,最后能得到一个字符串,如果最后走的字符串中‘a’的个数大于'b',那么第一个人赢,如果'b'>'a'第二个人赢,否则平局。需要注意的是每次是从当前字符串进行扩展的,而不是一个准确的位置。比如说下面这组数据:
5
cbbbb
bcbbb
aacbb
aaacb
aaaac
这个数据前两歩最开始的字符串是cb,此时可以在(1,2),(2,1)这两个点对应的字符串都是cb,因此可以从这两个点中任意位置开始扩展,所以下一次第一个人会走cba,最终的结果应该是cbaaaaaac,因此是第一个人赢。
思路:这题题意略坑……刚开始一直没看懂,看了题解+别人代码才明白。假设已经走了x歩,那么我们可以发现,此时可能的位置的范围是在一条对角线的位置,一条对角线长度最多只有20,利用状压的思想,可以用一组二进制数来表示走这条对角线的位置是否是等价的,那么通过这个就可以向下推导下一步的情况。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#define inf 0x3f3f3f3f#define Inf 0x3FFFFFFFFFFFFFFFLL#define eps 1e-9#define pi acos(-1.0)using namespace std;typedef long long ll;char a[25][25];int dp[40][1<<20],n;bool vis[40][1<<20];int f(int xx,int st){ if(vis[xx][st]) return dp[xx][st]; int &res=dp[xx][st]; vis[xx][st]=true; if(xx==n*2-2) { res=0; } else { if(xx&1) res=-inf; else res=inf; int mask[30]; memset(mask,0,sizeof(mask)); int cnt=0; for(int j=0;j<=xx+1;++j) { int x=xx+1-j; int y=j; if(x>=n||y>=n) continue; mask[a[x][y]-'a']|=(1<<cnt); cnt++; } for(int i=0;i<26;++i) { if(mask[i]) { int xt; if(xx+1<n) xt=(st|(st<<1))&mask[i]; else xt=(st|(st>>1))&mask[i]; if(xt==0) continue; int tmp=0; if(i==0) tmp=1; else if(i==1) tmp=-1; if(xx&1) res=max(res,tmp+f(xx+1,xt)); else res=min(res,tmp+f(xx+1,xt)); } } } if(xx==0) { if(a[0][0]=='a') res+=1; else if(a[0][0]=='b') res-=1; } return res;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%d",&n)) { for(int i=0;i<n;++i) scanf("%s",a[i]); memset(vis,0,sizeof(vis)); int res=f(0,1); if(res==0) printf("DRAW\n"); else if(res>0) printf("FIRST\n"); else printf("SECOND\n"); } return 0;}
- CF 354B - Game with Strings(状压+博弈)
- cf 276b 博弈
- CF 277C Game(博弈)
- CF - 223 - B. Two Strings
- CF 559B Equivalent Strings
- 482 C. Game with Strings
- CodeForces - 482C Game with Strings(状压DP,期望)
- Codeforces 482C. Game with Strings 状压DP
- CF482C Game with Strings(期望dp+状压dp)
- CF 578B "Or" Game
- 【cf 681B】 Economy Game
- CF 277C Game(NIM博弈)
- cf 318B. Strings of Power
- CF 226 DIV2 B. Bear and Strings
- CF 447B(DZY Loves Strings-贪心)
- CF 559B(Equivalent Strings-构造法)
- CF 447B DZY Loves Strings(水题)
- CF 455B trie上博弈
- FFT结果的物理意义
- Senium 2.0 通过webdriver提供的api完成各种验证操作
- poj 2823 单调队列
- 内部命令VS外部命令
- 14.IO(三)【File】【Properties】【打印流PrintStream】【序列流SequenceInputStream】
- CF 354B - Game with Strings(状压+博弈)
- IDE, ATA, SCSI
- 高通平台短信复制到sim卡流程
- #R#R读取EXCEL文件方法
- linux常用操作命令
- selenium webdriver 学习总结-浏览器启动方式
- 敏捷开发
- Introduce AJAX about how to dynamic loading HTML/JSON/XML dataType
- Tencent腾讯微信服务号了吗?