校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛1007

Problem G



Problem Description
Gomoku is an abstract strategy board game. Named Gobang or Five in a Row as well, it is traditionally played with pieces of two colors (black and white) on a board (with  N×M intersections). Once a piece is placed, it is not allowed to be moved or removed from the chessboard.

The rules of the game are quite simple. The black plays first, and two players alternate in placing a stone of their own color on an empty intersection. The winner is the first one who gets an unbroken row of five stones of his/her color horizontally, vertically, or diagonally.

Now we have already drawn some stages of the game, and you're required to answer, that for each stage whether it is possible to reach, and if so, who has already won the game. You have to solve it as soon as possible, because there are no much time left in the competition now!
 

Input
The number of test cases  T(T150) will occur in the first line of input.

For each test case:

The first line contains the size of the board  N and  M(1N,M100).

Then  N lines followed, each with exactly  M characters. The white stone is denoted by '1', and the black stone is denoted by '2', while the empty intersections are denoted by '.' (a full period).
 

Output
For each test case, if it is impossible to arrive at such a stage, output “fault”. Otherwise, if the white piece wins, then output “white”; if the black piece wins, output “black”; or else output “other”.
 

Sample Input
  
  
2 10 10 .......... .......... ...1...... ...2...... .......... .......... .......... .......... .......... .......... 10 10 .......... .......... .......... ...1...... ..1....... ..1.2..... .......... .......... .......... ..........
 

Sample Output
  
  
other fault
#include<cstdio>
using namespace std;
const int N=110;
int vis[N][N];
char s[N][N];
int n,m;
int getnum(char p)
{
    int i,j,t=0;
    for(i=1;i<=n;i++)for(j=1;j<=m;j++) vis[i][j]=0;
    for(i=1;i<=n-4;i++)
    {
        for(j=1;j<=m-4;j++)
        {
            if(s[i][j]==p&&s[i+1][j+1]==p&&s[i+2][j+2]==p&&s[i+3][j+3]==p&&s[i+4][j+4]==p)
            {
                t++;
                vis[i][j]++;vis[i+1][j+1]++;vis[i+2][j+2]++;vis[i+3][j+3]++;vis[i+4][j+4]++;
            }
        }
    }
    for(i=1;i<=n-4;i++)
    {
        for(j=m;j>=5;j--)
        {
            if(s[i][j]==p&&s[i+1][j-1]==p&&s[i+2][j-2]==p&&s[i+3][j-3]==p&&s[i+4][j-4]==p)
            {
                t++;
                vis[i][j]++;vis[i+1][j-1]++;vis[i+2][j-2]++;vis[i+3][j-3]++;vis[i+4][j-4]++;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m-4;j++)
        {
            if(s[i][j]==p&&s[i][j+1]==p&&s[i][j+2]==p&&s[i][j+3]==p&&s[i][j+4]==p)
            {
                t++;
                vis[i][j]++;vis[i][j+1]++;vis[i][j+2]++;vis[i][j+3]++;vis[i][j+4]++;
            }
        }
    }
    for(i=1;i<=n-4;i++)
    {
        for(j=1;j<=m;j++)
        {
            if(s[i][j]==p&&s[i+1][j]==p&&s[i+2][j]==p&&s[i+3][j]==p&&s[i+4][j]==p)
            {
                t++;
                vis[i][j]++;vis[i+1][j]++;vis[i+2][j]++;vis[i+3][j]++;vis[i+4][j]++;
            }
        }
    }
    return t;
}
int check(char p)
{
  int t=getnum(p),i,j;
  for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
        if(vis[i][j]==t)return 1;
  return 0;
}
int main()
{
    int T,i,j,cnt1,cnt2,cnt3,cnt4;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
        {
            scanf("%s",s[i]+1);
        }
        cnt1=0;cnt2=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(s[i][j]=='1')
                    cnt1++;
                else if(s[i][j]=='2')
                    cnt2++;
            }
        }
        if(cnt1!=cnt2&&cnt1!=cnt2-1)
        {
            printf("fault\n");
            continue;
        }
        cnt3=getnum('1');cnt4=getnum('2');
        if(!cnt3&&!cnt4)
        {
            printf("other\n");
            continue;
        }
        if(cnt3&&cnt4)
        {
            printf("fault\n");
            continue;
        }
        if(cnt3&&cnt1!=cnt2)
        {
            printf("fault\n");
            continue;
        }
        if(cnt4&&cnt1!=cnt2-1)
        {
            printf("fault\n");
            continue;
        }
        if(cnt3)
        {
            if(check('1')) printf("white\n");else printf("fault\n");
        }
        else
        {
            if(check('2')) printf("black\n");else printf("fault\n");
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值