题目:(luogu翻译错的很多)

Alice和Bob玩游戏,每人有8张牌,牌的值为0~4。每一轮当前玩家选择自己的牌A和对手的牌B,然后将A的值变为( A + B )%5,其中A和B都不是0。

当一个人手牌全为0时他就赢了。

T(T<=1e5)组询问,求最后谁赢了,如果都没赢输出Deal。(两个人都是最优方案)

题解:

博弈搜索。

只不过本题有无解情况,因此dfs会卡(应该是我太弱了)。所以考虑用bfs,相当于dfs中直接回溯。

代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int num[][][][],cnt;
int T;
int dp[],ind[];//first:second
void init()
{
for(int a=;a<=;a++)
for(int b=;a+b<=;b++)
for(int c=;a+b+c<=;c++)
for(int d=;a+b+c+d<=;d++)
num[a][b][c][d]=++cnt;
}
int tn(int a,int b)
{
return (a-)*cnt+b-;
}
int cg(int a[],int b[])
{
return tn(num[a[]][a[]][a[]][a[]],num[b[]][b[]][b[]][b[]]);
}
int gt(int a[])
{
int ret = ;
for(int i=;i<=;i++)ret+=(a[i]!=);
return ret;
}
struct node
{
int a[],b[];
int aa(){return num[a[]][a[]][a[]][a[]];}
int bb(){return num[b[]][b[]][b[]][b[]];}
int cc(){return tn(aa(),bb());}
node df()
{
node ret;
for(int i=;i<=;i++)ret.a[i]=b[i],ret.b[i]=a[i];
return ret;
}
}tmp;
queue<node>q;
int a0[],b0[];
void dfsb(int dep,int sum)
{
if(dep==)
{
int c = cg(a0,b0),ga=gt(a0),gb=gt(b0);
ind[c] = ga*gb;
if(!(ga*gb))
{
if(!ga)dp[c]=;
else dp[c]=;
tmp.a[]=-a0[]-a0[]-a0[]-a0[],tmp.a[]=a0[],tmp.a[]=a0[],tmp.a[]=a0[],tmp.a[]=a0[];
tmp.b[]=-b0[]-b0[]-b0[]-b0[],tmp.b[]=b0[],tmp.b[]=b0[],tmp.b[]=b0[],tmp.b[]=b0[];
q.push(tmp);
}
return ;
}
for(b0[dep]=;b0[dep]+sum<=;b0[dep]++)
dfsb(dep+,sum+b0[dep]);
}
void dfsa(int dep,int sum)
{
if(dep==){dfsb(,);return ;}
for(a0[dep]=;a0[dep]+sum<=;a0[dep]++)
dfsa(dep+,sum+a0[dep]);
}
void bfs()
{
while(!q.empty())
{
tmp = q.front();
q.pop();
node v = tmp.df();
for(int j=;j<=;j++)
{
if(!v.b[j])continue;
for(int i=;i<=;i++)
{
if(!v.a[i]||(i-j+)%==)continue;
v.a[i]--;
v.a[(i-j+)%]++;
int c = v.cc(),c0 = tmp.cc();
if(!ind[c])
{
v.a[i]++;
v.a[(i-j+)%]--;
continue;
}
if(dp[c0]==)
{
dp[c]=;
ind[c]=;
q.push(v);
v.a[i]++;
v.a[(i-j+)%]--;
continue;
}else if(!dp[c0])
{
dp[c]=;
}
ind[c]--;
v.a[i]++;
v.a[(i-j+)%]--;
if(ind[c])continue;
if(!dp[c])dp[c]=;
else dp[c]=;
v.a[i]--;
v.a[(i-j+)%]++;
q.push(v);
v.a[i]++;
v.a[(i-j+)%]--;
}
}
}
}
int typ;
int main()
{
init();
dfsa(,);
bfs();
scanf("%d",&T);
while(T--)
{
memset(a0,,sizeof(a0));
memset(b0,,sizeof(b0));
scanf("%d",&typ);
for(int x,i=;i<=;i++)
{
scanf("%d",&x);
a0[x]++;
}
for(int x,i=;i<=;i++)
{
scanf("%d",&x);
b0[x]++;
}
int c = typ?cg(b0,a0):cg(a0,b0);
int ans = dp[c];
if(!ans)
{
printf("Deal\n");
}else
{
if((ans-)^typ)
{
printf("Bob\n");
}else
{
printf("Alice\n");
}
}
}
return ;
}

CF919F A Game With Numbers的更多相关文章

  1. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  2. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  8. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. springboot(十一)SpringBoot任务

    github地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1 ...

  2. springboot整合elasticsearch出错:java.lang.NoClassDefFoundError: org/elasticsearch/plugins/NetworkPlugin

    Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/plugins/NetworkPlugin at java.lang.Clas ...

  3. nginx https反向代理tomcat

    Context体现在server.xml中的Host里的<Context>元素,它由Context接口定义.每个<Context>元素代表了运行在虚拟主机上的单个Web应用. ...

  4. [POI2007]石头花园SKA

    Description Blue Mary是一个有名的石头收藏家.迄今为止,他把他的藏品全部放在他的宫殿的地窖中.现在,他想将他的藏品陈列在他的花园中.皇家花园是一个边长为1000000000单位的平 ...

  5. Matlab实现Butterworth滤波器 分类: 图像处理 2014-06-02 00:05 527人阅读 评论(0) 收藏

    下面是用Matlab实现的Butterworth高通.低通滤波器. clc;clear all;close all; I=imread('cameraman.tif'); subplot(3,2,1) ...

  6. vijos P1426兴奋剂检查 多维费用背包问题的hash

    https://vijos.org/p/1426 这是个好题,容易想到用dp[i][v1][v2][v3][v4][v5]表示在前i个物品中,各种东西的容量是那个的时候,能产生的最大价值. 时间不会T ...

  7. poj3040 Allowance

    思路: 贪心. 看了题解说是 先把面值从大到小排序然后从头往尾扫,只要不超额,能取多少去多少然后如果还有剩余,就从尾往头扫,尽量取,让他恰好超额 不过并不懂证明. 实现: #include <i ...

  8. 让px单位自动转换为rem的方法

    开发工具:      编辑器:vscode;     css预处理器:less;(无具体要求): 步骤:   1. vscode安装cssrem插件:   2. 修改css插件的默认配置,其默认转换p ...

  9. Eclipse打包多渠道包

    下面介绍的是在eclipse中使用gradle进行多渠道打包(添加签名+混淆) 1,首先新建一个android测试项目,结构如下图所示: 2,选中右键选择expert->生成gradle文件,如 ...

  10. iOS Programming UIGestureRecognizer and UIMenuController

    iOS  Programming  UIGestureRecognizer and UIMenuController A UIGestureRecognizer intercepts touches ...