FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283
Problem L Tic-Tac-Toe
Accept: 94 Submit: 184
Time Limit: 1000 mSec Memory Limit : 262144 KB
Problem Description
Kim likes to play Tic-Tac-Toe.
Given a current state, and now Kim is going to take his next move. Please tell Kim if he can win the game in next 2 moves if both player are clever enough.
Here “next 2 moves” means Kim’s 2 move. (Kim move,opponent move, Kim move, stop).

Game rules:
Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
Input
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Each test case contains three lines, each line three string(“o” or “x” or “.”)(All lower case letters.)
x means here is a x
o means here is a o
. means here is a blank place.
Next line a string (“o” or “x”) means Kim is (“o” or “x”) and he is going to take his next move.
Output
For each test case:
If Kim can win in 2 steps, output “Kim win!”
Otherwise output “Cannot win!”
Sample Input
Sample Output
九宫棋Kim先下两步之内是否可以胜利
题解下次贴
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
char map[][];
int s[][],t,sum,k,cnt;
bool judge(int x,int y){
if((x+y)%==){//当此时这个格子的行列的和为奇数时
cnt=,sum=; //cnt代表可能胜利的次数
for(int i=;i<=;i++){
sum+=s[i][y];
}
sum*=k; //同是负数相乘为正数
if(sum==) //空白数为2时代表可以
return true;
else if(sum==) //当sum和为1时,此时可能胜利
cnt++;
sum=; //注意,此时一定要重置sum,因为sum在这个函数的前面后面的含义不同
for(int i=;i<=;i++){
sum+=s[x][i];//计算此时画相同的符号的个数之和
}
sum*=k;
if(sum==) //如果画相同符号的和为2,代表画下一个一定会胜利
return true;
else if(sum==) //如果此时画的相同的符合为1,代表可能胜利
cnt++;
if(cnt==){ //当可能胜利的次数超过2时一定可以胜利
return true;
}
}
else { //当此时这个各自的行列的和为偶数时
cnt=,sum=;
for(int i=;i<=;i++){
sum+=s[i][y];
}
sum*=k;
if(sum==)
return true;
else if(sum==)
cnt++;
sum=;
for(int i=;i<=;i++){
sum+=s[x][i];
}
sum*=k;
if(sum==)
return true;
else if(sum==)
cnt++;
sum=;
if(x==y){ //代表这个空白所在的地方在斜线上
for(int i=;i<=;i++){
sum+=s[i][i];
}
sum*=k; if(sum==)
return true;
else if(sum==)
cnt++;
}
else {
for(int i=;i<=;i++){
sum+=s[i][-i];
}
sum*=k; if(sum==)
return true;
else if(sum==)
cnt++;
}
if(cnt>=){
return true;
}
}
return false;
} int main(){
char st;
int flag;
scanf("%d",&t);
while(t--){
flag=;
memset(s,,sizeof());
for(int i=;i<=;i++){
for(int j=;j<=;j++){
cin>>map[i][j];
if(map[i][j]=='.') s[i][j]=;
else if(map[i][j]=='o') s[i][j]=;
else if(map[i][j]=='x') s[i][j]=-;
}
}
cin>>st; //代表此时Kim所用的符号
if(st=='o') k=;
else if (st=='x') k=-;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(map[i][j]=='.'){
if(judge(i,j))
flag=;
}
}
}
if(flag) puts("Kim win!");
else puts("Cannot win!");
}
return ;
}
FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283的更多相关文章
- 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 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- Golang版本的rocksdb-对gorocksdb的封装
rocksdb的优秀特性不用多说,但是它是用c++语言写的,就是这一个特点就把很多人拦住了.虽然rocksdb官方也有Java版本,但是Golang的发展速度让人不容小觑,而且由于golang原生对高 ...
- Another option to bootup evidence files
When it comes to booting up evidence files acquired from target disk, you got two options. One is VF ...
- HttpsUtils
package io.renren.modules.jqr.util; import java.io.BufferedReader; import java.io.InputStream; impor ...
- Linux基础文件查找
一.文件查找 (一).命令文件 [root@linux ~]# chich ls //从PATH环境变量 [root@linux ~]# chereis vim [root@linux ~]# ech ...
- 十分钟带你看一遍ES6新特性
let , const关键字 var 看习惯了java, 看js真的是忍不住想笑,比如说这个var,它太自由了,自由到{}根本限制不住它的生命周期 js的var关键字,无论在何处声明,都会被视为声明在 ...
- django drf框架中的user验证以及JWT拓展的介绍
登录注册是几乎所有网站都需要去做的接口,而说到登录,自然也就涉及到验证以及用户登录状态保存,最近用DRF在做的一个关于网上商城的项目中,引入了一个拓展DRF JWT,专门用于做验证和用户状态保存.这个 ...
- Switch分销技术解读
Switch分销技术解读 来源:环球旅讯|2009-03-13 当Switch在海外成熟运作近40年后,该业务终于进入中国市场.但对于中国业者来说,知道Switch的人很少,了解Switch的人更少. ...
- postman 测试http,接口
1.form-data: 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时 ...
- 使用ArrayPool池化大型数组(翻译)
原文链接:https://adamsitnik.com/Array-Pool/ 使用ArrayPool 简介 .NET的垃圾收集器(GC)实现了许多性能优化,其中之一就是,设定年轻的对象很快消亡,然而 ...
- Java初学心得(一)
Java中基本组成单元是类,在类中又包含属性和方法. 每个应用程序都包含一个main()方法,main方法里的称为主类. 一,基本变化 ①全局变量:在类中的属性 局部变量:在方法中的属性 ②基本数据类 ...