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)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...
随机推荐
- 关于IE6幽灵字体
前言:今天做项目的时候在IE6下出现了这样的一种现像,这种情况只在IE6下出现,最后在网友的帮助下这个问题最终得到了解决.所以马上作了下笔记! 情况如下图: 我在网上找了点资料出现IE6下幽灵字体的情 ...
- mvc中使用jsonp进行跨域请求详细说明
在web开发中,如果你要在不同域下进行数据异步请求,会出现一个No ‘Access-Control-Allow-Origin’ header is present on the requested r ...
- VS2010中属性页中,C/C++ -->预处理器定义
如上图中,在这里,WIN32._DEBUGE._UNICODE等其实是一些宏定义,在这里写上这些,相当于在本工程所有的文件中都写上了: #define WIN32 #define _DEBUG#def ...
- 在VMware中为Linux系统安装VM-Tools的详解教程
在安装Linux的虚拟机中,单击“虚拟机”菜单下的“安装Vmware-Tools”. 先介绍一下下面安装该工具时要用到的几个目录: /mnt 挂载目录,用来临时挂载别的文件系统,硬件设备 /tmp临时 ...
- css圆角
在CSS3中圆角属性,有四个.三个.两个和一个值. 四个值: 第一个值为左上角,第二个值为右上角,第三个值为右下角,第四个值为左下角.
- css行高line-height的用法(转)
本文导读: “行高“指一行文子的高度,具体来说是指两行文子间基线间的距离.在CSS,line-height被用来控制行与行之间的垂直距离.line- height 属性会影响行框的布局.在应用到一个块 ...
- tomcat下出现The file is absent or does not have execute&
启动tomcat出现The file is absent or does not have execute permission... Cannot find bin/catalina.sh The ...
- ref参数的用途
ref参数 能够将一个变量带入方法进行改变,改变完成后再将改变完成后的变量带出方法 ref参数要求在方法外必须为值赋值,而方法内可以不赋值 static void Main(string[] arr) ...
- 武汉科技大学ACM :1003: 零起点学算法67——统计字母数字等个数
Problem Description 输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符) Input 多组测试数据,每行一组 Output 每组输出一行 ...
- php 字符编码转换函数 iconv mb_convert_encoding比较
在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...