hdu 2699 ( Five in a Row ) 五子棋

#include <iostream>
#include <queue>
using namespace
std;

struct
Node
{

    char
x, y, time, dirc;
    bool
no_stone;
};

char
map[15][15], c;
int
dir[4][2] = {{0,1},{1,0},{1,-1},{1,1}};
bool
bfs(int x, int y)
{

    queue<Node> Q;
    Node first, next;
    int
i;
    first.x = x;
    first.y = y;
    first.no_stone = false;
    first.time = 1;
    first.dirc = -1;
    Q.push(first);

    while
(!Q.empty())
    {

        first = Q.front();
        Q.pop();
        if
(first.time == 5 && first.no_stone)
        {

            return
true;
        }

        for
(i = 0; i < 4; ++i)
        {

            if
(first.dirc == -1 || first.dirc == i)
            {

                next.x = first.x + dir[i][0];
                next.y = first.y + dir[i][1];
                if
(next.x >= 0 && next.x < 15 && next.y >= 0 && next.y < 15)
                {

                    next.dirc = i;
                    next.time = first.time + 1;
                    if
(map[next.x][next.y] == c)
                    {

                        next.no_stone = first.no_stone;
                        Q.push(next);
                    }

                    else if
(map[next.x][next.y] == '.' && !first.no_stone)
                    {

                        next.no_stone = true;
                        Q.push(next);
                    }
                }
            }
        }   
    }

    return
false;
}

int
main()
{

    int
T;
    scanf("%d", &T);
    while
(T--)
    {

        int
i, j;
        int
white_num = 0, black_num = 0;
        getchar();
        for
(i = 0; i < 15; ++i)
        {

            gets(map[i]);
        }

        for
(i = 0; i < 15; ++i)
        {

            for
(j = 0; j < 15; ++j)
            {

                if
(map[i][j] == 'W')
                {

                    white_num++;
                }

                else if
(map[i][j] == 'B')
                {

                    black_num++;
                }
            }
        }

        if
(black_num > white_num)
        {

            c = 'W';
        }

        else

        {

            c = 'B';
        }

        bool
flag = true;
        for
(i = 0; i < 15 && flag; ++i)
        {

            for
(j = 0; j < 15 && flag; ++j)
            {

                if
(map[i][j] == c)
                {

                    flag = !bfs(i, j);
                }
            }
        }

        if
(flag)
        {

            printf("NO\n");
        }

        else

        {

            printf("YES\n");
        }
    }

    return
0;
}

posted on 2009-08-01 21:30  ZAFU_VA  阅读(243)  评论(0编辑  收藏  举报

导航