注意一个问题就是不合法状态的判定。一个是点数不对,一个是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的更多相关文章

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

  2. POJ 2361 Tic Tac Toe

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

  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. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. python 井字棋(Tic Tac Toe)

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

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

随机推荐

  1. Tomcat启动时报org.springframework.web.context.ContextLoaderListener错误解决方案

    问题现象:新从svn上检出maven的项目maven+spring+springmvc项目在Tomcat启动时,报如下错误. 严重: Error configuring application lis ...

  2. Consul 入门

    1. 什么是Consul? Consul 有很多组件,对于整体来说,它是一个服务发现和服务配置的工具,它提供了一下特性: 服务发现 健康检查 KV存储 多数据中心 2.安装Consul 以下是在 Ce ...

  3. ACE反应器(Reactor)模式(1)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/13/591332.html 1.ACE反应器框架简介 反应器(Reactor):用于事件多路分 ...

  4. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. robots.txt使用和优化技巧

    一.利于网站优化的robots.txt使用技巧 1.在线建站提供方便之路.当我们将域名解析到服务器,可以访问站点了,可是这个时候站点还没有布局好,meta标签还一塌糊涂.乳沟此时的站点被 搜索引擎蜘蛛 ...

  6. [技巧篇]01.Servlet的优化模版代码

    Servlet.java的模版 #---------------------------------------------# # <aw:description>Template for ...

  7. rem自适应js代码

    以后懒得写,直接复制了 var computedFz = (function(){ var designWidth = 375, rem2px = 100; function computedFz() ...

  8. echarts 使用demo

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  9. java中各种循环

    简单介绍一下java中的一些循环 package test; import org.apache.log4j.Logger; import org.junit.Test; public class F ...

  10. Jmeter-12-如何使用Plugin Manager

    1. 搜索 Jmeter plugin 并找到plugin manager 下载jar文件 2. 放到jmeter/lib/ext下面, 重启jmeter 3. 找到选项-> Plugin ma ...