(fzu) Problem L Tic-Tac-Toe(水)

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!

题意:3*3的棋盘两个人玩画圈打叉,只要有一方的3个棋子连成水平、垂直、对角线,给你一个棋盘,上面已经有一些棋子,问Kim能否在两步之内赢?(两步是指Kim走一步,对手一步,Kim再走一步)
没做这道题之前感觉不难,因为很多人都AC了,然后看了下题,可能最近搜索题写多了,一看就把它当搜索写了
然后这题数据应该是比较水的,WA了之后就改了一点,总感觉不能AC,但是AC了
输入的地方卡住了,噗。。 现在好像知道用scanf(” %c”,&ch);留一个空格就不读那个空格了,是这样…
代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N=100000;
char a[3][3],ch;
int flag=0;
int dxy[4][2]= {1,0,0,-1,-1,0,0,1};
int in(int x,int y)
{
    if(x>=0&&x<3&&y>=0&&y<3)
        return 1;
    return 0;
}
void dfs(int x,int y)
{
    int cnt=0,cnt3=0;
    for(int i=0; i<4; i++)
    {
        int nx=x+dxy[i][0],ny=y+dxy[i][1];
        if(in(nx,ny)&&(a[nx][ny]==ch||a[nx][ny]=='.'))
        {
            if(a[nx][ny]==ch)
                cnt++;
            else cnt3++;
        }
        if((cnt==2&&cnt3)||(cnt>2))
        {
            flag=1;
            //return ;
        }
    }
    cnt=cnt3=0;
    for(int i=-2; i<3; i++)
    {
        int nx=x+i,ny=y;
        if(in(nx,ny)&&a[nx][ny]==ch)
            cnt++;
        if(cnt==2)
            {flag=1;return ;}
    }
    cnt=cnt3=0;
    for(int i=-2; i<3; i++)
    {
        int nx=x,ny=y+i;
        if(in(nx,ny)&&a[nx][ny]==ch)
            cnt++;
        if(cnt==2)
            {flag=1;return ;}
    }
    cnt=cnt3=0;
    for(int i=-1;i<=1;i++)
    {
        int nx=x+i,ny=y+i;
        if(in(nx,ny)&&a[nx][ny]==ch)
            cnt++;
        if(cnt==2)
            {flag=1;return ;}
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        flag=0;
        char cch,str[10][5];
        int cnt1=0,cnt2=0,k=0;
        for(int i=0; i<3; i++)
        {
            for(int l=0; l<3; l++)
            {
                cin>>str[l];
                a[i][l]=str[l][0];
            }
            //a[i][1]=str[2];
            //a[i][2]=str[4];
            //printf("%c,%c,%c\n",a[i][0],a[i][1],a[i][2]);
            for(int j=0; j<3; j++)
            {
                //scanf("%c",&cch);
                //if(a[i][j]!=' ') a[i][k]=cch;
                if(a[i][j]=='o')
                    cnt1++;
                else if(a[i][j]=='x')
                    cnt2++;
                //printf("%c",a[i][j]);
            }
        }
        cin>>ch;
        //printf("%c",ch);
        if(ch=='o'&&cnt1<2||ch=='x'&&cnt2<2)
            puts("Cannot win!");
        else
        {
            for(int i=0; i<3; i++)
                for(int j=0; j<3; j++)
                    if(a[i][j]=='.')
                    {
                        dfs(i,j);
                        if(flag)
                            break;
                    }
            printf("%s\n",flag?"Kim win!":"Cannot win!");
        }
    }
    return 0;
}

不对的望能指点,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值