hdu3368之DFS
Reversi
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1047 Accepted Submission(s): 430
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?
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.
Please follow the format of the sample output.
********
********
********
***LD***
***DL***
********
********
********
********
********
**DLL***
**DLLL**
**DLD***
********
********
********
********
********
*D******
*DLLD***
***LL***
**D*D***
********
********
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的更多相关文章
- hdu3368 dfs 下棋
两颗黑子之间的白子可以翻装成黑子,两颗白子之间的黑子可以翻转成白子,对于一个给定位置,有八个方向有翻转其他颜色的子的可能.规则之一是下棋的位置一定要能翻转对方的子. 求最优情况:黑子能翻转的白子个数的 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
随机推荐
- XML样本(格式没区别,但是一个有结果 一个没结果)
xml样本01<orderlist> <order> <orderid>1</orderid> <ord ...
- iOS使用阿里云OSS对象存储 (SDK 2.1.1)
最近项目中用到了阿里云OSS对象存储,用来存储APP中图片.音频等一些数据.但坑爹的阿里云居然在11月20日将SDK版本更新到了2.1.1,然而网上给出的教程都是1.*版本的(针对iOS),两个版本所 ...
- js函数--关于toString和valueOf
js函数--关于toString和valueOf 标签(空格分隔): JavaScript 今天看到一个试题,实现如下语法的功能: var a = add(2)(3)(4); //9 这个就是一个高阶 ...
- 你好,C++(39)6.4.4 依葫芦画瓢:用C++表达设计结果(下)
6.4.4 依葫芦画瓢:用C++表达设计结果 完成上面的分析与设计之后,小陈感觉已经成竹在胸胜利在望了.他知道,只要完成了程序中的类以及类之间关系的分析和设计,整个程序就相当于已经完成了一大半.接下 ...
- (转载)小课堂UI-有关配色的一个小技巧
- asp.net实现 EXCEL数据导入到数据库功能
在项目中经常要用EXCEL导入数据到数据库,提高工作效率. 注意:EXCEL中的第一行不能导入. 下面是源码: IntoExcel.aspx: [csharp] <%@ Page Languag ...
- javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)
样式表有三种方式: 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. (也称作“内联样式”) 内部样式(internal Style Sheet):是写在 ...
- linux c数据库备份第五版
linux下c实现的数据库备份程序终于迎来第五版啦,这样改程序就暂告一段落啦,有点小激动呢...接下来的一周(可能两周)时间里,我会用一个小型的网络游戏(比拼99乘法)作为我学习linux c的毕业之 ...
- 清橙A1484
http://www.tsinsen.com/ViewGProblem.page?gpid=A1484### 题解: 在线插入并不好做,我们将所有操作离线,变为删除操作. 每次询问的时候对于当前B串所 ...
- 变量-if else while-运算符
变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...