CCF201803-4棋局评估【博弈】

该博客通过博弈论的视角解析了一道棋局评估问题,采用DFS深度优先搜索模拟下棋过程,详细阐述了如何在每个可能的棋位上进行操作,并以Alice得分最大化、Bob得分最小化为目标进行递归回溯。提供了完整的AC代码及参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://118.190.20.162/view.page?gpid=T70
思路:博弈论的知识点
用DFS搜索模拟整个下棋的形式,对每一个可以下棋的位置进行下棋,对每一步进行判断,然后递归回溯, A l i c e Alice Alice的得分最大化, B o b Bob Bob的得分最小化。
AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int mp[10][10];
int left(){//计算还剩下多少空没有填
    int cnt=0;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if(mp[i][j]==0){
                cnt++;
            }
        }
    }
    return cnt;
}
bool row(int row,int flag){//判断行是否处于赢的状态
    if(mp[row][0]==flag&&mp[row][1]==flag&&mp[row][2]==flag){
        return true;
    }
    return false;
}
bool col(int col,int flag){//判断列是否处于赢的状态
    if(mp[0][col]==flag&&mp[1][col]==flag&&mp[2][col]==flag){
        return true;
    }
    return false;
}
bool check_win(int flag){//检查当前人是否赢了
    if(row(0,flag)||row(1,flag)||row(2,flag)){
        return true;
    }
    if(col(0,flag)||col(1,flag)||col(2,flag)){
        return true;
    }
    if(mp[0][0]==flag&&mp[1][1]==flag&&mp[2][2]==flag){
        return true;
    }
    if(mp[0][2]==flag&&mp[1][1]==flag&&mp[2][0]==flag){
        return true;
    }
    return false;
}
int cal(int flag){//计算赢的人的得分
    if(!check_win(flag)){
        return 0;
    }
    return flag==1?left()+1:-(left()+1);
}
int dfs(int person){//person==0代表Alice,person==1代表Bob
    if(left()==0){//平局的情况
        return 0;
    }
    int minn=100,maxx=-100;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if(mp[i][j]==0){//这个位置可以放棋子
                mp[i][j]=person+1;
                int tmp=cal(person+1);
                if(tmp){//当前这个人已经赢了
                    mp[i][j]=0;//当前位置置为0,递归回溯
                    return tmp>0?max(maxx,tmp):min(minn,tmp);//tmp>0表示Alice赢了,需要最大化答案,反之则是Bob赢了,需要最小化答案
                }
                if(person==0){
                    maxx=max(maxx,dfs(1));//Alice需要最大化它的值
                }
                else{
                    minn=min(minn,dfs(0));//Bob则需要最小化他的值
                }
                mp[i][j]=0;//重新置为0,递归回溯
            }
        }
    }
    return person==0?maxx:minn;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                cin>>mp[i][j];
            }
        }
        int t1=cal(1),t2=cal(2);
        if(t1!=0){//1代表的是Alice,先判断初始局势是否已经定输赢了
            cout<<t1<<endl;
            continue;
        }
        if(t2!=0){//2代表的是Bob
            cout<<t2<<endl;
            continue;
        }
        cout<<dfs(0)<<endl;
    }
    return 0;
}
/*
3
1 2 1
2 1 2
0 0 0
2 1 1
0 2 1
0 0 2
0 0 0
0 0 0
0 0 0
*/

参考:https://blog.csdn.net/xbb224007/article/details/79935167

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值