题目来源: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. springmvc 基础

    在最简单的springmvc应用程序中,控制器是唯一需要在java web部署描述文件(web.xml)中配置的servlete(springmvc的控制器是Dispatcher Servlet).每 ...

  2. 深入浅出 RPC - 深入篇

    <深入篇>我们主要围绕 RPC 的功能目标和实现考量去展开,一个基本的 RPC 框架应该提供什么功能,满足什么要求以及如何去实现它? RPC 功能目标 RPC 的主要功能目标是让构建分布式 ...

  3. eclipse打包 jar文件,中文乱码解决方案

    直接通过eclipse浏览源代码时,发现中文注释为乱码的问题.其实这个eclipse默认编码造成的问题.可以通过以下方法解决: 修改Eclipse中文本文件的默认编码:windows->Pref ...

  4. ORACLE之SQL语句内部解析过程【weber出品】

    一.客户端通过监听连接到数据库,数据库开启一个server process进程来接收客户端传过来的sql. 1.这条sql语句从来都没有被执行过.(硬解析) 2.这条sql语句被执行过.(软解析) 二 ...

  5. Sudoku Killer

    算法:深搜 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份 ...

  6. UVA 1344 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Here is a famous story in Chinese history. That was about 2300 years ago ...

  7. js判断是否微信浏览器打开

    function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)==&qu ...

  8. [Mugeda HTML5技术教程之7]添加动画

    前一节我们讲述了怎么在新建的作品中添加元素,元素加好以后我们还想让他们动起来,来实现比较炫的效果.这节我们将要讲述怎么给元素添加动画.Mugeda动画是通过时间轴和帧来实现的.通过在时间轴上创建图层和 ...

  9. DIV 遮挡问题总结

    1.DIV被Silverlight遮挡, 加入windowless参数即可. <object id=”silverlight” data=”data:application/x-silverli ...

  10. [Flask Security]当不能通过认证的时候制定跳转

    Flask Security这个插件能对用户权限进行很好的控制. 通过三个model实现: User,存放用户数据 Role,存放角色数据 User_Role.存放用户角色信息 user_datast ...