HDU 3368 Reversi 解题报告

本文详细解析了黑白棋(Reversi)的游戏规则,并通过一个具体的编程实例展示了如何计算玩家下一步能够翻转对手棋子的最大数量。介绍了游戏的基本概念、棋盘布局、玩家操作流程及胜负条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Reversi

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 805    Accepted Submission(s): 332


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.
 

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

Sample Output
  
Case 1: 1 Case 2: 3 Case 3: 0
 

Source
 

Recommend
lcy
 


 

//本题题意就是黑白棋的规则,让你判断下一个D最多能吃掉多少个L
//很简单的枚举,都不需要DFS...找到一个没下子的点...八个方向找L
//如果是L就跳到这个点以相同方向继续判断一直到有D为止,把每个方向搜到的步数加起来
//碰到墙,步数就为0...
//一开始我从D搜结果没把结果加起来...最后发现要从*搜...
//给点自己的数据吧 : 
/*
	********
	********
	**D*D*D*
	***LLL**
	**DL*LD*
	***LLL**
	**D*D*D*
	********
	8

	********
	********
	********
	********
	********
	********
	********
	********
	0

	DDDDDDDD
	DDDDDDDD
	DDDDDDDD
	DDDDDDDD
	DDDDDDDD
	DDDDDDDD
	DDDDDDDD
	DDDDDDDD
	0

	*LLLLLLL
	L*******
	L*******
	L*******
	L*******
	L*******
	L*******
	LLLLLLL*
	0

	
*/
#include<algorithm>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<stack>
#include<ctime>
#include<iomanip>
#include<sstream>
#include<vector>
#include<map>
#include<set>
#include<list>
using namespace std;
int x[8]={1,1,1,-1,-1,-1,0,0};
int y[8]={0,1,-1,1,0,-1,1,-1};//8个方向
char a[20][20];
int judge(int a,int b)  
{  
    if(a>=1&&a<=8&&b>=1&&b<=8)  
        return 1;  
    return 0;  
} 
int dfs(int i,int j)
{
    int m,s=0,p=0;
    for(m=0;m<8;m++)
    {
        int ii=i,jj=j,count=0;
        while(judge(ii,jj))
        {
            
            if(judge(ii+y[m],jj+x[m]) == 0)
            {
                count = 0;
                break;
            }
            else if(a[ ii+y[m] ][ jj+x[m] ] == 'D')
            {
                p+=count;
                break;
            }
            else if(a[ ii+y[m] ][ jj+x[m] ] =='*')
            {
                break;
            }
            else if(a[ ii+y[m] ][ jj+x[m] ] == 'L')
            {
                count++;
                ii+=y[m];
                jj+=x[m];
            }
        }
    }
    return p;
}
int main()
{    
//    freopen("in.txt","r",stdin);
    int n,nn,l=0;
    scanf("%d",&n);
    for(nn=1;nn<=n;nn++)
    {
        l=0;
        int i,j;
        for(i=1;i<=8;i++)
            for(j=1;j<=8;j++)
                cin>>a[i][j];
        for(i=1;i<=8;i++)
		{
            for(j=1;j<=8;j++)
			{
                if(a[i][j]=='*')
                {
                    int temp = dfs(i,j);
                    if(temp>l)
                        l=temp;
                }
			}
		}
        printf("Case %d: %d\n",nn,l);
        getchar();
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值