SGU 289. Challenging Tic-Tac-Toe
注意一个问题就是不合法状态的判定。一个是点数不对,一个是X赢了,但是0接着下了一个子,一个是0赢了,但X也接着下了子,判断一下就行了。
做法是直接搜索,然后调参数。。。比较难懂的说。
#include <bits/stdc++.h>
#define rep(_i, _n) for(int _i = 1; _i <= _n; ++_i)
char gchar() {
char ret = getchar();
for(; ret == '\n' || ret == '\r' || ret == ' '; ret = getchar());
return ret;
}
typedef long long LL;
typedef double DB;
const int maxn = 0x3f3f3f3f;
using namespace std;
int ch[]; bool win(int *t, int player) {
for(int i = ; i < ; ++i) {
if(t[i * ] == player && t[i * + ] == player && t[i * + ] == player)
return true;
if(t[i] == player && t[i + ] == player && t[i + ] == player)
return true;
}
if(t[] == player && t[] == player && t[] == player) return true;
if(t[] == player && t[] == player && t[] == player) return true;
return false;
} void dfs(int player, int &result) {
if(win(ch, result ^ )) {
// result ^= 1;
return ;
}
else if(player == ) result = ;
int cnt_win_state = , cnt_draw_state = ;
for(int i = ; i < ; ++i) {
if(ch[i] == ) {
ch[i] = (player & ) ^ ;
int tmp = result ^ ;
dfs(player + , tmp);
if(tmp == (result ^ )) ++cnt_win_state;
if(tmp == ) ++cnt_draw_state;
ch[i] = ;
}
}
if(cnt_win_state > ) result = result ^ ;
else if(cnt_draw_state > ) result = ;
} int main() {
#ifndef ONLINE_JUDGE
freopen("chess.in", "r", stdin), freopen("chess.out", "w", stdout);
#endif for(; ;) {
char c = gchar();
if(c == 'Q') break;
if(c == '') ch[] = ;
else if(c == 'X') ch[] = ;
else ch[] = ;
for(int i = ; i <= ; ++i) {
c = gchar();
if(c == 'Q') break;
if(c == '') ch[i] = ;
else if(c == 'X') ch[i] = ;
else ch[i] = ;
}
int cnt1 = , cnt2 = ;
for(int i = ; i < ; ++i)
if(ch[i] == ) ++cnt1;
else if(ch[i] == ) ++cnt2;
// for(int i = 0; i < 9; ++i) printf("%d ", ch[i]);
// puts("");
// printf("%d %d\n", cnt1, cnt2);
if(!(cnt1 - cnt2 == || cnt1 - cnt2 == ) || (win(ch, ) && win(ch, )) || (win(ch, ) && cnt1 > cnt2) || (win(ch, ) && cnt1 == cnt2)) {
printf("Illegal position.\n");
} else {
int result = ((cnt1 + cnt2) & ) ^ ;
dfs(cnt1 + cnt2, result);
if(result == ) printf("Game is a draw.\n");
else if(result == ) printf("X wins.\n");
else printf("0 wins.\n");
}
}
return ;
}
SGU 289. Challenging Tic-Tac-Toe的更多相关文章
- 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 ...
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 【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 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- linux 常见服务端口
Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons) 来执行的.守护进 ...
- Indexing GROUP BY
SQL databases use two entirely different group by algorithms. The first one, the hash algorithm, agg ...
- ACE自适配通信环境简介
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/03/580795.html ACE自适配通信环境 (Adaptive Communicatio ...
- springboot集成junit测试与javamail测试遇到的问题
1.springboot如何集成junit测试? 导入junit的jar包 使用下面注解: @RunWith()关于这个的解释看下这两篇文章: http://www.imooc.com/qadetai ...
- stout代码分支之十二:巧妙的EXIT
在c++中,为了便于定位问题,进程异常退出时,需要获取返回码和错误信息.stout中将这种功能巧妙的封装成EXIT类. #define EXIT(status) __Exit(status).stre ...
- opencv学习---打开摄像头检测个人头像
opencv中具有检测人体各部分的级联分类器,在opencv文件夹里面的sources/data/haarcascades里面. 这里要选择的是能够检测人体头像的还有检测眼睛的级联分类器的文件. 它们 ...
- 无线局域网中RADIUS协议原理与实现
转载自:http://blog.csdn.net/jinhill/article/details/5901042 摘要 RADIUS协议是一个被广泛应用于网络认证.授权和计费的协议.本文在介绍了RA ...
- Hibernate入门(3)- 持久对象的生命周期介绍
在hibernate中对象有三种状态:瞬时态(Transient). 持久态(Persistent).脱管态或游离态(Detached).处于持久态的对象也称为PO(Persistence Objec ...
- vijos 1004 伊甸园日历游戏 博弈+打表找规律
描述 Adam和Eve玩一个游戏,他们先从1900.1.1到2001.11.4这个日期之间随意抽取一个日期出来.然后他们轮流对这个日期进行操作: 1 : 把日期的天数加1,例如1900.1.1变到19 ...
- Ubuntu12.04 GIT安装和使用
一.安装GIT和配置GIT 1.安装GIT apt-get install git 2.配置GIT ##配置用户信息 git config --global user.name "John ...