• HDU:3368-Reversi(暴力枚举)


    Reversi

    Time Limit: 5000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1226    Accepted Submission(s): 258
    Problem Description
    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.
     
    SampleInput
    3
    ********
    ********
    ********
    ***LD***
    ***DL***
    ********
    ********
    ********
    
    ********
    ********
    **DLL***
    **DLLL**
    **DLD***
    ********
    ********
    ********
    
    ********
    ********
    *D******
    *DLLD***
    ***LL***
    **D*D***
    ********
    ********
     
    SampleOutput
    Case 1: 1
    Case 2: 3
    Case 3: 0

    题目大意:就是黑子要和原来已经在的黑子形成一条水平线或垂直线或斜线,并且在两个黑子之间不能用空格,也不能有其他黑子只能含有白子。这样才能放置,求最多能把多少白子变成黑子。

    题解:从白子的八个方向开始搜能放置黑子的位置,然后在判断如果这个位置放置黑子能把多少白子变成黑子。记录最大的数量。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char a[10][10];
    int dx[]={0,1,-1,0,1,-1,1,-1};
    int dy[]={1,0,0,-1,1,1,-1,-1};
    bool vis[10][10];
    
    bool ok(int x,int y)
    {
        if(x>=0 && x<8 && y>=0 && y<8) return 1;
         return 0;
    }
    
    int work(int i,int j)
    {
      int ans=0;
        for(int k=0;k<8;k++)
        {
            int xx=i;
            int yy=j;
            int sum=0;
            while(ok(xx+dx[k],yy+dy[k]))
            {
                xx+=dx[k];
                yy+=dy[k];
               if (a[xx][yy]=='L') sum++;
               if (a[xx][yy]=='D') break;
               if (a[xx][yy]=='*') {sum=0; break;}
               if (!ok(xx+dx[k],yy+dy[k])){sum=0; break;}
            }
            ans+=sum;
        }
        return ans;
    }
    
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for(int t=1;t<=n;t++)
            {
                for(int i=0;i<8;i++)
                 for(int j=0;j<8;j++)
                     cin>>a[i][j];
                 memset(vis,0,sizeof(vis));
                 int res=0;
                for(int i=0;i<8;i++)
                    for(int j=0;j<8;j++)
                        if(a[i][j]=='L')
                        {
                            for(int k=0;k<8;k++)
                            {
                                int xx=i+dx[k];
                                int yy=j+dy[k];
                                if(a[xx][yy]=='*' && !vis[xx][yy])
                                  {vis[xx][yy]=1; res=max(res,work(xx,yy));}
                            }
                        }
                printf("Case %d: %d
    ",t,res);
            }
        }
        return 0;
    }
  • 相关阅读:
    每天一道Java题[4]
    每天一道Java题[3]
    每天一道Java题[2]
    关于OOCSS架构
    新blog开张!
    [原]C++拾遗
    mark
    今天的情况(也是10月份的总结)
    11月份的总结
    Linux管道编程实例
  • 原文地址:https://www.cnblogs.com/stepping/p/6288857.html
Copyright © 2020-2023  润新知