Aizu 2325 Mysterious Maze
走迷宫 ~
不同的是题目给了你转向的方向序列
dis[x][y]表示到(x,y) 使用了最少的转向次数
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include <queue> using namespace std; const int maxn = 1010;
const int inf = (1<<30); int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1}; struct node
{
int x, y;
node(int i = 0, int j = 0)
{
x = i, y = j;
}
}; char s[1000010];
int g[maxn][maxn], dis[maxn][maxn], cost[1000010][4], dir[1000010]; int W,H,N;
queue<node>Q; void init()
{
memset(dis, -1, sizeof(dis));
memset(g, 0, sizeof(g)); dir[0] = 0;
for(int i = 1; i <= N; ++ i)
if(s[i] == 'L') dir[i] = (dir[i-1]+3)%4;
else dir[i] = (dir[i-1]+1)%4; for(int i = N; i >= 0; -- i)
for(int j = 0; j < 4; ++ j)
{
if(dir[i] == j) cost[i][j] = 0;
else
{
if(i == N) cost[i][j] = inf;
else cost[i][j] = cost[i+1][j] +1;
}
}
for(int i = 0; i < 4; ++ i) cost[N+1][i] = inf; while(!Q.empty()) Q.pop();
} bool solve(int sx, int sy, int ex, int ey)
{
Q.push(node(sx, sy));
dis[sx][sy] = 0;
while(!Q.empty())
{
node u = Q.front();
Q.pop();
int now = dis[u.x][u.y];
for(int i = 0; i < 4; ++ i)
{
int nx = u.x+dx[i], ny = u.y+dy[i];
if(g[nx][ny] && cost[now][i] < inf)
{
if(nx == ex && ny == ey) return true;
int tem = now+cost[now][i];
if(dis[nx][ny] == -1 || tem < dis[nx][ny])
{
dis[nx][ny] = tem;
Q.push(node(nx, ny));
}
}
}
}
return false;
} void show()
{
for(int i = 0; i <= W+1; ++ i)
{
for(int j = 0; j <= H+1; ++ j)
printf("%d ", g[i][j]);
puts("");
}
} int main()
{
while(scanf("%d%d%d", &W, &H, &N) ==3 && W+H+N)
{
scanf("%s", s+1);
init();
int sx, sy, ex, ey;
for(int i = 1; i <= W; ++ i)
{
scanf("%s", s+1);
for(int j = 1; j <= H; ++ j)
{
if(s[j] != '#') g[i][j] = 1;
if(s[j] == 'S') sx = i, sy = j;
if(s[j] == 'G') ex = i, ey = j;
}
}
// show();
if(solve(sx, sy, ex, ey)) puts("Yes");
else puts("No");
}
return 0;
}
Aizu 2325 Mysterious Maze的更多相关文章
- IEEEXtreme 10.0 - Mysterious Maze
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...
- IEEEXtreme 极限编程大赛题解
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 IEEEXtreme全球极限编程挑战赛,是由IEEE主办,IEEE学生分会组织承办.IEEE会员参与指导和监督的.IEEE学生会员以团队 ...
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- (期望)A Dangerous Maze(Light OJ 1027)
http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- sql(转自http://www.imooc.com/article/2325)
http://www.imooc.com/article/2325
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
随机推荐
- 20141110--SQL视图
--------------------------视图--------------------- --视图是一个虚拟表,数据来自于原表,原表的数据改变,视图也会改变 select Student.S ...
- iOS Foundation框架 -4.NSDate类的简单用法
NSDate为日期时间类对象,简单操作: 注意:直接NSLog输出NSDate对象,默认是以0时区为标准,因此会比北京时间少8小时 1.将Date格式转换为自定义格式的字符串格式 // 自定义Date ...
- 借Windows说明Linux分区和挂载点[转]
在介绍Linux分区和挂载点前,我想先说一个Windows的例子,Windows大家都比较熟,再借这个例子来说明什么是Linux分区和挂载点. 1.消失了的分区 在WinPE下,我将一块硬盘分成一个主 ...
- mongodb的常用操作
对于nosql之前工作中有用到bekerlydb,最近开始了解mongodb,先简单写下mongodb的一些常用操作,当是个总结: 1.mongodb使用数据库(database)和集合(collec ...
- 使用 Visual Studio Team Test 进行单元测试和java中的测试
C#中test测试地 方法一. 1.从NUnit官网(http://www.nunit.org/index.php)下载最新版本NUnit,当前版本为NUnit2.5.8. 2.安装后,在VS2008 ...
- js中typeof可以准确判断哪些变量类型
typeof 运算符返回一个用来表示表达式的数据类型的字符串. 可能的字符串有:"number"."string"."boolean".& ...
- Android 中的WiFi剖析
Android的WiFi 我们通常看到WiFi的守护进程wpa_supplicant在我们的ps的进程列表中,这个就是我们的wifi守护进程.wpa_supplicant在external/wpa_s ...
- Eclipse下jad反编译之“类文件查看器”不能处理给定的输入错误解决
Eclipse中的插件下载,安装和配置可以参考我的另一篇文章:MyEclipse反编译Class文件 下面重点讲解如何使用jad反编译 1.在DOS窗口中,到class所在目录,直接运行 >ja ...
- C# 将cookiecontainer写到本地
public static void WriteCookiesToDisk(string file, CookieContainer cookieJar) { using(Stream stream ...
- linux总线、设备和设备驱动的关系
之一:bus_type 总线是处理器和一个或多个设备之间的通道,在设备模型中,所有的设备都通过总线相连,甚至是内部的虚拟"platform"总线.可以通过ls -l /sys/bu ...