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, ...
随机推荐
- Liferay如何连接本地的数据库
Liferay自带的数据库非常迷你,一般就是玩玩的. 在真实的开发过程中,我们往往需要把它与我们本地的数据库相连. 有3中方法,我在这里就只介绍我自己最喜欢的方法啦.连的是mysql 1.在Lifer ...
- 数据ETL是指什么
ETL是数据抽取(Extract).清洗(Cleaning).转换(Transform).装载(Load)的过程.是构建数据仓库的重要一环,用户从数据源抽取出所需的数据,经过数据清洗,最终按照预先定义 ...
- Spring_自动组件扫描和 基于注解配置bean
自动组件扫描 启用Spring组件扫描功能. 使用@Component注释来表示这是类是一个自动扫描组件. package com.tanlei.dao; import org.springfram ...
- mac os 使用记录
折腾过一台黑苹果,配置很菜,奔腾G3260和B150M-D2V,具体EFI和配置详见repo Hackintosh 首先要解决的就是写代码问题,mac进行acm(C/C++)编程 VS code天下第 ...
- 【Django入坑之路】Django后台上传图片,以及前端的显示
#setting配置: MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(BASE_DIR, "media") # ...
- spring boot + mybatis 访问 neo4j
之前有通过rest的风格去访问,但是每次需要访问时候将statement一并加入header中去数据库执行,方式简单.且思路清晰,但是不便于形成模板调用,固采用mybaits来集成. 1.关键pom. ...
- php中括号定义数组
php5.3及之前的版本是不支持中括号定义数组的.5.4之后支持. 错误信息是,不识别“[”
- lower_bounder()和upper_bound()的函数
lower_bound() .upper_bound()都运用于有序区间的二分查找. ForwardIter lower_bound(ForwardIter first, ForwardIter la ...
- 使用 VSCODE 在 Windows 10 WSL 中远程开发
使用 VSCODE 在 Windows 10 WSL 中远程开发 安装 VSCODE 1.35+ 版本. 在 VSCODE 中安装 WSL 插件. 点击左下角的 WSL 图标. 打开项目,提示路径. ...
- UVA_10071:Back to High School Physics
Language:C++ 4.8.2 #include<stdio.h> int main(void) { int v, t; while(scanf("%d%d", ...