
Accept: 18 Submit: 23
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
Sample Output
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)
3*3的棋盘 三个相连就赢(横竖斜)Kim先下 对手下 Kim再下 看Kim能赢吗
有个坑的地方Kim下一次赢了就赢了
不用想3*3的棋盘暴力就OK了
看Kim下完一次 对手如果堵不是就赢了
#include<stdio.h>
#include<iostream>
using namespace std;
char ap[5][5];
char xf,dt;
int pd()
{
if(ap[1][1]==xf&&ap[1][2]==xf&&ap[1][3]==xf)
return 1;
if(ap[1][1]==xf&&ap[2][1]==xf&&ap[3][1]==xf)
return 1;
if(ap[1][1]==xf&&ap[2][2]==xf&&ap[3][3]==xf)
return 1;
if(ap[1][2]==xf&&ap[2][2]==xf&&ap[3][2]==xf)
return 1;
if(ap[1][3]==xf&&ap[2][2]==xf&&ap[3][1]==xf)
return 1;
if(ap[1][3]==xf&&ap[2][3]==xf&&ap[3][3]==xf)
return 1;
if(ap[2][1]==xf&&ap[2][2]==xf&&ap[2][3]==xf)
return 1;
if(ap[3][1]==xf&&ap[3][2]==xf&&ap[3][3]==xf)
return 1;
return 0;
}
char str[8];
int main()
{
int i,j,h,k,u,v,t;
scanf("%d",&t);
while(t--)
{
for(i=1; i<=3; i++)
for(j=1; j<=3; j++)
cin>>ap[i][j];
int hj;
scanf("%s",str);
xf=str[0];
if(str[0]=='x') dt='o';
else dt='x';
int flag=0;
for(i=1; i<=3; i++)
{
for(j=1; j<=3; j++)
{
if(ap[i][j]!='.') continue;
ap[i][j]=xf;
if(pd())
{
flag=1;
break;
}
hj=1;
for(k=1; k<=3; k++)
{
for(h=1; h<=3; h++)
{
int flagg=0;
if(ap[k][h]!='.') continue;
ap[k][h]=dt;
for(u=1; u<=3; u++)
for(v=1; v<=3; v++)
{
if(ap[u][v]!='.') continue;
ap[u][v]=xf;
if(pd())
{
flagg=1;
}
ap[u][v]='.';
}
ap[k][h]='.';
if(!flagg) hj=0;
}
}
if(hj)
{
printf("Kim win!\n");
break;
}
ap[i][j]='.';
}
if(hj)
{
break;
}
}
if(flag)
{
printf("Kim win!\n");
continue;
}
if(!hj) printf("Cannot win!\n");
}
return 0;
}