Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索
- 作者:zifeiy
- 标签:状态压缩、搜索
题目链接:https://codeforces.com/contest/3/problem/C
题目大意:
有一个 \(3 \times 3\) 的棋盘,给你一个棋盘当前的状态,请你输出当前这个状态对应的描述。
解题思路:
\(3 \times 3\) 的棋盘上一共有9个位置,每个位置只有可能是3种状态:“.”、“0”或“X”。
所以总的状态数有 \(3^9\) 种,我们可以从初始状态开始来搜索遍历得到所有的状态对应的描述(如果一个状态通过搜索遍历不到,那么这个状态就是 illegal 的)。
代码解释
get_status_num():获得当前状态对应的状态码。
status与状态对应关系:
illegal -- 0
first -- 1
second -- 2
the first player won -- 3
the second player won -- 4
draw -- 5
check_who_win():判断当前状态谁赢
- 返回1: the first win
- 返回2:the second win
- 返回0:还没有人赢
check_whos_turn():判断当前是应该谁下棋
- 返回1:first
- 返回2:second
最后我们从初始状态搜索一下,就能够得到所有的状态对应的描述。
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 20000; // 棋盘总共有3^9=19683种状态
char grid[3][4];
int status[maxn];
/**
status与状态对应关系
illegal -- 0
first -- 1
second -- 2
the first player won -- 3
the second player won -- 4
draw -- 5
*/
/*
* get_status_num()获得当前状态对应的状态码
*/
int get_status_num() {
int t = 1, ans = 1;
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j ++) {
int a;
switch (grid[i][j]) {
case 'X': a = 1; break;
case '0': a = 2; break;
default: a = 0;
}
ans += a * t;
t *= 3;
}
}
return ans;
}
/*
* check_who_win判断当前状态谁赢
* 返回1: the first win
* 返回2:the second win
* 返回0:还没有人赢
*/
int check_who_win() {
for (int i = 0; i < 3; i ++) {
if (grid[i][0]!='.' && grid[i][0]==grid[i][1] && grid[i][0]==grid[i][2]) {
return (grid[i][0] == 'X') ? 1 : 2;
}
if (grid[0][i]!='.' && grid[0][i]==grid[1][i] && grid[0][i]==grid[2][i]) {
return (grid[0][i] == 'X') ? 1 : 2;
}
}
for (int i = -1; i <= 1; i += 2) {
if (grid[1][1]!='.' && grid[1][1]==grid[0][1-i] && grid[1][1]==grid[2][1+i]) {
return (grid[1][1] == 'X') ? 1 : 2;
}
}
return 0;
}
/*
* check_whos_turn判断当前是应该谁下棋
* 返回1:first
* 返回2:second
*/
int check_whos_turn() {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < 3; i ++) for (int j = 0; j < 3; j ++) if (grid[i][j] != '.') {
if (grid[i][j] == 'X') cnt1 ++;
else cnt2 ++;
}
if (cnt1 == cnt2) return 1;
else return 2;
}
void init() {
for (int i = 0; i < 3; i ++) for (int j = 0; j < 3; j ++) grid[i][j] = '.';
}
void dfs() {
int status_num = get_status_num();
int winner_id = check_who_win();
if (winner_id) {
status[status_num] = winner_id + 2;
}
else {
int turn_id = check_whos_turn();
status[status_num] = turn_id;
char chess = (turn_id == 1) ? 'X' : '0';
bool has_place_to_put = false;
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j ++) {
if (grid[i][j] == '.') {
has_place_to_put = true;
grid[i][j] = chess;
dfs();
grid[i][j] = '.';
}
}
}
if (!has_place_to_put) {
status[status_num] = 5; // draw
}
}
}
int main() {
init();
dfs();
for (int i = 0; i < 3; i ++) cin >> grid[i];
string ans;
switch (status[get_status_num()]) {
case 0: ans = "illegal"; break;
case 1: ans = "first"; break;
case 2: ans = "second"; break;
case 3: ans = "the first player won"; break;
case 4: ans = "the second player won"; break;
case 5: ans = "draw"; break;
default: break;
}
cout << ans << endl;
return 0;
}
Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索的更多相关文章
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- 【leetcode】1275. Find Winner on a Tic Tac Toe Game
题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...
- 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe
题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...
- POJ 1632 Vase collection【状态压缩+搜索】
题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS Memory Limit: 10000K Total ...
- 洛谷P2258 子矩阵 题解 状态压缩/枚举/动态规划
作者:zifeiy 标签:状态压缩.枚举.动态规划 题目链接:https://www.luogu.org/problem/P2258 这道题目状态压缩是肯定的,我们需要用二进制来枚举状态. 江湖上有一 ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- JZYZOJ 1385 拉灯游戏 状态压缩 搜索
http://172.20.6.3/Problem_Show.asp?id=1385 刚开始想的时候一直以为同一排不同的拉灯顺序对结果是有影响的,手推了好多遍才发现拉灯结果只和拉的灯有关,这也要打 ...
- 洛谷P1433 吃奶酪 题解 状态压缩DP
题目链接:https://www.luogu.com.cn/problem/P1433 题目大意 房间里放着 \(n\) 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 \((0, ...
随机推荐
- python mooc 3维可视化<第一周第二&三单元>
小结: 创建 数据对象 structuredGrid grid 使用contourfilter con PolyDataMapper m Actor a 使用 MaskPoint3D mask gly ...
- cnn.py cs231n
n import numpy as np from cs231n.layers import * from cs231n.fast_layers import * from cs231n.layer_ ...
- Linux的概述与分类
1.Linux的概述 Linux是基于Unix的开源免费的操作系统,由于系统的稳定性和安全性几乎成为程序代码运行的最佳系统环境.Linux是由Linus Torvalds(林纳斯·托瓦兹)起初开发的, ...
- less知识点总结(一)
1.unit 删除或更换单位. 参数: dimension: 带单位或不带单位的数字. unit: (可选) 目标单位,如果省略此参数,则删除单位. See convert for changing ...
- addEventListener-第三个参数 useCapture
转载自:http://www.cftea.com/c/2008/10/MQ0U26KP565GNM5Q.aspaddEventListener-开始 addEventListener-事件流 addE ...
- if (donutString.indexOf("dozen") != -1)是什么意思
if (donutString.indexOf("dozen") != -1)是什么意思 function parseDonuts(donutString) { numDonuts ...
- fedora 安装ftp
fedora默认不安装ftp服务(包括client程序/service程序),需要进行手动安装: yum install ftp(安装client) yum install vsftpd(安装serv ...
- Directx11教程(53) D3D11管线(8) GS的调度执行
原文:Directx11教程(53) D3D11管线(8) GS的调度执行 在前面的教程中,我们分析了VS-PS的shader管线组合执行过程,本章我们分析一下VS-GS-PS的管线执行 ...
- 利用 JavaScript SDK 部署网页版“Facebook 登录”
facebook开发者平台https://developers.facebook.com/ 利用 JavaScript SDK 部署网页版“Facebook 登录” 通过采用 Javascript 版 ...
- jmeter进行的接口测试和压力测试
1.接口测试 接口测试的内容我们之前已经讲过,values-key形式和json串传参形式: 包括的协议有http,webservice(soap),jdbc数据库,java请求 2.参数化 定义:把 ...