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.
For each test case:
If Kim can win in 2 steps, output “Kim win!”
Otherwise output “Cannot win!”
3 . . . . . . . . . o o x o o . x x x o x o x . . o . . . x oSample Output
Cannot win! Kim win! Kim win!
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[4][4], vis[4][4];//记录‘标记数组
int f(char k){//查找函数 枚举所有情况(每条直线上有两个相同的并有一个空的)并记录个数
int i, sum = 0;
for(i = 1; i <= 3; i++){//三行
if((a[i][1] == k && a[i][2] == k && a[i][3] == '.'))sum++;
else if((a[i][1] == k && a[i][2] == '.' && a[i][3] == k))sum++;
else if((a[i][1] == '.' && a[i][2] == k && a[i][3] == k))sum++;
}
for(i = 1; i <= 3; i++){//三列
if((a[1][i] == k && a[2][i] == k && a[3][i] == '.'))sum++;
else if((a[1][i] == k && a[2][i] == '.' && a[3][i] == k))sum++;
else if((a[1][i] == '.' && a[2][i] == k && a[3][i] == k))sum++;
}//两斜线
if(a[1][1] == k && a[2][2] == k && a[3][3] == '.')sum++;
if(a[1][1] == '.' && a[2][2] == k && a[3][3] == k)sum++;
if(a[1][1] == k && a[2][2] == '.' && a[3][3] == k)sum++;
if(a[1][3] == k && a[2][2] == k && a[3][1] == '.')sum++;
if(a[1][3] == '.' && a[2][2] == k && a[3][1] == k)sum++;
if(a[1][3] == k && a[2][2] == '.' && a[3][1] == k)sum++;
return sum;
}
int main(){
char ch;
int t;
scanf("%d", &t);
getchar();//清除t
while(t--){
int i, j;
for(i = 1; i <= 3; i++)
for(j = 1; j <= 3; j++){//输入并标记
scanf("%c", &ch);
a[i][j] = ch;
if(ch != '.')vis[i][j] = 0;
else if(ch == 'o')vis[i][j] = 1;
else if(ch == 'x')vis[i][j] = 2;
getchar();//清除空格回车
}
char sh;
int flag = 0;
scanf("%c", &sh);
int sum1 = f(sh);
//printf("%d\n", sum1);
if(sum1 >= 1)flag = 1;//如果能一步达成
else if(sum1 == 0){//如果不能一步达成
int flag1 = 0;
for(i = 1; i <= 3; i++)
for(j = 1; j <= 3; j++){
if(a[i][j] == sh)flag1++;//记录已有需下棋同类型的个数
}
if(flag1 >= 2)//已有两个及以上
for(i = 1; i <= 3; i++){
for(j = 1; j <= 3; j++){
if(vis[i][j] == 0){//如过能两步达成并且另一个人不能达成
a[i][j] = sh;
//int sum2;
if(sh == 'o' && f('o') >= 2 && f('x') == 0){flag = 1;break;}
else if(sh == 'x' && f('x') >= 2 && f('o') == 0){flag = 1;break;}
}
}
}
}
if(flag == 1)printf("Kim win!\n");
else printf("Cannot win!\n");
getchar();
}
return 0;
}