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)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...
随机推荐
- i2c sub system __i2c_board_list/klist_devices/klist_drivers
i2c_devinfo全局链表: __i2c_board_list 用来挂接 i2c_board_info,这个信息用来生成 i2c_client i2c_client 链表: i2c_bus_typ ...
- MVC4 数据库连接字串
1.SQL Server <add name="DBEntities" connectionString="Data Source=.;Initial Catalo ...
- Cretiria查询应用(一)
1.查询所有 Criteria criteria=session.createCriteria(Dept.class); //调用list()方法 List<Dept> li ...
- CSS背景特殊属性值
CSS代码示例-背景附着属性(background-attachment)-[背景图固定不动,不跟随滚动条滚动]:<html><head><title>背景附着属性 ...
- sqlserver编程基本语法
一.定义变量 --简单赋值 declare @a int set @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @ ...
- .net framework 注册到IIS上
首先要安装好所需的IIS版本和.net framework 各版本,注册方式如下: 1.1:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_re ...
- C#析构函数,类运行结束后运行
public class Students { //创建对像时使用 public Students(string name, int age, char gender, int englist, in ...
- ubuntu 快捷键和安装知识知识
本文节选自“The Official Ubuntu Book, 7th Edition.pdf” 快捷键部分直接引用原书中图片. Linux Folders Learning Unity Keyboa ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- jQuery实现图片预加载提高页面加载速度和用户体验
我们在做网站的时候经常会遇到这样的问题:一个页面有大量的图片导致页面加载速度缓慢,经常会出现一个白页用户体验很不好.那么如何解决这个问题呢?首先我们会想到的是提高服务器性能,使用静态缓存等手段来加快图 ...