CCF-CSP题解 201803-4 棋局评估
求当前井字棋局的得分。
用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 棋局评估的更多相关文章
- ccf 201803-4 棋局评估(Python实现)
一.原题 问题描述 试题编号: 201803-4 试题名称: 棋局评估 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很 ...
- ccf 201803-4 棋局评估 (对抗搜索)
棋局评估 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很简单:两人轮流往3*3的棋盘中放棋子,Alice放的是“X”,Bob放的是“O”,Alice执先.当同一种棋子占据一行.一列 ...
- CCF(棋局评估)博弈论+对抗搜索+DFS
201803-4 棋局评估 这题主要使用对抗搜索,也就是每一步寻找可以下棋的位置,通过在这一步下棋看最后会取的什么样的分数. #include<iostream> #include< ...
- CCF CSP 201703-3 Markdown
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-3 Markdown 问题描述 Markdown 是一种很流行的轻量级标记语言(l ...
- CCF CSP 201312-3 最大的矩形
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201312-3 最大的矩形 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i( ...
- CCF CSP 201609-3 炉石传说
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201609-3 炉石传说 问题描述 <炉石传说:魔兽英雄传>(Hearthston ...
- CCF CSP 201403-3 命令行选项
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201403-3 命令行选项 问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些 ...
- CCF CSP 201709-4 通信网络
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201709-4 通信网络 问题描述 某国的军队由N个部门组成,为了提高安全性,部门之间建立了M ...
- CCF CSP 201409-3 字符串匹配
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201409-3 字符串匹配 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那 ...
- CCF CSP 201503-3 节日
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201503-3 节日 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形 ...
随机推荐
- [Ubuntu篇] 在ubuntu上源码编译gtest,编写gtest-config.cmake并测试
本文首发于个人博客https://kezunlin.me/post/4a1427cf/,欢迎阅读! compile gtest on ubuntu 16.04 Guide compile gtest ...
- Mac安装和卸载Mysql
目录 一.安装 二.环境变量 2.1 MySQL服务的启停和状态的查看 三.启动 四.初始化设置 4.1 退出sql界面 五.配置 5.1 检测修改结果 一.安装 第一步:打开网址,https://w ...
- nexus https proxy
- [FPGA]Verilog实现8位串并转换器HC595
目录 想说的话... 正文 IC介绍_HC595 电路连接图 功能表 逻辑图 代码实现 代码已经更新,新的代码按照电路编写,忠实于原电路的逻辑,已注于文末(11/16) 修复并行输出数据出错的bug, ...
- 闲来无事写了一套 Jenkins 主题样式:刀锋
背景 Jenkins 的前端 CSS 样式坚挺了这么多年已经觉得腻的不行了,于是想换个风格缓解一下视觉疲劳,便有了这个项目.由于本人不是前端,所以很多只是随便改改,有些复杂的需求也实现不了,但是总的来 ...
- Identityserver4中ResourceOwnerPassword 模式获取refreshtoken
一.IS4服务端配置 1.配置Client new Client { ClientId = "xamarin", ClientSecrets = { ".Sha256() ...
- 搭建nextcloud私有云存储网盘
简介: 搭建个人云存储一般会想到ownCloud,堪称是自建云存储服务的经典.而Nextcloud是ownCloud原开发团队打造的号称是“下一代”存储. 真正试用过后就由衷地赞同这个Nextclou ...
- 简单地认识一下 HTML
简单复盘一下 HTML. 1.HTML 什么是 HTML?HTML 是 Hyper Text Markup Language 的简写,译成中文是「超文本标记语言」. 顾名思义,超文本,就是不止于文本, ...
- jenkins System error
背景 在使用WAR包安装jenkins后,启动tomcat,显示启动成功,但最后提示信息如下: 04-Dec-2018 03:28:21.563 WARNING [Computer.threadPoo ...
- Djangoday2第二个app加减法
第二个app 计算新建一个app在view定义显示的内容修改urls指定连接对应的视图测试另一种通过路径传参的方式访问网址路径传参的urls定义方法网址路径传参测试urls的urlnamedjango ...