B.Five-In-a-Row【逻辑+模拟】

B. Five-In-a-Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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'.

Examples
input
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........
output
YES
input
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
output
NO

题意:现在给你一个10*10的棋盘,让你放一个X棋,使X连成同行或同列或对角线成5子,O处不能放;

思路:好不容易读懂题过了,结果数据这么水,被Hack了,然后wa一路,cf数据水深;

这道题难倒是不难,主要考虑的情况比较多;找到X点,向右找同行的,向下找同列的,向主对角线,副对角线找。首先找X,找不到4个X,就NO;找出4个X,如果被O中断,NO;如果没被中断,同行找左侧是不是‘ 。’ ,其他方向同理;

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define max_n 12
typedef long long LL;
using namespace std;
char a[max_n][max_n];

int main()
{
	memset(a,0,sizeof(a));
	for(int i=0;i<10;i++)
		scanf("%s",a[i]);
	bool flag1=true,flag2=true,flag3=true,flag4=true,flag=false;
	int ans1=0,ans2=0,ans3=0,ans4=0;
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			flag1=true,flag2=true,flag3=true,flag4=true;
			ans1=0,ans2=0,ans3=0,ans4=0;
			int res1=0,res2=0,res3=0,res4=0;
			if(a[i][j]=='X')
			{
				for(int p=1;p<=4;p++)
				{
					if(a[i+p][j]=='X')
						ans1++;
					else if(a[i+p][j]=='O')
					{
						flag1=false;
						break;
					}
					else if(a[i+p][j]=='.') res1++;
				}
				if(a[i-1][j]=='.' && i && res1==0)
				{
					if(ans1>=3) flag1=true;
					res1++;
				} 
				for(int p=1;p<=4;p++)
				{
					if(a[i][j+p]=='X')
						ans2++;
					else if(a[i][j+p]=='O')
					{
						flag2=false;
						break;
					}
					else if(a[i][j+p]=='.') res2++;
				}
				if(a[i][j-1]=='.' && j && res2==0)
				{
					if(ans2>=3) flag2=true;
					res2++;
				} 
				for(int p=1;p<=4;p++)
				{
					if(a[i+p][j+p]=='X')
						ans3++;
					else if(a[i+p][j+p]=='O')
					{
						flag3=false;
						break;
					}
					else if(a[i+p][j+p]=='.') res3++;
				}
				if(a[i-1][j-1]=='.' && i && j && res3==0)
				{
					if(ans3>=3) flag3=true;
					res3++;
				} 
				for(int p=1;p<=4;p++)
				{
					if(j-p<0) break;
					if(a[i+p][j-p]=='X')
						ans4++;
					else if(a[i+p][j-p]=='O')
					{
						flag4=false;
						break;
					}
					else if(a[i+p][j-p]=='.') res4++;
				}
				if(a[i-1][j+1]=='.' && i && res4==0)
				{
					if(ans4>=3) flag4=true;
					res4++;
				 }
				if(flag1 && ans1>=3 && res1==1 || flag2 && ans2>=3 && res2==1 || flag3 && ans3>=3 && res3==1 || flag4 && ans4>=3 && res4==1)
					flag=true;
			}
			if(flag) break;
		}
		if(flag) break;
	}
	if(flag) printf("YES\n");
	else printf("NO\n");
	return 0;
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值