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学习笔记(二)使用Sublime Text编写简单的Python程序()
一.使用Sublime Text编写Python 1.点击“文件” →”新建文件“ 2.点击”文件“→”保存“,并保存为.py文件 此时已经创建好Python文件了,接下来就可以编写Python程序了 ...
- Katalon系列二十:读写Excle
import org.apache.poi.xssf.usermodel.XSSFSheet import org.apache.poi.xssf.usermodel.XSSFWorkbook Fil ...
- Python使用Pandas高效处理测试数据
转自:https://www.cnblogs.com/keyou1/p/10948796.html 一.思考 1.Pandas是什么? 功能极其强大的数据分析库 可以高效地操作各种数据集 csv格式的 ...
- HTTP请求封装Java工具类
装载自:http://www.open-open.com/lib/view/open1384844838743.html package com.wiker; import java.io.Buffe ...
- cnn.py cs231n
n import numpy as np from cs231n.layers import * from cs231n.fast_layers import * from cs231n.layer_ ...
- 【水滴石穿】mobx-todos
我觉得代码在有些程序员手里,就好像是画笔,可以创造很多东西 不要觉得创意少就叫没有创意,每天进步一点点,世界更美好 首先源码地址为:https://github.com/byk04712/mobx-t ...
- (转)jQuery中append(),prepend()与after(),before()的区别
在jQuery中,添加元素有append(),prepend和 after(),before()两种共四个. 根据字面意思我们可以看出他们分别是追加,添加和之前,之后,意思相近.同时他们又都有添加元素 ...
- nodeJs学习-19 个人博客案例-(1)数据字典
智能社视频27.28 数据字典: 定义: url 300字 admin_table 管理员用户表 ID username varchar(32) password varchar(32) banner ...
- laravle 事务
DB::beginTransaction(); try{ $name = 'abc'; $result1 = Test::create(['name'=>$name]); ...
- springboot 数据访问【转】【补】
六.SpringBoot与数据访问 1.JDBC pom.xml配置 <dependencies> <dependency> <groupId>org.spring ...