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 win
1.
如果两人下的棋的步数小于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!");
else
printf("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();
}
}