题目链接:uva 11534 - Say Goodbye to Tic-Tac-Toe
题目大意:给定一个1*n的个子,每次操作可以选中一个未填过的个子画X或者O,如果该次操作形成了XX或者OO,那么该次操作者视为失败,人为先手,对于给定状态(注意当前状态也算在步数中),问是否可以战胜电脑。
解题思路:对于固定长度,两端的可能有空,X,O,组合情况共有9种,虽然有些情况等价,但是为方便处理,分为9种情况考虑。预先处理出各个长度下9种情况的SG值。然后对于给定状态,枚举位置放X和O,判断新状态的Nim和。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100;
int s[maxn+5][9];
int SG (int l, int x, int y) {
int vis[maxn+5];
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= l; i++) {
if ((i != 1 || x != 1) && (i != l || y != 1)) {
int X = s[i-1][x*3+1] ^ s[l-i][1*3+y];
vis[X] = 1;
}
if ((i != 1 || x != 2) && (i != l || y != 2)) {
int O = s[i-1][x*3+2] ^ s[l-i][2*3+y];
vis[O] = 1;
}
}
int mv = -1;
while (vis[++mv]);
return mv;
}
void init (int n) {
memset(s[0], 0, sizeof(s[0]));
for (int i = 0; i < 9; i++)
s[1][i] = 1;
s[1][1*3+2] = s[1][2*3+1] = 0;
for (int i = 2; i <= n; i++) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++)
s[i][x*3+y] = SG(i, x, y);
}
}
}
char str[maxn+5];
bool judge () {
int len = strlen(str+1);
int l = 0, r = 0, cnt = 0, ans = 0, k = 0;
for (int i = 1; i <= len; i++) {
if (str[i] == '.')
k++;
else {
r = (str[i] == 'X' ? 1 : 2);
ans ^= s[k][l*3+r];
cnt++;
l = r;
k = 0;
}
}
ans ^= s[k][l*3];
if (cnt&1)
ans = (ans == 0 ? 1 : 0);
return ans;
}
int main () {
init(maxn);
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%s", str+1);
printf("%s\n", judge() ? "Possible." : "Impossible.");
}
return 0;
}
分享到:
相关推荐
开源项目-riscie-websocket-tic-tac-toe.zip,tic-tac-toe multiplayer gameserver using gorilla/websocket
Tic Tac Toe game using C language
using simple c++ tic ta to game
有关象棋ai的程序,能够自动下棋。希望同学多多交流
两个人就可以玩Tic Tac Toe.一个玩家画 x
实现带有图形用户界面的Tic-Tac-Toe游戏,游戏双方可以在同一台计算机上玩。(1)游戏有图形界面,图形界面自定义,用鼠标落子,下图仅供参考。(2)在每一步前,需要有标识当前需要落子的是哪位玩家。(3)在每一步后,...
Tic-Tac-Toe-Multiplayer-Firebase-Database-Android-Game 这是流行游戏Tic Tac Toe(也称为X和0)的游戏项目。此游戏在kotlin中使用简单编码,请检查出来。 有两种游戏模式,一种是Player v / s CPU,CPU在其中...
cd ./tic-tac-toe sudo docker build -t tic-tac-toe . sudo docker run -p 8080:8080 tic-tac-toe ###或者 没有码头工人。 在终端运行 git clone https://github.com/leoleoleonid/tic-tac-toe.git cd ./tic-...
A two player tic-tac-toe game.
#3D Tic-Tac-Toe ###Introduction 这是作为静态 Web 应用程序的 tic-tac-toe 的 3D 版本。 该游戏的工作原理类似于经典井字游戏,点沿 3x3x3、4x4x4 或 5x5x5 立方体的格子排列。 游戏可以由两个玩家在一个显示器上...
Tic-Tac-Toe-Endgame:决策树
编写tic-tac-toe游戏程序。即 3*3的9个方格子,每人下一次看谁先连成一行3个。
用JavaScript 和 html5 实现游戏tic tac toe
tic tac toe developed based on python
a game with tic-tac-toe, using J2ME.It can play 3*3, 4*4, 5*5 board.
下载项目 git clone https://github.com/Nayshins/ocaml-tic-tac-toe.gitcd Ocaml-Tic-Tac-Toe运行安装脚本总安装时间需要 5-10 分钟从项目根目录运行$ ./setup.sh 。 按照脚本上的说明进行操作,并在出现提示时输入...
此代码使用python3实现井字棋游戏,此项目在机器算法上可以进行扩展。
this is a tic-tac-toe game.... no one can win...try it.. :)
人工智能作业:实现基于MiniMax Search的tic_tac_toe问题解决方案。
java-tic-tac-toe