传送门
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!”
题目大意:
有两个人要玩游戏,游戏规则如下:
一个人用
X
, 另一个人用
解题思路:
暴力枚举。首先枚举一遍,观察能不能只走一步就胜出。如果能胜出,那么直接输出结果;否则就在枚举把当前为
.
的换成需要替换的元素,枚举另一个人的最优选择,如果没有最优选择随机一个为
代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int MAXN = 10;
char g[MAXN][MAXN];
int check(char ch){
if(g[0][0]==ch && g[1][1]==ch && g[2][2]==ch) return 1;
if(g[0][3]==ch && g[1][1]==ch && g[3][0]==ch) return 1;
if(g[0][0]==ch && g[0][1]==ch && g[0][2]==ch) return 1;
if(g[1][0]==ch && g[1][1]==ch && g[1][2]==ch) return 1;
if(g[2][0]==ch && g[2][1]==ch && g[2][2]==ch) return 1;
if(g[0][0]==ch && g[1][0]==ch && g[2][0]==ch) return 1;
if(g[0][1]==ch && g[1][1]==ch && g[2][1]==ch) return 1;
if(g[0][2]==ch && g[1][2]==ch && g[2][2]==ch) return 1;
return 0;
}
int main()
{
int T; scanf("%d", &T);
while(T--){
char ch;
for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>ch, g[i][j] = ch;
cin>>ch;
int ok = 0;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
if(g[i][j] == '.'){
g[i][j] = ch;
if(check(ch)){
ok = 1;
goto A;
}
g[i][j] = '.';
}
}
}
A:;
if(ok) puts("Kim win!");
else {
ok = 0;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
if(g[i][j] == '.'){
g[i][j] = ch;
int ta = -1, tb = -1;
for(int ii=0; ii<3; ii++){
for(int jj=0; jj<3; jj++){
if(g[ii][jj] == '.'){
g[ii][jj] = ch;
if(check(ch)){
ta = ii, tb = jj;
goto B;
}
g[ii][jj] = '.';
}
}
}
for(int ii=0; ii<3; ii++){
for(int jj=0; jj<3; jj++){
if(g[ii][jj] == '.'){
ta = ii, tb = jj;
goto B;
}
}
}
B:;
if(ch == 'o') g[ta][tb] = 'x';
else if(ch == 'x') g[ta][tb] = 'o';
for(int iii=0; iii<3; iii++){
for(int jjj=0; jjj<3; jjj++){
if(g[iii][jjj] == '.'){
g[iii][jjj] = ch;
if(check(ch)){
ok = 1;
goto C;
}
g[iii][jjj] = '.';
}
}
}
g[ta][tb] = '.';
g[i][j] = '.';
}
}
}
C:;
if(ok) puts("Kim win!");
else puts("Cannot win!");
}
}
return 0;
}
/**
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 每次AC
**/