注意一个问题就是不合法状态的判定。一个是点数不对,一个是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. JS传递中文参数出现乱码的解决办法

    一.window.open() 乱码: JS中使用window.open("url?param="+paramvalue)传递参数出现乱码,提交的时候,客户端浏览器URL中显示参数 ...

  2. mybatis主键返回的实现

    向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...

  3. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  4. C#泛型实例详解

    本文以实例形式讲述了C#泛型的用法,有助于读者深入理解C#泛型的原理,具体分析如下: 首先需要明白什么时候使用泛型: 当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型. 一.针 ...

  5. LightOJ 1278 - Sum of Consecutive Integers 分解奇因子 + 思维

    http://www.lightoj.com/volume_showproblem.php?problem=1278 题意:问一个数n能表示成几种连续整数相加的形式 如6=1+2+3,1种. 思路:先 ...

  6. Doc常用命令

    1. 获取目录: dir 2. 清屏: cls

  7. redis linux下的环境搭建

    系统  CentOS7 Redis 官网下载   https://redis.io/download 1.下载解压 [root@TestServer-DFJR programs]# /usr/loca ...

  8. job源码分析

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agree ...

  9. 【TYVJ】1520 树的直径

    [算法]树的直径 memset(a,0,sizeof(a)) #include<cstdio> #include<algorithm> #include<cstring& ...

  10. 使用TSQL语句操作MySQL数据库

    使用TSQL语句创建数据库 以前用的是鼠标在界面上手动创建,这样创建会比较麻烦,而且还会经常出问题.在其它电脑上要用的话还需要重复操作.所以要使用程序代码操作,能通过代码的就不用手动操作. 在数据库界 ...