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. 20190803 NOIP模拟测试12「斐波那契(fibonacci)· 数颜色 · 分组 」

    164分 rank11/64 这次考的不算太差,但是并没有多大的可能性反超(只比一小部分人高十几分而已),时间分配还是不均,T2两个半小时,T1半个小时,T3-额十几分钟吧 然额付出总是与回报成反比的 ...

  2. Vue2.0仿饿了么webapp单页面应用

    Vue2.0仿饿了么webapp单页面应用 声明: 代码源于 黄轶老师在慕课网上的教学视频,我自己用vue2.0重写了该项目,喜欢的同学可以去支持老师的课程:http://coding.imooc.c ...

  3. 用mongodb 固定集合实现只保留固定数量的记录,自动淘汰老旧数据

    在一个保存report记录的场景中,我们使用MongoDB进行数据存储 example: db: report Collection: daily_report 创建db:  use report; ...

  4. 不等"金九银十",金风八月,我早已拿下字节跳动的offer

    字节跳动,我是在网上投的简历,之前也投过一次,简历都没通过删选,后来让师姐帮我改了一下简历,重新投另一个部门,获得了面试机会.7月23日,中午HR打电话过来预约了下午4点半面试,说会在线写代码,让我准 ...

  5. 信息收集框架——recon-ng

    背景:在渗透测试前期做攻击面发现(信息收集)时候往往需要用到很多工具,最后再将搜集到的信息汇总到一块.          现在有这样一个现成的框架,里面集成了许多信息收集模块.信息存储数据库.以及报告 ...

  6. java后端开发面经 数据库相关

    小姐姐:怎么理解感情中的付出和回报? 你答:有这样一个故事,讲的是一个小男孩和一个小女孩,这个小男孩呢,用很多好玩石头,而这个小女孩呢,有好多好吃的糖果,有一天,他们相互约定:小男孩用所有的石头交互小 ...

  7. Windows Server 2008利用NTFS管理数据

    今天我们学习关于NTFS管理数据 以下是学习的内容NTFS分区和FAT32分区的区别,如何将FAT32分区转化成NTFS分区,FAT 32 不支持大于4G ,NTFS权限设置 ,EFS加密 ,文件夹的 ...

  8. 消息中间件——RabbitMQ(三)理解RabbitMQ核心概念和AMQP协议!

    前言 本章学习,我们可以了解到以下知识点: 互联网大厂为什么选择RabbitMQ? RabbiMQ的高性能之道是如何做到的? 什么是AMQP高级协议? AMQP核心概念是什么? RabbitMQ整体架 ...

  9. Zabbix数据库空间大小使用计算

    一.Zabbix的数据存储主要分类 1.历史数据 2.趋势数据 3.事件数据 二.每秒处理的数据量 顾名思义,例如,有3000个监控项(item),每60秒取一次值,即平均每秒有50(3000/60) ...

  10. Ng-Matero 0.1 发布了!

    Ng-Matero 0.1 终于发布了!周末折腾了两天,结果最后发版还是出了点意外,好在今天补了一版. 距离 Ng-Matero 发布第一版已经过去了一个多月,然后很颓废地休息了半个多月,最近项目的关 ...