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

来源:互联网 发布:4gip网络加速器.apk 编辑:程序博客网 时间:2024/05/22 06:28

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
210 10.......................1.........2..................................................................10 10.................................1........1.........1.2.............................................
 

Sample Output
otherfault
#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
原创粉丝点击