CodeForces 825B:Five-In-a-Row(模拟)

Five-In-a-Row

Time limit:1000 ms Memory limit:262144 kB


Problem Description

Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.

In current match they have made some turns and now it’s Alice’s turn. She wonders if she can put cross in such empty cell that she wins immediately.

Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.

Input

You are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters ‘X’ being a cross, letters ‘O’ being a nought and ‘.’ being an empty cell. The number of ‘X’ cells is equal to the number of ‘O’ cells and there is at least one of each type. There is at least one empty cell.

It is guaranteed that in the current arrangement nobody has still won.

Output

Print ‘YES’ if it’s possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print ‘NO’.

Example

这里写图片描述

这里写图片描述


题意:

下五子棋,X代表Alice 的棋子,给出棋盘目前的情况.代表空白,O代表对面的棋子,现在轮到Alice下了,问她能否在这回合取胜

解题思路:

对每个.和X都来遍历,一共4个方向:左,左上,上,右上,只要有任意方向出现四个X+一个.就说明可以


Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

char mp[15][15];

bool check(int i,int j)
{
    if(i<0||i>=10||j<0||j>=10)
        return false;
    return true;
}

bool judge1(int i,int j)//左
{
    int x=0,d=0;
    for(int p=0;p<5;p++)
    {
        if(!check(i,j-p))
            return false;
        if(mp[i][j-p]=='X')
            x++;
        else if(mp[i][j-p]=='.')
            d++;
    }
    if(x==4&&d==1)
        return true;
    return false;
}

bool judge2(int i,int j)//左上
{
    int x=0,d=0;
    for(int p=0;p<5;p++)
    {
        if(!check(i-p,j-p))
            return false;
        if(mp[i-p][j-p]=='X')
            x++;
        else if(mp[i-p][j-p]=='.')
            d++;
    }
    if(x==4&&d==1)
        return true;
    return false;
}

bool judge3(int i,int j)//上
{
    int x=0,d=0;
    for(int p=0;p<5;p++)
    {
        if(!check(i-p,j))
            return false;
        if(mp[i-p][j]=='X')
            x++;
        else if(mp[i-p][j]=='.')
            d++;
    }
    if(x==4&&d==1)
        return true;
    return false;
}

bool judge4(int i,int j)//右上
{
    int x=0,d=0;
    for(int p=0;p<5;p++)
    {
        if(!check(i-p,j+p))
            return false;
        if(mp[i-p][j+p]=='X')
            x++;
        else if(mp[i-p][j+p]=='.')
            d++;
    }
    if(x==4&&d==1)
        return true;
    return false;
}

int main()
{
    for(int i=0;i<10;i++)
        scanf("%s",mp[i]);
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            if(mp[i][j]=='X'||mp[i][j]=='.')
            {
                if(judge1(i,j)||judge2(i,j)||judge3(i,j)||judge4(i,j))
                {
                    printf("YES\n");
                    return 0;
                }
            }
        }
    }
    printf("NO\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值