Tic-Tac-Toe (第八届福建省赛)

Problem 2283 Tic-Tac-Toe

Accept: 2    Submit: 3
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. . .. . .. . .oo x oo . xx x oxo x .. o .. . xo

Sample Output

Cannot win!Kim win!Kim win!

Source

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


一开始觉得太麻烦了,所以没去写这道题,结果比赛完后,回顾了一下,发现就是一道水题,暴力搜索就好了,只有3*3的格子;

发现还是缺乏经验,水题都发现不了。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
char map[4][4];
int sum;
int flag;
int ans;
char a,b;
int isok(char c) {
    for(int i=0; i<3; i++) {
        if(map[0][i]==c&&map[1][i]==c&&map[2][i]==c) return 1;       //行列
        if(map[i][0]==c&&map[i][1]==c&&map[i][2]==c) return 1;
    }
    if(map[0][0]==c&&map[1][1]==c&&map[2][2]==c) return 1;
    if(map[2][0]==c&&map[1][1]==c&&map[0][2]==c) return 1;           //对角线
    return 0;
}

void dfs() {
    ans=0;
    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++) {
            if(map[i][j]!='.') continue;
            map[i][j]=a;
            if(isok(a)) ans++;
            map[i][j]='.';
            if(ans>=2) return;          //有超过两个地方可以让A赢,则A必赢
        }
    }
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        flag=0;
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                scanf("%s",&map[i][j]);
            }
            getchar();
        }
        scanf("%c",&a);
        if(a=='o') b='x';
        else b='o';
        //情况一;一开始便已经可以赢了;
        if(isok(a)) {
            printf("Kim win!\n");
            continue;
        }
        //情况二:只差一位就可以赢
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                if(map[i][j]!='.') continue;
                map[i][j]=a;
                if(isok(a)) {
                    flag=1;
                    break;
                }
        map[i][j]='.';
            }
            if(flag)
                break;
        }
        if(flag) {
            printf("Kim win!\n");
            continue;
        }
        // 情况三;b只差一步就可以赢,所以要封锁这个位置,然后判断之后是否有超过两个位置可以赢,有则可以赢;
        ans=0;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++) {
                if(map[i][j]!='.') continue;
                map[i][j]=b;
                if(isok(b)) {
                    flag=1;
                    map[i][j]=a;
                    dfs();
                    if(ans>=2) flag=2;
                    map[i][j]='.';

                }
                map[i][j]='.';

            }
        if(flag) {
            if(flag==2) {
                printf("Kim win!\n");
            } else
                printf("Cannot win!\n");
            continue;

        }
          //不是以上特殊情况,则直接搜索,看有没有能练成线的;
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                ans=0;
                if(map[i][j]!='.')
                    continue;
                else {
                    map[i][j]=a;
                    dfs();
                    map[i][j]='.';
                }
                if(ans>=2) break;
            }
            if(ans>=2) break;
        }
        if(ans>=2) printf("Kim win!\n");
        else printf("Cannot win!\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值