L - Tic-Tac-Toe

2023-11-09 16:33
文章标签 tic tac toe

本文主要是介绍L - Tic-Tac-Toe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char ma[4][4];
char ch;
int panduan( )
{int a1,a2,a3,a4,a5,a6,a7,a8;a1=a2=a3=a4=a5=a6=a6=a8=0;if(ma[1][1]==ch){a1++,a4++;a8++;}if(ma[1][1]!=ch&&ma[1][1]!='.'){a1+=100,a4+=100,a8+=100;}if(ma[1][2]==ch){a1++,a5++;}if(ma[1][2]!=ch&&ma[1][2]!='.'){a1+=100,a5+=100;}if(ma[1][3]==ch){a1++,a6++,a7++;}if(ma[1][3]!=ch&&ma[1][3]!='.'){a1+=100,a6+=100,a7+=100;}if(ma[2][1]==ch){a2++,a4++;}if(ma[2][1]!=ch&&ma[2][1]!='.'){a2+=100,a4+=100;}if(ma[2][2]==ch){a2++,a5++,a7++,a8++;}if(ma[2][2]!=ch&&ma[2][2]!='.'){a2+=100,a5+=100,a7+=100,a8+=100;}if(ma[2][3]==ch){a2++,a6++;}if(ma[2][3]!=ch&&ma[2][3]!='.'){a2+=100,a6+=100;}if(ma[3][1]==ch){a3++,a4++,a7++;}if(ma[3][1]!=ch&&ma[3][1]!='.'){a3+=100,a4+=100,a7+=100;}if(ma[3][2]==ch){a3++,a5++;}if(ma[3][2]!=ch&&ma[3][2]!='.'){a3+=100,a5+=100;}if(ma[3][3]==ch){a3++,a6++,a8++;}if(ma[3][3]!=ch&&ma[3][3]!='.'){a3+=100,a6+=100,a8+=100;}if(a1>=1&&a1<=5){if(a4>=1&&a4<=6){if(ma[1][1]==ch&&a1==1&&a4==1)return 0;elsereturn 1;}if(a5>=1&&a5<=6){if(ma[1][2]==ch&&a1==1&&a5==1)return 0;elsereturn 1;}if(a6>=1&&a6<=6){if(ma[1][3]==ch&&a1==1&&a6==1)return 0;elsereturn 1;}if(a7>=1&&a7<=6){if(ma[1][3]==ch&&a1==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[1][1]==ch&&a1==1&&a8==1)return 0;elsereturn 1;}}if(a2>=1&&a2<=5){if(a4>=1&&a4<=6){if(ma[2][1]==ch&&a2==1&&a4==1)return 0;elsereturn 1;}if(a5>=1&&a5<=6){if(ma[2][2]==ch&&a2==1&&a5==1)return 0;elsereturn 1;}if(a6>=1&&a6<=6){if(ma[2][3]==ch&&a2==1&&a6==1)return 0;elsereturn 1;}if(a7>=1&&a7<=6){if(ma[2][2]==ch&&a2==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[2][2]==ch&&a1==1&&a8==1)return 0;elsereturn 1;}}if(a3>=1&&a3<=5){if(a4>=1&&a4<=6){if(ma[3][1]==ch&&a3==1&&a4==1)return 0;elsereturn 1;}if(a5>=1&&a5<=6){if(ma[3][2]==ch&&a3==1&&a5==1)return 0;elsereturn 1;}if(a6>=1&&a6<=6){if(ma[3][3]==ch&&a3==1&&a6==1)return 0;elsereturn 1;}if(a7>=1&&a7<=6){if(ma[3][1]==ch&&a3==1&&a7==1)return 0;elsereturn 1;;}if(a8>=1&&a8<=6){if(ma[3][3]==ch&&a3==1&&a8==1)return 0;elsereturn 1;}}if(a4>=1&&a4<=5){if(a7>=1&&a7<=6){if(ma[3][1]==ch&&a4==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[1][1]==ch&&a4==1&&a8==1)return 0;elsereturn 1;}}if(a5>=1&&a5<=5){if(a7>=1&&a7<=6){if(ma[2][2]==ch&&a5==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[2][2]==ch&&a5==1&&a8==1)return 0;elsereturn 1;}}if(a6>=1&&a6<=5){if(a7>=1&&a7<=6){if(ma[1][3]==ch&&a6==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[3][3]==ch&&a6==1&&a8==1)return 0;elsereturn 1;}}if(a7>=1&&a7<=5){if(a8>=1&&a8<=6){if(ma[2][2]==ch&&a7==1&&a8==1)return 0;elsereturn 1;}}return 0;
}
int main()
{int n,m,t,i,j,flag;scanf("%d",&t);getchar();while(t--){for(i=1;i<=3;i++){for(j=1;j<=3;j++){scanf("%c",&ma[i][j]);if(ma[i][j]==' '||ma[i][j]=='\n')scanf("%c",&ma[i][j]);}}getchar();scanf("%c",&ch);if(ma[1][1]=='.'&&ma[1][2]==ch&&ma[1][3]==ch){printf("Kim win!\n");continue;}if(ma[2][1]=='.'&&ma[2][2]==ch&&ma[2][3]==ch){printf("Kim win!\n");continue;}if(ma[3][1]=='.'&&ma[3][2]==ch&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]=='.'&&ma[2][1]==ch&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][2]=='.'&&ma[2][2]==ch&&ma[3][2]==ch){printf("Kim win!\n");continue;}if(ma[1][3]=='.'&&ma[2][3]==ch&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]=='.'&&ma[2][2]==ch&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][3]=='.'&&ma[1][2]==ch&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[1][2]=='.'&&ma[1][3]==ch){printf("Kim win!\n");continue;}if(ma[2][1]==ch&&ma[2][2]=='.'&&ma[2][3]==ch){printf("Kim win!\n");continue;}if(ma[3][1]==ch&&ma[3][2]=='.'&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][1]=='.'&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][2]==ch&&ma[2][2]=='.'&&ma[3][2]==ch){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[2][3]=='.'&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][2]=='.'&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[1][2]=='.'&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[1][2]==ch&&ma[1][3]=='.'){printf("Kim win!\n");continue;}if(ma[2][1]==ch&&ma[2][2]==ch&&ma[2][3]=='.'){printf("Kim win!\n");continue;}if(ma[3][1]==ch&&ma[3][2]==ch&&ma[3][3]=='.'){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][1]==ch&&ma[3][1]=='.'){printf("Kim win!\n");continue;}if(ma[1][2]==ch&&ma[2][2]==ch&&ma[3][2]=='.'){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[2][3]==ch&&ma[3][3]=='.'){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][2]==ch&&ma[3][3]=='.'){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[1][2]==ch&&ma[3][1]=='.'){printf("Kim win!\n");continue;}flag=0;flag = panduan();if(flag)printf("Kim win!\n");if(!flag)printf("Cannot win!\n");}return 0;
}

这篇关于L - Tic-Tac-Toe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/377281

相关文章

matlab 程序运行时间 tic toc

tic ... code toc   刚改完一个程序,把大段的 k = 0 for... k = k+1 ... end 改成了矩阵计算 240s缩短到180s,甚喜 缺点嘛....容易out of memory - -   plus 一个段子 判断代码质量的唯一标准是你在code review 的时候每分钟的脏话数。

linux查看日志文件内容命令tail、cat、tac、head、echo

-- linux 下修改文件内容保存并退出 vim /etc/profile e i shift+: 退出保存 wq ctrl+z sudo shutdown -r now chmod -777 修改权限 如果用命令chmod 777 tt.htm,结果是 -rwxrwxrwx 1 bu users 2254 2006-05-20 13:47 tt.htm

强化学习实践一:Tic-Tac-Toe游戏

这里给出一个简单的强化学习例子Tic-Tac-Toe。这是一个简单的游戏,在一个3x3的九宫格里,两个人轮流下,直到有个人的棋子满足三个一横一竖或者一斜,赢得比赛游戏结束,或者九宫格填满也没有人赢,则和棋。 这个例子的完整代码在我的github。例子只有一个文件,很简单,代码首先会用两个电脑选手训练模型,然后可以让人和机器对战。当然,由于这个模型很简单,所以只要你不乱走,最后的结果都是和棋,当然

Linux 命令使用:cat与tac、more与less、head与tail

Linux系统中有许多命令提供查看文件内容的功能,无所谓哪个命令更好,在具体工作中,根据需求选择具有不同功能特点的命令。 1. cat <file> 查看文件内容;指定输入创建文件;连接或合并文件 【1】查看一个或多个文件内容 tmpuser:test/ $ lltotal 0tmpuser:test/ $ echo 'a' > t1.filetmpuser:test/ $ echo

matlab中的tic、toc命令使用方法

tic和toc用来记录matlab命令执行的时间。 tic用来保存当前时间,而后使用toc来记录程序完成时间。 两者往往结合使用,用法如下: tic operations toc 显示时间单位:秒 例如: >> a = rand(1,10000);ticb = mean(a)tocb =0.4961时间已过 0.014130 秒。

ACdream区域赛指导赛之手速赛系列(4) E Tic-Tac-Toe-Tomek

题目: Tic-Tac-Toe-Tomek Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Tic-Tac-Toe-Tomek is a game playe

tac 查看文件内容(反向列出)

命令解释 如果说cat是由第一行到最后一行连续显示到屏幕上,tac则是由最后一行到第一行反向在屏幕上显示出来。 命令举例 [root@localhost ~]# tac /etc/issueKernel \r on an \mCentOS release 5.11 (Final)# 这个文件是由最后一行先显示的

查看文件内容的指令:cat,tac,nl,more,less,head,tail,写入文件:echo

目录 cat 介绍 输入重定向 选项 -b -n -s  tac 介绍 输入重定向  nl 介绍 示例  more 介绍 选项 less 介绍 搜索文本 选项  head 介绍 示例 选项 -n tail 介绍 示例 选项 echo 介绍 输出重定向 追加重定向 cat 介绍 将标准输入(键盘输入)的内容打

《C++游戏编程入门》第7章 指针:Tic-Tac-Toe 2.0

《C++游戏编程入门》第7章 指针:Tic-Tac-Toe 2.0 7.1 指针基础07.pointing.cpp 7.2 指针和常量7.3 传递指针07.swap_pointer_ver.cpp07.inventory_displayer_pointer_ver.cpp 7.4 返回指针07.inventory_pointer.cpp 7.5 指针与数组的关系07.array_pas

具有Reactredux express的第一印象redux tic tac toe

I had no prior experience with redux, was looking for a chance to get into it a bit more. These scripts (English, Javascript) can help you too or may be you can help me by suggesting my mistakes