您的位置:首页 > 其它

FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛

2017-07-24 19:41 423 查看
传送门

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 , 另一个人用 O, 如果一个人有一行或者一列或者对角线都是相同元素,那么这个人就胜出了,现在给定一个状态,让你求在这个状态下,你在走两步,能不能赢(俩个人都是采用的最优策略)

解题思路:

暴力枚举。首先枚举一遍,观察能不能只走一步就胜出。如果能胜出,那么直接输出结果;否则就在枚举把当前为 . 的换成需要替换的元素,枚举另一个人的最优选择,如果没有最优选择随机一个为 . 的元素,然后在进行判断就OK了,就是代码稍微麻烦了一点,不过还是很好想的。

代码:

#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
**/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐