注意一个问题就是不合法状态的判定。一个是点数不对,一个是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. Ubuntu16.04 U盘安装Ubuntu16.04制作 光盘刻录 安装与简介

    从今天开始,我会把我遇到过的技术问题一一记录下来,从而分享给有需要的朋友,尽量希望你们少走弯路! 一.首先从官网上下载Ubuntu16.04镜像,下载最好从官网上下载(http://www.ubunt ...

  2. rand、randi和randn的区别?

    1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数 rand(m,n,'double')生成指定精度的均匀分布的伪随机数,参数还可 ...

  3. swift的UIbutton

    override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...

  4. 求前n项正整数的倒数和

    求前n项正整数的倒数和 前n项正整数的和是一个发散的序列,学过高等数学的这个都知道.所以它没有一个精确的公式,但是近似的公式是有的: 1 + 1/2 + 1/3 + …… + 1/n ≍ ln n + ...

  5. C语言双链表遍历,插入,删除

    #include<stdio.h> #include<stdlib.h> #include <string.h> #define bzero(a, b) memse ...

  6. MEF——.NET中值得体验的精妙设计

    摘要:.NET 是 Microsoft XML Web services 平台.MEF是.NET Framework 4.0一个重要的库,Visual Studio 2010 Code Editor的 ...

  7. 【通用邮件发送】C# QQ 网易邮箱

    using BooksStore.Domain.Models; using System; using System.Collections.Generic; using System.Linq; u ...

  8. linux查看内存cpu占用

    linux查看内存cpu占用top 命令  按q退出 可以添加额外选项选择按进程或按用户查看如: top -u gitu PID:进程idPR:进程的优先级别,越小越优先被执行NInice:值VIRT ...

  9. 基于http的追加协议、构建web内容的技术、web的攻击技术(9,10,11)

    第九章 基于http的追加协议 用来提升http的瓶颈,比如Ajax技术,spdy等 第十章 构建web内容的技术 html.css.js等 第十一章 web的攻击技术 比如sql注入攻击.xss等.

  10. poj 2104 可持久化线段树

    我们先离散化,然后根据权值建立线段树,假设我们现在有一颗权值线段树,表示在区间1-n中每个数出现了几次,那么我们可以二分的求出来这个区间的k大值,类似sbt的select操作,那么因为点的权值插入是无 ...