Five-In-a-Row CodeForces - 825B

Five-In-a-Row CodeForces - 825B

 

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
Output
Example
Output
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char mp[12][12];
int flag=0,sum=0;
void dfs(int x,int y)
{
    int i,j,t;
    for(i=x,j=y-1; j>=0; j--) //向左
    {
        if(mp[i][j]!='X')break;
        else sum++;
    }
    for(i=x,j=y+1; j<10; j++) //向右
    {
        if(mp[i][j]!='X')break;
        else sum++;
    }
    if(sum>=4)
    {
        flag=1;
        return ;
    }
    else sum=0;
    //
    for(i=x-1,j=y; i>=0; i--) //向上
    {
        if(mp[i][j]!='X')break;
        else sum++;
    }
    for(i=x+1,j=y; i<10; i++) //向下
    {
        if(mp[i][j]!='X')break;
        else sum++;
    }
    if(sum>=4)
    {
        flag=1;
        return ;
    }
    else sum=0;
    //
    for(i=x,j=y,t=1;t<=4; t++) //向右上
    {
        if(i-t<0||j+t>=10||mp[i-t][j+t]!='X')
            break;
        else sum++;
    }
    for(i=x,j=y,t=1;t<=4; t++) //向左下
    {
        if(i+t<0||j-t>=10||mp[i+t][j-t]!='X')
            break;
        else sum++;
    }
    if(sum>=4)
    {
        flag=1;
        return ;
    }
    else sum=0;
    ///
    for(i=x,j=y,t=1;t<=4; t++) //向右上
    {
        if(i-t<0||j-t>=10||mp[i-t][j-t]!='X')
            break;
        else sum++;
    }
    for(i=x,j=y,t=1;t<=4; t++) //向右上
    {
        if(i+t<0||j+t>=10||mp[i+t][j+t]!='X')
            break;
        else sum++;
    }
    if(sum>=4)
    {
        flag=1;
        return ;
    }
    else sum=0;
}
int main()
{
    while(gets(mp[0]))
    {
        for(int i=1; i<10; i++)
            scanf("%s",mp[i]);
        flag=0;
        sum=0;
        for(int i=0; i<10; i++)
        {
            for(int j=0; j<10; j++)
            {
                if(mp[i][j]=='.')
                {
                    dfs(i,j);
                    if(flag)
                        break;
                }
            }
            if(flag)
                break;
        }
        if(flag)
            printf("YES\n");
        else printf("NO\n");
        getchar();
    }
}

NO
 
      
 
      
题意:10*10的棋盘内,如果加上一个‘X’后连续在水平,垂直,对角方向上出现5个以上的‘X’时输出YES,否则的话输出NO
思想及方法:向上向下,向左向右,向左上向右下,向左下向右上,分别找四个以上的话break;输出YES,找不到的话输出NO。
 
      
 
      
 
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值