第八届福建省大学生程序设计竞赛 L.Tic-Tac-Toe【思维+暴力枚举】

 Problem 2283 Tic-Tac-Toe

Accept: 31    Submit: 42
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

3
. . .
. . .
. . .
o
o x o
o . x
x x o
x
o x .
. o .
. . x
o

 Sample Output

Cannot win!
Kim win!
Kim win!

 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

题目大意:


给你一个3*3的井字棋,判断Kim是否能够通过两步赢得对手。

两步赢得对手的步骤是:Kim下,对方下,Kim下。


如果棋子能够三连就算Kim赢。


思路:


队长比赛过程中这个思路非常好:


①首先我们暴力枚举一个位子,使得这个位子就是Kim下的位子。

②然后check一下是否能够胜利。

③然后我们不枚举对方的走法,因为对方要足够聪明,所以我们枚举Kim下一步走哪里会赢,如果有这样一个位子,对手肯定要阻拦。

那么怎样的条件对方阻拦不了Kim的胜利呢?就是Kim下一步,如果有两个位子以上的放置方案会赢,那么对手如何阻拦都不行了。

④过程模拟一下即可。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int a[5][5];
int check(int kim)
{
    if(a[0][0]==a[0][1]&&a[0][1]==a[0][2]&&a[0][0]==kim)return 1;
    if(a[1][0]==a[1][1]&&a[1][1]==a[1][2]&&a[1][0]==kim)return 1;
    if(a[2][0]==a[2][1]&&a[2][1]==a[2][2]&&a[2][0]==kim)return 1;
    if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[0][0]==kim)return 1;
    if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]&&a[0][2]==kim)return 1;
    if(a[0][0]==a[1][0]&&a[1][0]==a[2][0]&&a[0][0]==kim)return 1;
    if(a[0][1]==a[1][1]&&a[1][1]==a[2][1]&&a[0][1]==kim)return 1;
    if(a[0][2]==a[1][2]&&a[1][2]==a[2][2]&&a[0][2]==kim)return 1;
    return 0;
}
int judge(int kim)
{
    if(check(kim)==1)return 1;
    int tot=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(a[i][j]==0)
            {
                a[i][j]=kim;
                if(check(kim)==1)
                {
                    tot++;
                }
                a[i][j]=0;
            }
        }
    }
    if(tot>=2)return 1;
    else return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                char tmp[5];
                scanf("%s",tmp);
                if(tmp[0]=='.')a[i][j]=0;
                else if(tmp[0]=='x')a[i][j]=1;
                else a[i][j]=2;
            }
        }
        char tmp[5];
        scanf("%s",tmp);
        int kim;
        if(tmp[0]=='.')kim=0;
        else if(tmp[0]=='x')kim=1;
        else kim=2;
        int flag=0;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(a[i][j]==0)
                {
                    a[i][j]=kim;
                    if(judge(kim)==1)
                    {
                        flag=1;
                    }
                    a[i][j]=0;
                }
            }
        }
        if(flag==1)printf("Kim win!\n");
        else printf("Cannot win!\n");
    }
}










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值