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能不能在两步之内 完成连线,如果可以 输出 Kim win  否则 输出 Cannot win1.
如果两人下的棋的步数小于2(又关 'o' 或者 'x' 总数小于 2),也就是只下了一步或者还没有开始,那么肯定Kim是不可能在两步之内取得胜利的。还没有开始的话两步就算对方没有去阻拦也不够三个棋子,只走了一步,接下来的两步中对方一去阻拦,也不会连成一条线。2.如果这时候步数大于等于2了,能否取得胜利的关键就是能否占领了 最中间的位置 或者在最中间的位置双方都没有下过棋。最主要的是看中间的位置有没有棋子
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{int t,i,j,sum=0;char s[50][50];char c;scanf("%d",&t);getchar();while(t--){memset(s,0,sizeof(s));for(i=0;i<=2;i++){for(j=0;j<=2;j++){scanf("%c",&s[i][j]);getchar();}}scanf("%c",&c);sum=0;for(i=0;i<=2;i++)for(j=0;j<=2;j++)if(s[i][j]==c)sum++;
// 判断 是字符c 的有几个if(sum<=1)// 0 个或者 1 个 都不能赢printf("Cannot win!");else{if(s[1][1]==c||s[1][1]=='.')//中间位置printf("Kim win!");elseprintf("Cannot win!");}printf("\n");getchar();
}/*
另一种方法  模拟法(比较困难)
*/
#include<stdio.h>
#include<string.h>
using namespace std;
int a[5][5];
int check(int kim)
{if(a[0][0]==a[0][1]&&a[0][1]==a[0][2]&&a[0][0]==kim)return 1;if(a[1][0]==a[1][1]&&a[1][1]==a[1][2]&&a[1][0]==kim)return 1;if(a[2][0]==a[2][1]&&a[2][1]==a[2][2]&&a[2][0]==kim)return 1;if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[0][0]==kim)return 1;if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]&&a[0][2]==kim)return 1;if(a[0][0]==a[1][0]&&a[1][0]==a[2][0]&&a[0][0]==kim)return 1;if(a[0][1]==a[1][1]&&a[1][1]==a[2][1]&&a[0][1]==kim)return 1;if(a[0][2]==a[1][2]&&a[1][2]==a[2][2]&&a[0][2]==kim)return 1;return 0;
}
int judge(int kim)
{if(check(kim)==1)return 1;int tot=0;for(int i=0;i<3;i++){for(int j=0;j<3;j++){if(a[i][j]==0){a[i][j]=kim;if(check(kim)==1){tot++;}a[i][j]=0;}}}if(tot>=2)return 1;else return 0;
}
int main()
{int t;scanf("%d",&t);while(t--){for(int i=0;i<3;i++){for(int j=0;j<3;j++){char tmp[5];scanf("%s",tmp);if(tmp[0]=='.')a[i][j]=0;else if(tmp[0]=='x')a[i][j]=1;else a[i][j]=2;}}char tmp[5];scanf("%s",tmp);int kim;if(tmp[0]=='.')kim=0;else if(tmp[0]=='x')kim=1;else kim=2;int flag=0;for(int i=0;i<3;i++){for(int j=0;j<3;j++){if(a[i][j]==0){a[i][j]=kim;if(judge(kim)==1){flag=1;}a[i][j]=0;}}}if(flag==1)printf("Kim win!\n");else printf("Cannot win!\n");getchar();}
}

Tic-Tac-Toe(三井旗)相关推荐

  1. python二维游戏示例_Python实现的井字棋(Tic Tac Toe)游戏示例

    本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码 ...

  2. python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行

    转不到"玩家1"的原因是你的支票中缺少一个空格.你也没有正确地检查一个玩家何时获胜,这就是为什么你会有这种奇怪的行为.你需要检查每个位置,而不仅仅是最后一个.我还添加了对用户输入的 ...

  3. react中使用构建缓存_通过在React中构建Tic Tac Toe来学习ReasonML

    react中使用构建缓存 3. 7. 2018: UPDATED to ReasonReact v0.4.2 3. 7. 2018:更新为ReasonReact v0.4.2 You may have ...

  4. 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 ...

  5. amazon.设计1. tic tac toe

    //不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/progra ...

  6. python井字棋ai,python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  7. python井字棋游戏代码_Python实现的井字棋(Tic Tac Toe)游戏示例

    Python实现的井字棋(Tic Tac Toe)游戏示例 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  Python实现的井字棋(Tic Tac Toe)游戏示 ...

  8. C++ 很有趣:编写一个井字游戏 (Tic Tac Toe)

    英文原文:C++ is fun: Writing a Tic Tac Toe Game 这个有趣的C++系列打算展示一下使用C++写代码可以和其他主流语言一样高效而有趣.在第二部分,我将向你展示使用C ...

  9. 圈叉游戏 java_【炫光圈叉棋】炫光圈叉棋 Tic Tac Toe Glow 1.8.1下载_安卓(android)软件下载-魅族溜...

    一款炫光风格的圈叉棋游戏,支持单/双人模式.圈叉棋,英文:tic-tac-toe,别名:圈叉游戏.是一种游戏,3*3的9个方格子,先下者画圈,后下者画叉,每人可以在任意没有对方棋子的封闭方格里下一次, ...

  10. java tic tac toe_请问我这个tic tac toe的游戏代码的问题在哪里

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 Scanner in = new Scanner(System.in); final int SIZE =3; int[][] board = new i ...

最新文章

  1. 机器学习 KNN算法实践
  2. php怎么删除所有文件夹,用php删除所有文件,文件夹及其子文件夹
  3. [SIGMOD 10] Pregel 基于BSP的大规模图处理系统 学习总结
  4. POJ 1723 Soldiers (中位数)
  5. CentOS7 源码编译安装NodeJS 最新版本Shell脚本
  6. VsCode配置Java环境
  7. 《概率论基础教程》总结 -- 样本空间、概率、条件概率 等
  8. 阿里云短信验证码购教程(Java演示)
  9. 古琴入门-古琴十大名曲-古琴教学——唐畅古琴
  10. STM32F4定时器介绍
  11. 风起亚洲公共云与VPS比较
  12. 自学转行3年经验,终入职阿里!
  13. IEEE-754标准
  14. Clang与GCC的区别
  15. HDMI协议介绍(六)--EDID
  16. 爬虫小小实战——豆瓣电影top250
  17. 创建多个wordpress_如何轻松创建多语言WordPress网站
  18. 【论文笔记】Are We Ready for Vision-Centric Driving Streaming Perception? The ASAP Benchmark
  19. 区块链技术能否提高网络安全?
  20. pdf转cad用什么软件比较不错

热门文章

  1. SAP BOM展开函数CS_BOM_EXPL_MAT_V2
  2. 世界上应用最广泛的算法之一的卡尔曼滤波算法原理-从放弃到精通-无人机/机器人应用
  3. github上能找到中文博主吗_Lyx的安装流程(windows10系统)及配置中文环境
  4. mysql在原有的字段添加字符串(用逗号分隔)
  5. 拓扑排序 by zyz on 2021/4/11
  6. 安卓禁用硬件加速_开/关大不同 Android4.0 GPU硬件加速实测
  7. php中文输出有乱码怎么办,php中文输出乱码怎么办
  8. 解决nexus 6p 无限重启的问题。nexus 6p 刷入twrp,magisk
  9. Vue组织架构树图组件vue-org-tree的使用
  10. [z] 人工智能和图形学、图像处理方面的各种会议的评级