UPD 2026.3.11
发现代码有问题,向着尽量赢的更改方向修改了代码。
第一眼看到这一题时,很明显,可以用 dfs 预处理方案。
我们可以开一个五维数组 ,这五维分别表示我们的两个数,对手的两个数,两方总共的已行动次数(而不是现在是第几步)。 共有四种可能的状态:
-
,表示当前是玩家回合且玩家必胜以及必胜方案。
-
,初始状态,表示之前没有搜索到。
-
,表示玩家已经失败 / 玩家必败。
特殊的,对于每个测试点,由于走 步及以上仍然可以得到该测试的全部分数,因此当已行动次数达到 时,视为玩家胜利。
所以,本道题的流程如下:
-
检查步数,攻击和防御(步数是否足够,攻击是否成功以及攻击导致的数值变化)。
-
判断当前是谁的回合。
-
枚举所有的可能,如果当前是玩家的回合且玩家有一种方案必胜且步数最少,记录方案并返回步数,否则返回对方必胜。如果当前是对方的回合同理。
-
预处理完成后输出必胜方案并处理输入,直到输入结束。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int dp[10][10][10][10][202];//玩家的两个数,对方的两个数,走了几步(走了第200步且玩家未失败视为玩家胜),0~3表示玩家必胜操作,-1表示未标记,-2表示玩家已败/必败
int attack(int x, int y)
{
if(x<y)
{
swap(x, y);
}
if((x==3 && y==3) || (x==5 && y==3) || (x==6 && y==1))
{
return 1;
}
if(x==9 && y==1)
{
return 2;
}
return 0;
}//进攻值
int guard(int x, int y)
{
if(x<y)
{
swap(x, y);
}
if(x==5 && y==1)
{
return 1;
}
if(x==5 && y==5)
{
return 2;
}
return 0;
}//防守值
int dfs(int p0, int p1, int c0, int c1, int step)
{
if(step==201)//200会有一些点错
{
return dp[p0][p1][c0][c1][step]=201;//2:TLE
}
if(step && step%2==0 && guard(p0, p1)<attack(c0, c1))
{
return dp[p0][p1][c0][c1][step]=-2;//3:防守失败
}
if(step && step%2==0 && guard(p0, p1)==2 && attack(c0, c1)==1)
{
p1=1;//结算:1-2
}
else if(step && step%2==0 && guard(p0, p1) && attack(c0, c1)==guard(p0, p1))
{
p0=p1=1;//结算:相等
}
if(step%2==1 && guard(c0, c1)<attack(p0, p1))
{
return dp[p0][p1][c0][c1][step]=step;//4:进攻成功
}
if(step%2==1 && guard(c0, c1)==2 && attack(p0, p1)==1)
{
c1=1;//结算:1-2
}
else if(step%2==1 && guard(c0, c1) && attack(p0, p1)==guard(c0, c1))
{
c0=c1=1;//结算:相等
}
if(dp[p0][p1][c0][c1][step]!=-1)
{
return dp[p0][p1][c0][c1][step];//1:记忆化
}
if(step%2==0)//玩家回合
{
int x;
int re = -2;
x = dfs((p0+c0)%10, p1, c0, c1, step+1);
if(x>=0)
{
re=(re==-2 ? x : min(re, x));
if(re==x)
{
dp[p0][p1][c0][c1][step]=0;
}
}
x = dfs((p0+c1)%10, p1, c0, c1, step+1);
if(x>=0)
{
re=(re==-2 ? x : min(re, x));
if(re==x)
{
dp[p0][p1][c0][c1][step]=1;
}
}
x = dfs(p0, (p1+c0)%10, c0, c1, step+1);
if(x>=0)
{
re=(re==-2 ? x : min(re, x));
if(re==x)
{
dp[p0][p1][c0][c1][step]=2;
}
}
x = dfs(p0, (p1+c1)%10, c0, c1, step+1);
if(x>=0)
{
re=(re==-2 ? x : min(re, x));
if(re==x)
{
dp[p0][p1][c0][c1][step]=3;
}
}
return re;//4&5:赢得了&赢不了
}
//对方回合
int x, re=0;
x = dfs(p0, p1, (p0+c0)%10, c1, step+1);
if(x==-2)
{
return dp[p0][p1][c0][c1][step]=-2;//6:赢不了
}
else
{
re=max(re, x);
}
x = dfs(p0, p1, (p1+c0)%10, c1, step+1);
if(x==-2)
{
return dp[p0][p1][c0][c1][step]=-2;//6:赢不了
}
else
{
re=max(re, x);
}
x = dfs(p0, p1, c0, (p0+c1)%10, step+1);
if(x==-2)
{
return dp[p0][p1][c0][c1][step]=-2;//6:赢不了
}
else
{
re=max(re, x);
}
x = dfs(p0, p1, c0, (p1+c1)%10, step+1);
if(x==-2)
{
return dp[p0][p1][c0][c1][step]=-2;//6:赢不了
}
else
{
re=max(re, x);
}
return dp[p0][p1][c0][c1][step]=re;//7:对方无法获胜
}
int main()
{
for(int i = 0; i<10; i++)
{
for(int j = 0; j<10; j++)
{
for(int k = 0; k<10; k++)
{
for(int l = 0; l<10; l++)
{
for(int step = 0; step<=200; step++)
{
dp[i][j][k][l][step]=-1;
}
}
}
}
}
int p0, p1, c0, c1, prec, prep, id, step=-1;
cin >> p0 >> p1 >> c0 >> c1 >> id;
dfs(p0, p1, c0, c1, 0);
do
{
step++;
if(step)
{
if(prec==0 && prep==0)
{
c0=(c0+p0)%10;
}
if(prec==0 && prep==1)
{
c0=(c0+p1)%10;
}
if(prec==1 && prep==0)
{
c1=(c1+p0)%10;
}
if(prec==1 && prep==1)
{
c1=(c1+p1)%10;
}
if(guard(p0, p1)==2 && attack(c0, c1)==1)
{
p1=1;//结算:1-2
}
else if(guard(p0, p1) && attack(c0, c1)==guard(p0, p1))
{
p0=p1=1;//结算:相等
}
}
if(step>=201)
{
cout << "0 0" << endl;
}
else
{
int d = dp[p0][p1][c0][c1][step];
cout << d/2 << " " << d%2 << endl;
if(d/2==0 && d%2==0)
{
p0=(p0+c0)%10;
}
if(d/2==0 && d%2==1)
{
p0=(p0+c1)%10;
}
if(d/2==1 && d%2==0)
{
p1=(p1+c0)%10;
}
if(d/2==1 && d%2==1)
{
p1=(p1+c1)%10;
}
if(guard(c0, c1)==2 && attack(p0, p1)==1)
{
c1=1;//结算:1-2
}
else if(guard(c0, c1) && attack(p0, p1)==guard(c0, c1))
{
c0=c1=1;//结算:相等
}
}
step++;
}while(cin >> prec >> prep);
return 0;
}本题解在写作完成后使用 DeepSeek 进行了润色。