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

3
. . .
. . .
. . .
o
o x o
o . x
x x o
x
o x .
. o .
. . x
o

 Sample Output

Cannot win!
Kim win!
Kim win!

九宫棋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的更多相关文章

  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. BeanFactory体系结构

    BeanFactory是Spring中非常重要的一个类,搞懂了它,你就知道了bean的初始化和摧毁过程,对于深入理解IOC有很大的帮助. BeanFactory体系结构 首先看一下使用IDEA生成的继 ...

  2. spring学习笔记之---bean管理的注解方式

    bean管理的注解方式 (一)使用注解定义bean (1)常用注解 (2)实例 1.在pom.xml中进行配置 <dependencies> <dependency> < ...

  3. 解决:Navicat连接不上MySQL 8.0

    转载自 https://www.cnblogs.com/shiysin/p/shiysin.html Navicat连接不上,总是报错1251: 原因是MySQL8.0版本的加密方式和MySQL5.0 ...

  4. centos yum 安装 mariadb

    1. 在 /etc/yum.repos.d/ 下建立 MariaDB.repo,输入内容 [mariadb] name=MariaDB baseurl=http://yum.mariadb.org/1 ...

  5. linux 配置ssh无密码登录不起作用的解决方案

    1.安装ssh 直接 sudo apt-get install openssh-server 2.查看ssh运行状态 ps -e | grep ssh 如果发现 sshd 和 ssh-agent 即表 ...

  6. vue-cli中的跨域之proxytable

    为什么会有跨域? 浏览器有一个叫做同源策略的东西.同源策略限制了从同一个源加载的文档或脚本如何与来自另一个源的资源进行交互.这是一个用于隔离潜在恶意文件的重要安全机制. 同源策略规定了如果两个页面的协 ...

  7. CSV Data Set Config 详细使用说明

    JMeter 5.1.1 CSV Data Set Config 场景一:线程组中设置:单线程执行1次 如上图所示:变量名称为空时JMeter默认把new 1.txt的文件首行作为变量名 再如:此时A ...

  8. C#自动计算字符串公式的四种方法

    原地址:https://blog.csdn.net/ifu25/article/details/53292134 四种方式 简单粗暴:利用SQL数据库计算 功能强大:利用JavaScript计算 看不 ...

  9. spring-boot-plus运维部署(八)

    spring-boot-plus运维部署 线上部署 打包环境为prod mvn clean package -Pprod 打包后的目录 cd target/spring-boot-plus-1.2.0 ...

  10. 类spring ioc 泛型保留

    类spring ioc 泛型保留 什么是泛型擦除 Java并不会传递泛型类,举个直观的栗子: @Component public class BaseProvider<T> { publi ...