题目来源: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的更多相关文章

  1. SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707 算法决定一切,这道题目有很多方法解,个人认为这里 ve ...

  2. SRM 581 D2 L3:TreeUnionDiv2,Floyd算法

    题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501 这道题目开始以为是要在无向 ...

  3. SRM 581 D2 L2:SurveillanceSystem,重叠度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588 在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 ...

  4. 22. Generate Parentheses——本质:树,DFS求解可能的path

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 数据结构:关键路径,利用DFS遍历每一条关键路径JAVA语言实现

    这是我们学校做的数据结构课设,要求分别输出关键路径,我查遍资料java版的只能找到关键路径,但是无法分别输出关键路径 c++有可以分别输出的,所以在明白思想后自己写了一个java版的 函数带有输入函数 ...

  6. HDFS追本溯源:租约,读写过程的容错处理及NN的主要数据结构

    1.      Lease 的机制: hdfs支持write-once-read-many,也就是说不支持并行写,那么对读写的互斥同步就是靠Lease实现的.Lease说白了就是一个有时间约束的锁.客 ...

  7. ural 1106,二分图染色,DFS

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染 ...

  8. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  9. FS,FT,DFS,DTFT,DFT,FFT的联系和区别

    DCT变换的原理及算法 文库介绍 对于初学数字信号处理(DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...

随机推荐

  1. 近段时间学习html和CSS的一些细碎总结

    1.边框圆角属性:border-radius,取值能够是 百分比 / 自己定义长度,不能够取负值.假设是圆,将高度和宽度设置相等,而且将border-radius设置为100% 2.IE6,IE7,I ...

  2. 使用VS Code开发Angular 2应用程序所需配置文件的解析

    目录 package.json typings.json tsconfig.json launch.json settings.json tasks.json package.json: 这是项目的基 ...

  3. zendguard安装破解

    http://www.thefox.cn/zend-guard.shtml 到官网下载6.0版本安装后 URL: http://www.zend.com/en/products/guard/downl ...

  4. IE下兼容Css+HTML5

    /*! HTML5 Shiv vpre3.6 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed Uncompressed source: h ...

  5. php 字符编码转换函数 iconv mb_convert_encoding比较

    在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...

  6. Python新手学习基础之运算符——位运算

    位运算符 位运算实际上是把数字看作二进制来进行计算,它的运算法则如下: 结合实例,来看下位运算是如何进行的吧: 位运算在实际应用中用途很广泛,比如我们经常听到的子网掩码,它其实就是和IP地址做了按位与 ...

  7. C#中类的属性

    1.[StructLayout] 控制类或结构的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列.如果要将类传递给需要指定布局的非托管代码,则显式控制类布局是重要的. 2.[Marshal ...

  8. Android学习笔记--存储方案(SharedPreference、文件IO)

    1. SharedPreference SharedPreference可以很容易的保存key-value对,通常用于保存配置信息.保存的步骤 1. 获得SharedPreferences对象 (最后 ...

  9. 另一种root方法,Android boot.img破解

    一.破解原理 Android手机获得Root权限,其实就是让/system和/data分区获得读写的权限.这两个分区的权限配置,一般在根分区的init.rc文件中,修改这个文件可永久获得root权限. ...

  10. Visual Studio下Qt编程中对中文的处理

    Visual Studio下Qt编程中对中文的处理 本文为原创文章,原文地址http://www.cnblogs.com/c4isr/p/qt_develop_in_vs.html Visual St ...