• 作者: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 题解 状态压缩+搜索的更多相关文章

  1. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  2. 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 ...

  3. 【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 ...

  4. 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 ...

  5. POJ 1632 Vase collection【状态压缩+搜索】

    题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS   Memory Limit: 10000K Total ...

  6. 洛谷P2258 子矩阵 题解 状态压缩/枚举/动态规划

    作者:zifeiy 标签:状态压缩.枚举.动态规划 题目链接:https://www.luogu.org/problem/P2258 这道题目状态压缩是肯定的,我们需要用二进制来枚举状态. 江湖上有一 ...

  7. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  8. JZYZOJ 1385 拉灯游戏 状态压缩 搜索

    http://172.20.6.3/Problem_Show.asp?id=1385   刚开始想的时候一直以为同一排不同的拉灯顺序对结果是有影响的,手推了好多遍才发现拉灯结果只和拉的灯有关,这也要打 ...

  9. 洛谷P1433 吃奶酪 题解 状态压缩DP

    题目链接:https://www.luogu.com.cn/problem/P1433 题目大意 房间里放着 \(n\) 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 \((0, ...

随机推荐

  1. oracle-Normal

    从shutdown normal命令发布起, 禁止建立任何新的oracle连接. 数据库将等到所有用户都被断开后再继续关闭过程. 等待当前所有已连接的用户断开与数据库的连接

  2. 洛谷P1929 迷之阶梯

    P1929 迷之阶梯 题目描述 在经过地球防卫小队的数学家连续多日的工作后,外星人发的密码终于得以破解.它 告诉我们在地球某一处的古老遗迹中,存在有对抗这次灾难的秘密武器.防卫小队立即赶 到这处遗迹. ...

  3. spark-ML基础

    一.ML组件 ML的标准API使用管道(pipeline)这样的方式,可以将多个算法或者数据处理过程整合到一个管道或者一个流程里运行,其中包含下面几个部分: 1. dataFrame:用于ML的dat ...

  4. 递归系列——树型JSON数据转换问题

    JSON数据转换方式: 1.标准结构=>简单结构 var root = { id: 'root', children: [ { id: "1", children: [ { ...

  5. 关于JSP的淘汰问题(转)

    来源:http://1t.click/peD 大中型公司需要专业人才,小公司需要全才,但是对于个人职业发展来说,我建议是分开.你要是这辈子就吃java这碗饭,就不要去研究什么css,js等等. 把你的 ...

  6. Python编码---转自金角大王

    本节内容 编码回顾 编码转换 Python的bytes类型 编码回顾 在备编码相关的课件时,在知乎上看到一段关于Python编码的回答 这哥们的这段话说的太对了,搞Python不把编码彻底搞明白,总有 ...

  7. 2018-11-21-WPF-解决-ViewBox--不显示线的问题

    title author date CreateTime categories WPF 解决 ViewBox 不显示线的问题 lindexi 2018-11-21 09:37:53 +0800 201 ...

  8. PHP汉字验证码

    转自:http://www.blhere.com/1167.html 12345678910111213141516171819202122232425262728293031323334353637 ...

  9. mysql通过日志恢复数据库

    案例:http://www.linuxidc.com/Linux/2012-11/74005.htm http://blog.csdn.net/ssrc0604hx/article/details/1 ...

  10. Oracle使用——impdp导入数据时数据表已经存在

    背景 在做数据迁移时,需要将不同地方的dmp文件整合到一个数据库中,在导入时,目标表已经存在,该如何把数据追加进入目标表中 方法介绍 当使用IMPDP完成数据库导入时,如遇到表已存在时,Oracle提 ...