走迷宫 ~

不同的是题目给了你转向的方向序列

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的更多相关文章

  1. IEEEXtreme 10.0 - Mysterious Maze

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...

  2. IEEEXtreme 极限编程大赛题解

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 IEEEXtreme全球极限编程挑战赛,是由IEEE主办,IEEE学生分会组织承办.IEEE会员参与指导和监督的.IEEE学生会员以团队 ...

  3. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

  4. (期望)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 ...

  5. 1204. Maze Traversal

    1204.   Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...

  6. uva705--slash maze

    /*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...

  7. 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 ...

  8. sql(转自http://www.imooc.com/article/2325)

    http://www.imooc.com/article/2325

  9. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...

随机推荐

  1. CSS学习备忘

    CSS样式控制行间距,用line-height 测试一下: <div style="line-height:6px"> 111<br> 222<br& ...

  2. C++ 11 之初始化

    1.4中不同初始化的形式     a.string s("zhl").int i(3);    //括号初始化     b.string s="zhl".int ...

  3. android activity空指针异常解决问题解决

    今天在开发过程中,遇到了一个错误 unable to instantiate activity componentinfo java.lang.nullpointerexception google之 ...

  4. if语句代码优化

    if($sum==7){ $sz+=135; }elseif($sum==5){ $sz+=80; }elseif($sum==6){ $sz+=97; }elseif($sum==4){ $sz+= ...

  5. js中substring和substr的用法比较

    推荐使用substring 方法   stringObject.substring(start,stop)   stringObject.substr(start,length)   定义和用法 提取 ...

  6. Stanford parser学习:LexicalizedParser类分析

    上次(http://www.cnblogs.com/stGeekpower/p/3457746.html)主要是对应于javadoc写了下LexicalizedParser类main函数的功能,这次看 ...

  7. Zencart 国家排序及中文名称的扩展

    最终实现效果如上 具体步骤: 1. 手动或SQL修改数据表,增加2个字段 ) ) '; 2. 修改admin/countries.php文件,增加表单插入编辑功能, 共计7处,此处忽略具体代码. 3. ...

  8. c#读写注册表示例分享

    c#读写注册表示例,示例中有详细注释. 代码: //写注册表RegistryKey regWrite;//往HKEY_CURRENT_USER主键里的Software子键下写一个名为“Test”的子键 ...

  9. HTML5的页面资源预加载技术(Link prefetch)加速页面加载

    不管是浏览器的开发者还是普通web应用的开发者,他们都在做一个共同的努力:让Web浏览有更快的速度感觉.有很多已知的技术都可以让你的网站速度变得更快:使用CSS sprites,使用图片优化工具,使用 ...

  10. TextView中gravity属性值测定

    Attributes Explain top 不改变控件大小,对齐到容器顶部 bottom 不改变控件大小,对齐到容器底部 left 不改变控件大小,对齐到容器左侧 right 不改变控件大小,对齐到 ...