BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)
2024-10-15 05:18:24
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 439 Solved: 379
[Submit][Status][Discuss]
Description
菲菲和牛牛在一块n行m列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手。棋局开始时,棋盘上没有任何棋子,
两人轮流在格子上落子,直到填满棋盘时结束。落子的规则是:一个格子可以落子当且仅当这个格子内没有棋子且
这个格子的左侧及上方的所有格子内都有棋子。
棋盘的每个格子上,都写有两个非负整数,从上到下第i行中从左到右第j列的格子上的两个整数记作Aij、Bij。在
游戏结束后,菲菲和牛牛会分别计算自己的得分:菲菲的得分是所有有黑棋的格子上的Aij之和,牛牛的得分是所
有有白棋的格子上的Bij的和。
菲菲和牛牛都希望,自己的得分减去对方的得分得到的结果最大。现在他们想知道,在给定的棋盘上,如果双方都
采用最优策略且知道对方会采用最优策略,那么,最终的结果如何
Input
第一行包含两个正整数n,m,保证n,m≤10。
接下来n行,每行m个非负整数,按从上到下从左到右的顺序描述每个格子上的
第一个非负整数:其中第i行中第j个数表示Aij。
接下来n行,每行m个非负整数,按从上到下从左到右的顺序描述每个格子上的
第二个非负整数:其中第i行中第j个数表示Bij
n, m ≤ 10 , Aij, Bij ≤ 100000
Output
输出一个整数,表示菲菲的得分减去牛牛的得分的结果。
Sample Input
2 3
2 7 3
9 1 2
3 7 2
2 3 1
2 7 3
9 1 2
3 7 2
2 3 1
Sample Output
2
HINT
Source
用$now[i]$表示第$i$行已经放了$now[i]$个棋子
爆搜之后发现状态数很小
因此用map记忆化一下就好
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define LL long long
using namespace std;
using namespace __gnu_pbds;
const int MAXN = , INF = 1e9 + , base = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M;
int A[MAXN][MAXN], B[MAXN][MAXN];
cc_hash_table<LL, int>mp;
int now[MAXN];
LL gethash() {
LL x = ;
for(int i = ; i <= N; i++) x = x * base + now[i];
return x;
}
void push(LL x) {
for(int i = N; i >= ; i--) now[i] = x % base, x /= base;
}
int getnext() {
int x = ;
for(int i = ; i <= N; i++) x += now[i];
return x & ;
}
int dfs(LL sta) {
if(mp.find(sta) != mp.end()) return mp[sta];
push(sta);
bool type = getnext();
int ans = !type ? -INF : INF;
for(int i = ; i <= N; i++) {
if(now[i] < now[i - ]) {
now[i]++;
LL nxt = gethash();
int val = dfs(nxt);
ans = !type ? max(ans, val + A[i][now[i]]) : min(ans, val - B[i][now[i]]);
now[i]--;
}
}
return mp[sta] = ans;
}
main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#else
freopen("chess2018.in", "r", stdin);
freopen("chess2018.out", "w", stdout);
#endif
N = read(); M = read();
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
A[i][j] = read();
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
B[i][j] = read();
for(int i = ; i <= N; i++) now[i] = M;
mp[gethash()] = ;
dfs();
printf("%d", mp[]);
}
最新文章
- MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN
- UVa 11384 Help is needed for Dexter
- SQL*Loader实验笔记【二】
- uva----11729 Commando war (突击战争)
- oracle11g rman验证备份有效性
- 再看JavaScript线程
- iOS uuchart 用法
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
- Kendo UI 使用小知识点汇总
- centos7 无法启动网络(service network restart)错误解决办法
- Resolving Issues of ";Library Cache Pin"; or ";Cursor Pin S wait on X"; (Doc ID 1476663.1)
- 如何将plist大图拆分成原来的小图
- java中package指什么
- 采用Extjs MVVM + ThinkPHP 架构开发的思考
- Vue热更新报错(log.error(&#39;[WDS] Errors while compiling. Reload prevented.&#39;))
- bip39
- DRF01
- cocos2d-js V3.0 V3.1使用DragonBones
- [LeetCode] 458. Poor Pigs_Easy tag: Math
- django QueryDict对象