zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    appfabric cache配置,实验记录
    appfabric cache存储ef 查询结果的bug
    CruiseControl.NET svn获取 自动编译 ftp上传
    @Ajax.RenderAction 把局部页改为ajax加载
    分布式架构下的mvc 异步controller ajax comet 长连接
    win7重装iis,搞死
    验证码识别的基本思路及方法
    C# 获取程序当前文件夹
    在c#中 限制文本框只能输入数字
    string字符串 获取指定位置范围的子字符串
  • 原文地址:https://www.cnblogs.com/cchun/p/2520227.html
Copyright © 2011-2022 走看看