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[][];
int dx[]={,,-,,,-,,-};
int dy[]={,,,-,,,-,-};
bool vis[][]; bool ok(int x,int y)
{
if(x>= && x< && y>= && y<) return ;
return ;
} int work(int i,int j)
{
int ans=;
for(int k=;k<;k++)
{
int xx=i;
int yy=j;
int sum=;
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=; break;}
if (!ok(xx+dx[k],yy+dy[k])){sum=; break;}
}
ans+=sum;
}
return ans;
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int t=;t<=n;t++)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
cin>>a[i][j];
memset(vis,,sizeof(vis));
int res=;
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a[i][j]=='L')
{
for(int k=;k<;k++)
{
int xx=i+dx[k];
int yy=j+dy[k];
if(a[xx][yy]=='*' && !vis[xx][yy])
{vis[xx][yy]=; res=max(res,work(xx,yy));}
}
}
printf("Case %d: %d\n",t,res);
}
}
return ;
}

最新文章

  1. bzoj2330: [SCOI2011]糖果
  2. django(一)
  3. VS2013无法连接到SqlServer的问题解决
  4. python生成器实现杨辉三角
  5. js学习笔记5----函数传参
  6. Java中字符串的几个实例
  7. Objective-C语法之KVO的使用
  8. $.get的重写
  9. android解析XML总结(SAX、Pull、Dom三种方式) &lt;转载&gt;
  10. 百度之星A
  11. Web文件管理:elFinder.Net(支持FTP)
  12. 智能指针剖析(上)std::auto_ptr与boost::scoped_ptr
  13. Uva11582
  14. Android开发之漫漫长途 Ⅷ——Android Binder(也许是最容易理解的)
  15. Github管理自己的代码-远程篇
  16. HDU-1709 The Balance(生成函数)
  17. [Swift]两种线程休眠的方式
  18. 机器学习库--dlib
  19. Python之字符编码(一)
  20. Spring Boot源码分析

热门文章

  1. 怎么在Windows下安装Linux虚拟机
  2. CA认证
  3. svn clean up 出错解决方案
  4. 实时控制软件第一周 汽车ABS系统软件分析
  5. Python基础之列表
  6. 简易富文本编辑器bootstrap-wysiwyg源码注释
  7. asp.net MVC漏油配置总结
  8. java静态方法之线程安全问题
  9. Beego学习笔记——开始
  10. CodeForces 711C Coloring Trees