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 ...
随机推荐
- 20141113--SQL 事务
---------------------触发器----------------------------- --触发器本质上还是一个存储过程,trigger --只不过不是通过exec调用执行,而是通 ...
- netstat命令[转]
原文地址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...
- flume+kafka (分区实现 默认单分区)
这篇文章主要是log4j+flume+kafka的内容 首先从从下面的地址下载flume+kafka的插件包 https://github.com/beyondj2ee/flumeng-kafka-p ...
- template_12特化与重载
1,重载函数模板f<int*>((int*)pi);//1f<int>((int*)pi);//2上面用int*替换第一个模板的T,用int来替换第二个模板的T.那么将得到两个 ...
- ADO.NET笔记——使用DataSet返回数据
相关知识: DataSet和DataAdapter的内部结构: DataSet通过DataAdapter从数据库中获取数据 DataSet对象内部包括一个集合(Tables),也就是可以拥有多个表(D ...
- WPF 绑定一(数据源为控件)
xaml: <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.co ...
- lower_bound和upper_bound
lower_bound:返回大于或等于val的第一个元素位置 upper_bound:返回大于val的第一个元素位置 两个函数用的都是二分查找
- 如何修改 Discuz 门户文章页默认视频大小
在 Discuz 系统中,论坛插入 Flash 等可以输入自定义的尺寸,但是门户文章页不可以修改.经过一番研究,找到了修改门户文章页默认视频大小的方法如下,希望对你有用:找到:/source/func ...
- Discuz X1.5 利用添加好友处存储xss进行蠕虫worm扩散
Discuz X1.5 在添加好友的地方有处存储xss,借助此处xss跟用户交互可以进行蠕虫指数扩散. 位置在添加好友处 x完之后的效果 点击后触发 ok 借助此存储xss,我们进行worm传播,dz ...
- CHARINDEX,PATINDEX,STUFF函数
-- CHARINDEX函数 -- 返回字符或者字符串在另一个字符串中的起始位置. -- 语法:CHARINDEX(expression1 , expression2 [,start_location ...