SRM 588 D2 L3:GameInDarknessDiv2,DFS
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12710
采用DFS搜索,第一次写的时候忘了加访问标志,结果状态空间呈指数增长(主要是因为有大量重复的状态),根本算不出结果,后来加入访问标志数组 v 后,就能保证不访问重复的状态的了。这道题目的启示就是使用DFS一定要记住确保不访问重复的状态,有些时候很容易就忘了这一点,导致算法失败。
代码如下:
#include <algorithm>
#include <iostream>
#include <sstream> #include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring> using namespace std; /*************** Program Begin **********************/
vector <string> f;
string M;
bool v[2600][50][50];
bool BobWin = false;
void move(int Ax, int Ay, int Bx, int By, int steps)
{
v[steps][Bx][By] = true;
if (steps == M.size()) {
BobWin = true;
return;
} else {
int nAx = Ax;
int nAy = Ay;
switch (M[steps]) {
case 'U':
nAy = Ay - 1;
break;
case 'R':
nAx = Ax + 1;
break;
case 'L':
nAx = Ax - 1;
break;
case 'D':
nAy = Ay + 1;
break;
}
if (nAx == Bx && nAy == By) {
return;
}
int nBx = Bx;
int nBy = By;
// 上
nBx = Bx;
nBy = By - 1;
if ( nBy >= 0 && '.' == f[nBy][nBx] && !(nAx == nBx && nAy == nBy) && !v[steps+1][nBx][nBy] ) {
move(nAx, nAy, nBx, nBy, steps+1);
}
// 下
nBx = Bx;
nBy = By + 1;
if ( nBy <= f.size() - 1 && '.' == f[nBy][nBx] && !(nAx == nBx && nAy == nBy) && !v[steps+1][nBx][nBy] ) {
move(nAx, nAy, nBx, nBy, steps+1);
}
// 左
nBx = Bx - 1;
nBy = By;
if ( nBx >= 0 && '.' == f[nBy][nBx] && !(nAx == nBx && nAy == nBy) && !v[steps+1][nBx][nBy] ) {
move(nAx, nAy, nBx, nBy, steps+1);
}
// 右
nBx = Bx + 1;
nBy = By;
if ( nBx <= f[0].size() - 1 && '.' == f[nBy][nBx] && !(nAx == nBx && nAy == nBy) && !v[steps+1][nBx][nBy] ) {
move(nAx, nAy, nBx, nBy, steps+1);
}
}
} class GameInDarknessDiv2 {
public:
string check(vector <string> field, vector <string> moves) {
string res = "";
f = field;
int Ax = 0, Ay = 0, Bx = 0, By = 0;
for (int i = 0; i < f.size(); i++) {
for (int j = 0; j < f[0].size(); j++) {
if ('A' == f[i][j]) {
Ay = i;
Ax = j;
f[i][j] = '.';
} else if ('B' == f[i][j]) {
By = i;
Bx = j;
f[i][j] = '.';
}
}
}
M = "";
for (int i = 0; i < moves.size(); i++) {
M += moves[i];
}
BobWin = false;
memset(v, 0, sizeof(v));
move(Ax, Ay, Bx, By, 0);
if (BobWin) {
return "Bob wins";
} else {
return "Alice wins";
}
}
};
/************** Program End ************************/
SRM 588 D2 L3:GameInDarknessDiv2,DFS的更多相关文章
- SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707 算法决定一切,这道题目有很多方法解,个人认为这里 ve ...
- SRM 581 D2 L3:TreeUnionDiv2,Floyd算法
题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501 这道题目开始以为是要在无向 ...
- SRM 581 D2 L2:SurveillanceSystem,重叠度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588 在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 ...
- 22. Generate Parentheses——本质:树,DFS求解可能的path
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 数据结构:关键路径,利用DFS遍历每一条关键路径JAVA语言实现
这是我们学校做的数据结构课设,要求分别输出关键路径,我查遍资料java版的只能找到关键路径,但是无法分别输出关键路径 c++有可以分别输出的,所以在明白思想后自己写了一个java版的 函数带有输入函数 ...
- HDFS追本溯源:租约,读写过程的容错处理及NN的主要数据结构
1. Lease 的机制: hdfs支持write-once-read-many,也就是说不支持并行写,那么对读写的互斥同步就是靠Lease实现的.Lease说白了就是一个有时间约束的锁.客 ...
- ural 1106,二分图染色,DFS
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染 ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- FS,FT,DFS,DTFT,DFT,FFT的联系和区别
DCT变换的原理及算法 文库介绍 对于初学数字信号处理(DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...
随机推荐
- js控制select数据绑定下拉列表
JS代码段: <script type="text/javascript"> $(document).ready(function () { $("sele ...
- 关于 视频同步vsync 信号在不同一时候钟域採样问题
今天调试 视频 4k(3840 x 1920)的vsync信号(时钟为 297Mhz) 进入 170Mhz 的时钟域, 发现输出来的信号信号抖动特别厉害.后来才发现这是不同一时候钟域 造成的影响. 快 ...
- samba服务简介
1.1 Samba 概述Samba主要用于Windows和Linux之间的文件共享,也一样用于Linux和Linux之间的共享文件:不过对于Linux和Linux之间共享文件有更好的网络文件系统NFS ...
- include子页面传递过来的参数传递到后台
在页面上可以使用 ${param.moduleId}来获取 在判断中也可以使用${param.moduleId == "test" ? "1":"2& ...
- string.Format 指定字符串宽度
语法: { index[,alignment][:formatString]} index,为索引号,不用多说. alignment,是一个带符号的整数,绝对值的大小表示字段的宽度. formatSt ...
- iOS分享 - AFNetworking之多图片/文件上传
在分享经验之前,先说点题外话,之前的一个项目涉及到了多图片的上传,本来以为是一个很简单的事情,却着实困扰了我好久,究其原因,一是我不够细心,二是与后台人员的交流不够充分.在此,我想将我的老师常说的一句 ...
- IIS上部署网站404错误
新装的系统上部署.net网站遇到403.404错误,可能原因记录: 1.应用程序池选择错误,一般选择4.0的 2.ASP.NET4.0应用程序池未安装(一般先安装了framework4.0,后安装ii ...
- Uploadify插件使用方法
1.下载所需文件 2.导入所需文件,还需要应用jquery.js文件 3.导入css.js uploadify.css.jquery.uploadify.min.js 4.前端代码 p标签存放uplo ...
- AjaxPro.2.dll使用方法简介
1.添加对 AjaxPro.2.dll 的引用 2.在 webconfig 中 system.web 节点下 进行如下配置 <httpHandlers> <add verb=&quo ...
- 自动启动docker container
当系统启动的时候要启动docker container, 可以利用systemctl来实现 比如拿mongodb为例 创建 /usr/lib/systemd/system/docker_mongodb ...