CCF(棋局评估)博弈论+对抗搜索+DFS
201803-4
棋局评估
这题主要使用对抗搜索,也就是每一步寻找可以下棋的位置,通过在这一步下棋看最后会取的什么样的分数。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
int map[3][3];
bool row(int i,int p){return map[i][1]==p&&map[i][2]==p&&map[i][0]==p;}
bool col(int j,int p){return map[0][j]==p&&map[1][j]==p&&map[2][j]==p;}
bool tan(int p){
if(map[0][0]==p&&map[1][1]==p&&map[2][2]==p)
return true;
else if(map[0][2]==p&&map[1][1]==p&&map[2][0]==p)
return true;
return false;
}
int calans(){//计算空格有多少
int ans=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(!map[i][j])
ans++;
}
}
return ans;
}
int grade(int p){//当前局面得分多少,p=1表示Alice,2表示Bob
int ans=calans();
ans+=1;
bool flag=false;
if(row(0,p)||row(1,p)||row(2,p))
flag=true;
if(col(1,p)||col(2,p)||col(0,p))
flag=true;
if(tan(p))
flag=true;
if(!flag)
return 0;
else{
return p==1?ans:-ans;
}
}
int dfs(int p){//现在下棋的是p
if(calans()==0)
return 0;
int maxs=-INF;
int mins=INF;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(!map[i][j]){//如果这里是空格,可以下棋
map[i][j]=p;
int gra=grade(p);
if(gra!=0){//这里已经可以判断胜负了
map[i][j]=0;
return gra>0?max(maxs,gra):min(mins,gra);
}
if(p==1){//当前是Alice
maxs=max(maxs,dfs(2));
}else{//当前是Bob
mins=min(mins,dfs(1));
}
map[i][j]=0;
}
}
}
if(p==1)
return maxs;
return mins;
}
int main(){
int t;
cin>>t;
while(t--){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>map[i][j];
}
}
int gra1=grade(1);
int gra2=grade(2);
if(gra1){
cout<<gra1<<endl;
continue;
}
if(gra2){
cout<<gra2<<endl;
continue;
}
cout<<dfs(1)<<endl;
}
//system("pause");
return 0;
}
Either Excellent or Rusty
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了