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 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- python代码格式规范
目前的规范基于pep-0008 基本格式 缩进 使用4个空格进行缩进 行宽 每行代码尽量不超过80个字符 理由: 这在查看side-by-side的diff时很有帮助 方便在控制台下查看代码 太长可能 ...
- best code #54 div 2 A 水
A problem of sorting Accepts: 443 Submissions: 1696 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- poj2060——Taxi Cab Scheme(最小路径覆盖)
Description Running a taxi station is not all that simple. Apart from the obvious demand for a centr ...
- JS 中类型鉴别
JS中的基本类型有:数字(Number(NaN,Infinity)),字符串(String),Undefined,Null,Boolean 引用类型有:数组(Array),对象(Object),函数( ...
- <audio>标签HTML5音乐播放器
<audio>标签:用于在文档中表示音频内容.利用它,你可以在你的个人网站上放一首你喜欢的歌. <audio src="music.mp3">< ...
- getQueryString----获取url中得参数
/** * 获取url中得参数 * @param name * @returns {null} */ window.getQueryString = function (name) { var reg ...
- C语言双链表遍历,插入,删除
#include<stdio.h> #include<stdlib.h> #include <string.h> #define bzero(a, b) memse ...
- Iterator与ListIterator的区别
Iterator与ListIterator 相同点:(1)两者都是fail-fast机制,都是作为内部类实现的. 区别:二者的区别主要是功能上的: (1)Iterator实现了接口Iterator,属 ...
- ssm项目,web容器无法初始化项目
在web.xml中配置加载spring时,发现项目无法运行:而去掉spring的配置时,项目可以被初始化. 此时应考虑到spring的配置文件中存在错误,以至于web容器无法对项目成功初始化,在web ...
- Windows、Linux及Mac查看端口和杀死进程
本文介绍如何在Windows.Linux及Mac下查看端口和杀死进程. Windows下查看端口和杀死进程 查看占用端口号的进程号:netstat –ano | findstr "指定端口号 ...