HDU3368_翻转棋

题目大意: 给你一个棋盘,然后一开始已经有黑白棋了,现在轮到黑棋下,如果你下得这个黑棋的八个方向,有方向存在连续的白棋,且也以一颗黑棋结束,那么两个黑棋中夹着的白棋就变成了黑棋。要求求最大能翻转掉多少白棋。 解题思路: 一开始没有看清楚题意,原来可以翻转掉多个方向的白棋。Dark must place a piece with the dark side up on the board, in such a position that there exists at least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another dark piece, with one or more contiguous light pieces between them. In the below situation, dark has the following options indicated by transparent pieces: 题意说明了at least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another dark piece 然后就直接搜咯,我是枚举每一个'D'点开始搜的。 代码:
#include
#include
#include
using namespace std;

const int MAX = 9;
char Map[MAX][MAX];
int dir[8][2] = {{ -1, 0}, { -1, 1}, {0, 1}, {1, 1},
    {1, 0}, {1, -1}, {0, -1}, { -1, -1}
};
int result;
bool visited[MAX][MAX];
int flagDir;
int remain;

void DFS1(int x, int y, int step, int d)
{
    x += dir[d][0];
    y += dir[d][1];
    if(x >= 0 && x < 8 && y >= 0 && y < 8)
    {
        if(Map[x][y] == 'L')
        {
            step++;
            DFS1(x, y, step, d);
        }
        else if(Map[x][y] == 'D')
        {
            remain += step;
        }
        else
            return ;
    }
}

void DFS(int x, int y, int step, int d)
{
    remain = 0;
    x += dir[d][0];
    y += dir[d][1];
    if(x >= 0 && x < 8 && y >= 0 && y < 8)
    {
        if(Map[x][y] == 'L')
        {
            visited[x][y] = true;
            step++;
            DFS(x, y, step, d);
        }
        else if(Map[x][y] == '*')
        {
            visited[x][y] = true;
            for(int i = 0; i < 8; i++)
            {
                if(visited[x + dir[i][0]][y + dir[i][1]] == false)
                    DFS1(x, y, 0, i);
            }
            step += remain;
            if(step > result)
                result = step;
        }
        else
            return ;
    }
}

void init()
{
    memset(visited, false, sizeof(visited));
}

int main(void)
{
    int n, cas_c = 1;
    scanf("%d", &n);
    while(n--)
    {
        result = 0;

        for(int i = 0; i < 8; i++)
            scanf("%s", Map[i]);
        for(int i = 0; i < 8; i++)
            for(int j = 0; j < 8; j++)
            {
                if(Map[i][j] == 'D')
                {
                    for(int h = 0; h < 8; h++)//方向
                    {
                        init();
                        DFS(i, j, 0, h);
                    }
                }
            }
        printf("Case %d: %d\n", cas_c++, result);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cchun/p/2520227.html