HDU3368 Reversi

来源:互联网 发布:java.util. 无法引入 编辑:程序博客网 时间:2024/06/05 19:00
Reversi
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

 

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. 
 

Sample Input

3***************************LD******DL*********************************************DLL*****DLLL****DLD********************************************D*******DLLD******LL*****D*D*******************
 

Sample Output

Case 1: 1Case 2: 3Case 3: 0
 

题意
在一张棋盘上,有黑棋和白棋,让你在一个位置放置黑棋,使当前能够翻转的白棋数量最大。


分析
dfs搜索题,枚举每个位置,然后向八个方向搜索即可。搜索时记录中途的白棋个数,返回每个位置能翻转的最大白棋个数。本题只需注意在一点放置黑棋时,八个方向都有可能有白棋被翻转。

 

AC代码如下

#include <cstdio>#include <cstring>#define N 10using namespace std;int dr[][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};char m[N][N];int res,sum;void dfs(int x,int y,int dir,int num){    if(x < 0 || x > 7 || y < 0 || y >7) return ;    if(dir == 8)    {        for(int i = 0 ; i < 8 ; i++)//八个方向dfs            dfs(x + dr[i][0],y + dr[i][1],i,num);    }    else    {        if(m[x][y] == '*') return ;        else if(m[x][y] == 'D')        {            sum += num;            return ;        }        else dfs(x + dr[dir][0],y + dr[dir][1],dir,num+1);    }    return ;}void dosth(){    res = 0;    for(int i = 0 ; i < 8; i++)        for(int j = 0 ; j < 8; j++)    {        sum = 0;        if(m[i][j] == '*') dfs(i,j,8,0);        if(res < sum) res = sum;    }}int main(){    int t;    scanf("%d",&t);    int cas = 0;    while(t--)    {        for(int i = 0 ; i < 8 ; i++)        {            scanf("%s",m[i]);            getchar();        }        dosth();        printf("Case %d: %d\n",++cas,res);    }    return 0;}

 

0 0