题解:B4534 [信息与未来 2026] 桌面游戏
CJJ_niuniu
·
·
题解
思路
博弈搜索,每一步强制选剩余最小数字,只枚举填哪个空位。
棋盘填满就计算行乘积和减列乘积和。
递归得到最优差值,根据正负输出结果。
代码
---
```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll calc(int g[3][3])
{
ll r1=1LL*g[0][0]*g[0][1]*g[0][2];
ll r2=1LL*g[1][0]*g[1][1]*g[1][2];
ll r3=1LL*g[2][0]*g[2][1]*g[2][2];
ll x=r1+r2+r3;
ll c1=1LL*g[0][0]*g[1][0]*g[2][0];
ll c2=1LL*g[0][1]*g[1][1]*g[2][1];
ll c3=1LL*g[0][2]*g[1][2]*g[2][2];
ll y=c1+c2+c3;
return x-y;
}
ll dfs(int g[3][3], bool turnX)
{
bool used[10]={false};
int cnt0=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(g[i][j]==0) cnt0++;
else used[g[i][j]]=true;
}
}
if(cnt0==0) return calc(g);
int mn=10;
for(int d=1;d<=9;d++)
{
if(!used[d]){mn=d;break;}
}
ll res;
if(turnX) res=-1e18;
else res=1e18;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(g[i][j]==0)
{
int ng[3][3];
memcpy(ng,g,sizeof(ng));
ng[i][j]=mn;
ll nxt=dfs(ng,!turnX);
if(turnX)
{
res=max(res,nxt);
}
else
{
res=min(res,nxt);
}
}
}
}
return res;
}
int main()
{
int T;cin>>T;
while(T--)
{
int g[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>g[i][j];
}
}
ll val=dfs(g,true);
if(val>0) cout<<"first\n";
else if(val<0) cout<<"second\n";
else cout<<"tie\n";
}
return 0;
}
```