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
3
. . .
. . .
. . .
o
o x o
o . x
x x o
x
o x .
. o .
. . x
o
Sample Output
Cannot win!
Kim win!
Kim win!
题目:三子连的规则,然后给出残局,
1.kim可以先下一步,判断是否赢,
2.kim下一步,然后对手下一步,kim再下一步 判断是否赢。
刚开始的时候想用DFS因为最近搜索做的多了,然后样例过,WA了3发,发现我只是想当然的算法,然后想到可以分为2种情况
1.下一步直接获胜
2.”套路“
* * *
* * *
* * *
kim抢了四个角中的一个或者以上+中间的点,然后其余3个角的点再有2个即可套路对方用2步获胜
否则无法判断
#include <iostream>
#include <cstdio>
using namespace std;
char s[10],juzi[10],ch,ck,m1;
int judge1()
{
if(s[0]==ch&&s[1]==ch&&s[2]==ch) return 1;
if(s[3]==ch&&s[4]==ch&&s[5]==ch) return 1;
if(s[6]==ch&&s[7]==ch&&s[8]==ch) return 1;
if(s[0]==ch&&s[3]==ch&&s[6]==ch) return 1;
if(s[1]==ch&&s[4]==ch&&s[7]==ch) return 1;
if(s[2]==ch&&s[5]==ch&&s[8]==ch) return 1;
if(s[0]==ch&&s[4]==ch&&s[8]==ch) return 1;
if(s[2]==ch&&s[4]==ch&&s[6]==ch) return 1;
return 0;
}
int judge2()
{ /*0 1 2*/
/*3 4 5*/
/*6 7 8*/
if(s[0]==ch&&s[4]==ch)
{
int num=0;
if(s[2]!=ck)
num++;
if(s[6]!=ck)
num++;
if(s[8]!=ck)
num++;
if(num>1)
return 1;
} if(s[6]==ch&&s[4]==ch)
{
int num=0;
if(s[0]!=ck)
num++;
if(s[2]!=ck)
num++;
if(s[8]!=ck)
num++;
if(num>1)
return 1;
}
if(s[2]==ch&&s[4]==ch)
{
int num=0;
if(s[0]!=ck)
num++;
if(s[6]!=ck)
num++;
if(s[8]!=ck)
num++;
if(num>1)
return 1;
}
if(s[4]==ch&&s[8]==ch)
{
int num=0;
if(s[0]!=ck)
num++;
if(s[6]!=ck)
num++;
if(s[2]!=ck)
num++;
if(num>1)
return 1;
}
return 0;
}
int main()
{
int icase;
scanf("%d",&icase);
while(icase--)
{
getchar();
int pos[10],cnt=1,ans=0;
for(int i=1;i<=9;i++)
{
char a;
scanf("%c",&a);
getchar();
s[ans++]=a;
}
//¶ÁÈë×Ö·û
// printf("%s\n",s);
scanf("%c",&ch);
if(ch=='x') ck='o';
else ck='x';
for(int i=0;i<9;i++){
if(s[i]=='.')
{
pos[cnt++]=i;
// printf("%d\n",cnt);
}
} //ÕÒ³ö¿ÕÏÐλÖÃ
int flag=0;
for(int i=1;i<cnt;i++)
{
m1=s[pos[i]];
s[pos[i]]=ch;
if(judge1())
{
flag=1;
break;
}
s[pos[i]]=m1; //¸´Ô
}
if(flag)
printf("Kim win!\n");
else
{ if(judge2())
printf("Kim win!\n");
else
printf("Cannot win!\n");
}
}
}
这个题目属于数学逻辑推理部分的,是我的弱项,刚看上很是蒙蔽,而且之前有一道三子连的原题在湖科大OJ上没有看过,所以很是紧张,幸好最后AC了。
仍可做的题目:Problem K Wand Problem G YYS