HDU 3368

Reversi, also called Othello, is a two-sided game.
Each of the two sides corresponds to one player; they are referred to here as light and dark after the sides of Othello pieces, but "heads" and "tails" would identify them equally as well, so long as each marker has sufficiently distinctive sides.
Originally, Reversi did not have a defined starting position. Later it adopted Othello's rules, which state that the game begins with four markers placed in a square in the middle of the grid, two facing light-up, two pieces with the dark side up. The dark player makes the first move.


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:


After placing the piece, dark turns over (flips, captures) all light pieces lying on a straight line between the new piece and any anchoring dark pieces. All reversed pieces now show the dark side, and dark can use them in later moves—unless light has reversed them back in the meantime. In other words, a valid move is one where at least one piece is reversed.
If dark decided to put a piece in the topmost location (all choices are strategically equivalent at this time), one piece gets turned over, so that the board appears thus:


Now light plays. This player operates under the same rules, with the roles reversed: light lays down a light piece, causing a dark piece to flip. Possibilities at this time appear thus (indicated by transparent pieces):


Light takes the bottom left option and reverses one piece:


Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. When neither player can move, the game ends. This occurs when the grid has filled up, or when one player has no more pieces on the board, or when neither player can legally place a piece in any of the remaining squares. The player with the most pieces on the board at the end of the game wins.
Now after several rounds, it’s dark’s turn. Can you figure out the largest number of light pieces he can turn over?
 

Input

The first line contains one integer T representing the number of test cases.
For each test case, there’re 8 lines. Each line contains 8 characters (D represents dark, L represents light, * represents nothing here).
Every two adjacent cases are separated by a blank line.
 

Output

For each test case, in one line print the case number and the largest number of light pieces the dark player can turn over. If he can’t put one piece in any position, then print 0.
Please follow the format of the sample output.
 

Sample Input

3 ******** ******** ******** ***LD*** ***DL*** ******** ******** ******** ******** ******** **DLL*** **DLLL** **DLD*** ******** ******** ******** ******** ******** *D****** *DLLD*** ***LL*** **D*D*** ******** ********
 
 
 
大意:D为黑棋,L为白棋,“*”为空格,如果白棋的一端是黑棋另一端是空格则在空格处放黑棋,就能吃掉两个黑棋之间的白棋;问黑棋放哪能吃最多的白棋
注意也可以是斜放的;
*LLLLLLD
L                       DLLLLLLD
L    这种情况能吃10个;   DLLLLLLD
L                       DLLLLLLD
L     (1)              DLL*LLLD  这种情况吃两个;
L
L
L
D
D
D
虽然这个题不难,dfs 一下就能过但是本人比较水,dfs还不怎么会,导致赛场上没做出来
一开始我以D为起点开始搜,但是有bug 第一种情况照顾不到,而且要向一个方向寻找,不要乱方向;
问的大神,从‘*’开始搜,就可以
#include <bits/stdc++.h>
using namespace std;
const int maxn=10;
char s[maxn][maxn];
const int n=8;
int  dfs(int i,int j, int r, int c)
{
    if(i < 0 || j < 0 || j >= n || i >= n)  return -100000;//如果return 0,会计入L总个数,会wa
    if(s[i][j] == '*') return -1000;
    if(s[i][j] == 'D')  return 0;
    if(s[i][j] == 'L')  return 1 + dfs(i+r, j + c, r, c);
}
int main()
{
    int kase;
    scanf("%d",&kase);
    getchar();
    for(int k=1; k<=kase; k++)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%s",s[i]);
        }
        int mmax=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(s[i][j]=='*')
                {

                    int cnt  = 0;
                    for(int id=-1; id<=1; id++)
                    {
                        for(int jd=-1; jd<=1; jd++)
                        {
                            if(id||jd)
                                cnt += max(0, dfs(i+id, j+jd, id, jd));//情况1;
                        }
                    }
                    mmax = max(mmax, cnt);
                }
            }
        }

        printf("Case %d: %d\n",k,mmax);
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn=10;
char s[maxn][maxn];
const int n=8;
int  dfs(int i,int j,int id,int jd,int &cnt)
{
    if(i<0||j<0||i>=n||j>=n || s[i][j] == '*')
    {
        cnt = 0;
        return 0;
    }
    if(s[i][j] == 'L')
    {
        ++cnt;
        dfs(i+id, j+jd, id, jd, cnt);
    }
    return 0;
}
int main()
{
    int kase;
    scanf("%d",&kase);
//    getchar();
    for(int k=1; k<=kase; k++)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%s",s[i]);
        }
        int mmax=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(s[i][j]=='*')
                {
                    int ans  = 0;
                    for(int id=-1; id<=1; id++)
                    {
                        for(int jd=-1; jd<=1; jd++)
                        {
                            if(id||jd)
                            {
                                int cnt=0;
                                dfs(i+id,j+jd,id,jd,cnt);
                                ans+=cnt;
                            }
                        }
                    }
                    mmax = max(mmax, ans);
                }
            }
        }
        printf("Case %d: %d\n",k,mmax);
    }
    return 0;
}


 
posted @ 2016-07-27 11:11  AC_dream  阅读(274)  评论(0编辑  收藏  举报