CCF-CSP 201803-4 棋局评估 博弈+DFS

news/2024/7/20 20:21:22 标签: 深度优先, 算法, c++

原题链接:CCF-CSP 201803-4 棋局评估

在这里插入图片描述

推荐博客:CSP认证:棋局评估

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f;

int a[3][3];
bool check(int w)
{
    for(int i=0;i<3;i++)
    {
        int res=0;
        for(int j=0;j<3;j++)
        {
            if(a[i][j]==w) res++;
        }
        if(res==3) return true;
    }
    for(int j=0;j<3;j++)
    {
        int res=0;
        for(int i=0;i<3;i++)
        {
            if(a[i][j]==w) res++;
        }
        if(res==3) return true;
    }
    if(a[0][0]==w && a[1][1]==w && a[2][2]==w ) return true;
    if(a[0][2]==w && a[1][1]==w && a[2][0]==w ) return true;
    return false;
}
int eval()
{
    int res=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(a[i][j]==0) res++;
        }
    }
    if(check(1)) return res+1;
    else if(check(2)) return -(res+1);
    else if(res==0) return 0;
    return INF;
}
int dfs(int w)
{
    int tmp=eval();
    if(tmp!=INF) return tmp;
    if(w==1)
    {
        int res=-INF;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(a[i][j]==0)
                {
                    a[i][j]=1;
                    res=max(res,dfs(2));
                    a[i][j]=0;
                }
            }
        }
        return res;
    }
    else
    {
        int res=INF;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(a[i][j]==0)
                {
                    a[i][j]=2;
                    res=min(res,dfs(1));
                    a[i][j]=0;
                }
            }
        }
        return res;
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                cin>>a[i][j];
            }
        }
        cout<<dfs(1)<<endl;
    }
	return 0;
}


http://www.niftyadmin.cn/n/1264550.html

相关文章

CCF-CSP 201803-5 二次求和 30分

原题链接&#xff1a;CCF-CSP 201803-5 二次求和 #include <bits/stdc.h> using namespace std; const int N2010; const int Q1000000007;long long tree[N]; long long mp[N]; vector<long long> adj[N]; set<vector<int>> path;void dfs1(int f…

CCF-CSP 201903-2 二十四点

原题链接&#xff1a;CCF-CSP 201903-2 二十四点 #include <bits/stdc.h> using namespace std; const int N2010;struct node {int num;char op;bool flag; }; map<char,int> op; string str; stack<node> s; queue<node> q;void change() {int nu…

CCF-CSP 201903-3 损坏的RAID5 70分待优化

原题链接&#xff1a;CCF-CSP 201903-3 损坏的RAID5 #include <bits/stdc.h> using namespace std; #define ll long longstruct Node {int f;vector<string> v; }node[1010]; map<int,pair<int,int>> mp;void fun(string& t1,string t2) {map&l…

CCF-CSP 201903-4 消息传递接口

原题链接&#xff1a;CCF-CSP 201903-4 消息传递接口 参考博客&#xff1a;CSP 201903-4 &#xff08;消息传递接口&#xff09;两种满分思路 #include <bits/stdc.h> using namespace std; const int N1e410;struct node {char type;int id; }; int main() {std::ios:…

CCF-CSP 202006-3 Markdown渲染器 100分

原题链接&#xff1a;CCF-CSP 202006-3 Markdown渲染器 写的下面这个代码就是奔着40分去的&#xff0c;结果呢&#xff0c;细节又没把握好&#xff0c;一直只有20分&#xff0c;找错的时候发现自己犯的两个错误&#xff1a; 1.如果一开头就有空格行&#xff0c;需要忽略&#…

CCP-CSP 201912-5 魔数 暴力25

原题链接&#xff1a;CCP-CSP 201912-5 魔数 线段树是写不来的。。。 #include <bits/stdc.h> using namespace std; #define ll unsigned long long const ll mod2009731336725594113; const int N1e610; ll U[5]{314882150829468584,427197303358170108,102229269072…

CCP-CSP 201909-3 字符画 100

原题链接&#xff1a;CCP-CSP 201909-3 字符画 这道题我觉得难点主要是不会处理ASCII码的转换&#xff0c;还有就是看不懂题&#xff0c;其实逻辑不难。 参考博客&#xff1a;CCF CSP 20190903 字符画 100分 #include <bits/stdc.h> using namespace std;int block[2…

CCF-CSP 201909-4 推荐系统 100分

原题链接&#xff1a;CCF-CSP 201909-4 推荐系统 超时20分的代码实用vector存放结构体&#xff0c;然后写了一个cmp结构体排序的函数&#xff0c;所有的结点都存在vector数组中。 满分代码改为用优先队列存储结构体&#xff0c;直接在结构体内排序。 两种方法对于的查询逻辑都…