求当前井字棋局的得分。

用dfs虚构一下搜索树,每个节点对应一个不同的棋局。

每个节点有一个situation()情况评估,若胜负已定,则对应该棋局的评分;否则为0,表示胜负未定或平局。

每个节点还有一个得分用于return,如果situation()值不为0,胜负已定,则节点不再向下拓展,得分即为situation()值;否则若棋盘已满为平局,得分为0,若棋盘未满胜负未定,节点向下拓展,得分需要根据子节点的得分及当前下棋人cur确定。

出题人有一句“当棋盘被填满的时候,游戏结束,双方平手”。Absolutely wrong!棋盘填满不一定平手,一定是先要situation()为0再判断棋盘满不满,以确定是否平手。

#include <bits/stdc++.h>

using namespace std;

struct tNode
{
int chess[9];
tNode()
{
memset(chess, 0, sizeof(chess));
}
tNode(tNode *y)
{
for (int i = 0; i <= 8; i++)
chess[i] = y->chess[i];
}
int remain()
{
int ret = 0;
for (int i = 0; i <= 8; i++)
{
if (chess[i] == 0)
ret ++;
}
return ret;
}
int situation()
{
if (chess[0] == chess[3] && chess[3] == chess[6] && chess[0] != 0)
{
if (chess[0] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[1] == chess[4] && chess[4] == chess[7] && chess[1] != 0)
{
if (chess[1] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[2] == chess[5] && chess[5] == chess[8] && chess[2] != 0)
{
if (chess[2] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[0] == chess[1] && chess[1] == chess[2] && chess[0] != 0)
{
if (chess[0] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[3] == chess[4] && chess[4] == chess[5] && chess[3] != 0)
{
if (chess[3] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[6] == chess[7] && chess[7] == chess[8] && chess[6] != 0)
{
if (chess[6] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[0] == chess[4] && chess[4] == chess[8] && chess[0] != 0)
{
if (chess[0] == 1)
return 1 + remain();
else
return - (1 + remain());
}
if (chess[2] == chess[4] && chess[4] == chess[6] && chess[2] != 0)
{
if (chess[2] == 1)
return 1 + remain();
else
return - (1 + remain());
}
return 0;
}
}; int dfs(tNode *x, int cur)
{
int sit = x->situation();
if (sit != 0)
return sit;
if (x->remain() == 0)
return 0;
int mmax = -20, mmin = 20;
for (int i = 0; i <= 8; i++)
{
if (x->chess[i] == 0)
{
tNode *xx = new tNode(x);
xx->chess[i] = cur;
int temp = dfs(xx, cur % 2 + 1);
mmax = max(mmax, temp);
mmin = min(mmin, temp);
}
}
if (cur == 1)
return mmax;
else
return mmin;
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
tNode *st = new tNode();
for (int i = 0; i <= 8; i++)
scanf("%d", &st->chess[i]); printf("%d\n", dfs(st, 1));
}
return 0;
}

CCF-CSP题解 201803-4 棋局评估的更多相关文章

  1. ccf 201803-4 棋局评估(Python实现)

    一.原题 问题描述 试题编号: 201803-4 试题名称: 棋局评估 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很 ...

  2. ccf 201803-4 棋局评估 (对抗搜索)

    棋局评估 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很简单:两人轮流往3*3的棋盘中放棋子,Alice放的是“X”,Bob放的是“O”,Alice执先.当同一种棋子占据一行.一列 ...

  3. CCF(棋局评估)博弈论+对抗搜索+DFS

    201803-4 棋局评估 这题主要使用对抗搜索,也就是每一步寻找可以下棋的位置,通过在这一步下棋看最后会取的什么样的分数. #include<iostream> #include< ...

  4. CCF CSP 201703-3 Markdown

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-3 Markdown 问题描述 Markdown 是一种很流行的轻量级标记语言(l ...

  5. CCF CSP 201312-3 最大的矩形

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201312-3 最大的矩形 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i( ...

  6. CCF CSP 201609-3 炉石传说

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201609-3 炉石传说 问题描述 <炉石传说:魔兽英雄传>(Hearthston ...

  7. CCF CSP 201403-3 命令行选项

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201403-3 命令行选项 问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些 ...

  8. CCF CSP 201709-4 通信网络

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201709-4 通信网络 问题描述 某国的军队由N个部门组成,为了提高安全性,部门之间建立了M ...

  9. CCF CSP 201409-3 字符串匹配

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201409-3 字符串匹配 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那 ...

  10. CCF CSP 201503-3 节日

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201503-3 节日 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形 ...

随机推荐

  1. 2019-9-18:渗透测试,基础学习,ubuntu搭建LNMP,phpmyadmin

    1,安装nginx:sudo apt-get install nginx 2,安装完成,输入,service nginx start开启服务,输入:netstat -ntulp |grep 80,确定 ...

  2. 新闻实时分析系统-Kafka分布式集群部署

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  3. linux 正确的关机流程

    查看系统的使用状态 1.使用who命令查看在线用户. 2.使用netstat -a或ss -tnl查看网络状态: 3.使用ps -aux 查看后台运行的程序. 通过上述操作可以了解系统目前使用状态,从 ...

  4. 原生JS通过类名(className)获取dom元素

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Precision,Recall,F1的计算

    Precision又叫查准率,Recall又叫查全率.这两个指标共同衡量才能评价模型输出结果. TP: 预测为1(Positive),实际也为1(Truth-预测对了) TN: 预测为0(Negati ...

  6. 关于Go defer的详细使用

    先抛砖引玉defer的延迟调用:defer特性: . 关键字 defer 用于注册延迟调用. . 这些调用直到 return 前才被执.因此,可以用来做资源清理. . 多个defer语句,按先进后出的 ...

  7. 更强、更稳、更高效:解读 etcd 技术升级的三驾马车

    点击下载<不一样的 双11 技术:阿里巴巴经济体云原生实践> 本文节选自<不一样的 双11 技术:阿里巴巴经济体云原生实践>一书,点击上方图片即可下载! 作者 | 陈星宇(宇慕 ...

  8. WebGPU学习(二): 学习“绘制一个三角形”示例

    大家好,本文学习Chrome->webgl-samplers->helloTriangle示例. 准备Sample代码 克隆webgl-samplers Github Repo到本地. ( ...

  9. Android DataBinding不能自动生成ViewDataBinding类的解决方法

    如果Build.gradle和Layout文件配置正确,仍无法生成ViewDataBinding类. 经测试,Gradle的sync无效,clean project无效,invalidate and ...

  10. CA-RNN论文读取

    ***CA-RNN: Using Context-Aligned Recurrent Neural Networks for Modeling Sentence Similarity(CA-RNN:使 ...