Five-In-a-Row CodeForces - 825B
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。