• 作者: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. JRE System Library、Referenced Libraries、Web App Libraries的含义

    JRE System Library.Referenced Libraries.Web App Libraries 这三个都是jar包的存放集合. JRE System Library:指Java S ...

  2. optim.py cs231n

    n如果有错误,欢迎指出,不胜感激 import numpy as np """ This file implements various first-order upda ...

  3. python ndarray相关操作:转置、翻转

  4. 【codeforces Manthan, Codefest 17 C】Helga Hufflepuff's Cup

    [链接]h在这里写链接 [题意]     k是最高级别的分数,最高界别的分数最多只能有x个.     1<=k<=m;     和k相邻的点的分数只能小于k;     n个点的树,问你每个 ...

  5. R语言与显著性检验学习笔记

    R语言与显著性检验学习笔记 一.何为显著性检验 显著性检验的思想十分的简单,就是认为小概率事件不可能发生.虽然概率论中我们一直强调小概率事件必然发生,但显著性检验还是相信了小概率事件在我做的这一次检验 ...

  6. 2018-12-25-win10-uwp-release-因为-Entry-Point-Not-Found-无法启动

    title author date CreateTime categories win10 uwp release 因为 Entry Point Not Found 无法启动 lindexi 2018 ...

  7. 2019-10-21-WPF-多个-StylusPlugIn-的事件触发顺序

    title author date CreateTime categories WPF 多个 StylusPlugIn 的事件触发顺序 lindexi 2019-10-21 08:33:15 +080 ...

  8. Java练习 SDUT-3849_分数四则运算

    分数四则运算 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写程序,实现两个分数的加减法 Input 输入包含多行数 ...

  9. 【NS2】NS2 教學手冊(转载)

    之前做毕设的时候搜索NS2的相关资料,发现这个里面涵盖很广,特此收藏,感谢原作者的辛勤劳作. NS2 教學手冊 ( NS2 Learning Guide) [快速連結區] My works  中文影音 ...

  10. python if elif else 区别

    if data_ori=='医疗': # 医疗 df = pd.read_excel(path_apply + 'apply/YS_ZY_HZSQ_样例.xls', encoding='gbk', e ...