求当前井字棋局的得分。

用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. PowerMock学习(五)之Verifying的使用

    前言 Verifying是一个非常强大的测试工具,在mock系列框架中使用广泛,主要用于验证方法是否被调用,下面将举例说明. 场景 模拟这样一个场景,通过Dao查询学生,如果存在更新原来学生,不存在则 ...

  2. python CGI编程---Apache服务安装(2)

    一.下载Apache 下载地址:https://www.apachehaus.com/cgi-bin/download.plx 我这里下载第一个,我电脑是window的64位. 下载完成后,解压到 我 ...

  3. HTML学习 day04

    1.字体.文本声明 声明语句必须要包含在{}号之中: 属性和属性值之间用":"分隔: 当有多个属性时,用":"进行区分: 在书写属性时属性之间使用空格.换行等, ...

  4. python爬取中国知网部分论文信息

    爬取指定主题的论文,并以相关度排序. #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests import linecache impor ...

  5. 【搞定面试官】谈谈你对JDK中Executor的理解?

    ## 前言 随着当今处理器计算能力愈发强大,可用的核心数量越来越多,各个应用对其实现更高吞吐量的需求的不断增长,多线程 API 变得非常流行.在此背景下,Java自JDK1.5 提供了自己的多线程框架 ...

  6. scikit-learn文本特征提取之TF-IDF

    TF-IDF(term frequency–inverse document frequency)是一种用于资讯检索与文本挖掘的常用加权技术. TF-IDF是一种统计方法,用以评估一字词对于一个文件集 ...

  7. CentOS 7 安装 dnsmasq 服务 实现内网DNS

    目录 安装 配置 服务管理 测试解析 安装 废话不多述,上来就安装 yum install -y bind-utils dnsmasq 配置 [root@jenkins ~]# rpm -ql dns ...

  8. No provider available for the service com.xxx.xxx 错误解决

    HTTP Status 500 - Servlet.init() for servlet springmvc threw exception type Exception report message ...

  9. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  10. node.js多进程架构

    node.js是单进程应用,要充分利用多核cpu的性能,就需要用到多进程架构. 作为web服务器,不能多个进程创建不同的socket文件描述符去accept网络请求, 有经验的同学知道,如果端口被占用 ...