Reversi

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

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

题目大意:首先,给你一副走到一半的棋盘(8*8),现在轮到黑棋走。规则如下:若新增的黑棋和另外一颗已存在的黑棋之间全是白棋(即没有空格或另外的黑棋),例如:黑(new)白白黑(old),则里面的白棋全变黑棋,另外,[边界]白白黑(new),这种情况不算。问:这颗黑棋放下后,最多能够翻转多少颗白棋?(输入时字母D(Dark)代表黑棋,字母L(Light)代表白棋,*号代表空格。)

简单分析:遍历所有棋牌上的位置,若该位置为空,则用一个DFS分别从该位置的8个方向(即上[0,1],下[0,-1],左[-1,0],右[1,0],左上[-1,-1],右上[1,-1],左下[-1,1],右下[1,1])依次试探。同时把每一个位置最多能够翻转的白棋个数存放在变量sum中。

注意这种情况:

*****D**
*****L**
*****L**
*****L**
DLLLL***
********
********
********
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=10;
char Map[MAX][MAX];
int dir[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,1,-1,-1,1};
int sum,ans; void DFS(int x,int y,int i,int num){
if(x<0 || y<0 || x>=8 || y>=8 || Map[x][y] == '*')return;
if(Map[x][y] == 'L')++num;
if(Map[x][y] == 'D'){ans+=num;return;}//表示i这个方向能翻转num个白棋
DFS(x+dir[i][0],y+dir[i][1],i,num);
} int main(){
int t,num=0;
cin>>t;
while(t--){
sum=0;
for(int i=0;i<8;++i)cin>>Map[i];
for(int i=0;i<8;++i){
for(int j=0;j<8;++j){
if(Map[i][j] == '*'){
ans=0;
for(int k=0;k<8;++k){//对每个点8个方向进行搜索
DFS(i+dir[k][0],j+dir[k][1],k,0);
}
sum=sum>ans?sum:ans;
}
}
}
cout<<"Case "<<++num<<": "<<sum<<endl;
}
return 0;
}

hdu3368之DFS的更多相关文章

  1. hdu3368 dfs 下棋

    两颗黑子之间的白子可以翻装成黑子,两颗白子之间的黑子可以翻转成白子,对于一个给定位置,有八个方向有翻转其他颜色的子的可能.规则之一是下棋的位置一定要能翻转对方的子. 求最优情况:黑子能翻转的白子个数的 ...

  2. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  3. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  4. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  5. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  6. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  7. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  8. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  9. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

随机推荐

  1. iOS开发 UITableView之cell

    1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...

  2. 重新开始学习javase_控制程序流程

    @学习thinking in java 二.控制程序流程 负数使用 Java 运算符:运算符以一个或多个自变量为基础,可生成一个新值.自变量采用与原始方法调用不同的一种形式,但效果是相同的.根据以前写 ...

  3. python正则表达式入门

    基本概念 使用正则表达式需要import re 表达式前加r避免转义 \d代表数字,\s代表空白字符,\w代表字母+数字. .代表任意单个字符 {m,n}代表前面字符至少出现m次,最多出现n次. (x ...

  4. 跨域的iframe自动调整高度(cross-domain iframe resizer)

    可以使用iframe-resizer项目地址: http://davidjbradshaw.github.io/iframe-resizer/演示地址: http://davidjbradshaw.c ...

  5. Android源码下载方法详解

    转自:http://www.cnblogs.com/anakin/archive/2011/12/20/2295276.html Android源码下载方法详解 相信很多下载过内核的人都对这个很熟悉 ...

  6. Jquery的外部链接和编写样式

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></ ...

  7. 插入数据显示 Duplicate entry '4913' for key 'user_id'

    提示信息翻译:插入4913 使索引重复 分析:索引如果是primary unique这两两种,那么数据表的数据对应的这个字段就必须保证其每条记录的唯一性.否则就会产生这个错误. 一般发生在对数据库写操 ...

  8. JS中 confirm()方法的使用?

    confirm() 方法用于显示一个带有指定消息和 OK 及取消按钮的对话框. 如果用户点击确定按钮,则 confirm() 返回 true.如果点击取消按钮,则 confirm() 返回 false ...

  9. (C初学) 对数组与指针的一些浅显的理解

    因为课堂上没听懂,又看不懂教科书(<C语言程序设计教程>第3版 谭浩强,张基温编著)上晦涩的表达方式,昨天晚上特意拿<C语言入门经典>这本书自己研究了一晚的数组与指针. 先来一 ...

  10. python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中

    使用sys.excepthook函数进行全局异常的获取. 1. 使用MessageDialog实现异常显示: 2. 使用logger把捕获的异常信息输出到日志中: 步骤:定义异常处理函数, 并使用该函 ...