http://acm.hdu.edu.cn/showproblem.php?pid=1180

注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是水平的还是垂直的。

并且我们需要知道当前到达楼梯这个点的方向,这样才知道下一个往哪个方向走,可以根据dir数组来判断方向。

楼梯不用判重。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std; char field[][];
int dir[][]={{,},{-,},{,},{,-}};
int n,m;
struct point
{
int x,y,time;
bool operator < (const point a) const
{
return time>a.time;
}
};
point s,e; bool check(point t)
{
if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&field[s.x][s.y]!='*')
return true;
return false;
} int bfs()
{
priority_queue<point>que;
que.push(s);
field[s.x][s.y]='*';
while(que.size())
{
point t=que.top(); que.pop(); if(t.x==e.x&&t.y==e.y) return t.time;
for(int i=;i<;i++)
{
s=t;
s.x=t.x+dir[i][];
s.y=t.y+dir[i][];
if(check(s))
{
if(field[s.x][s.y]=='|')
{
//printf("%d %d %d %d %d\n",dir[i][0],dir[i][1],t.x,t.y,t.time);
if(s.time%==) //偶数 那么 还是 '|'
{
if(dir[i][]!=) //如果是垂直方向只要1分钟就可以到达
{
s.x+=dir[i][];
// printf("%d %d %d\n",s.x,s.y,s.time);
if(!check(s)) continue;
s.time+=;
// printf("%d %d %d\n",s.x,s.y,s.time);
field[s.x][s.y]='*';
que.push(s);
}
else //是水平方向,就要等2秒 因为要等一秒
{
s.y+=dir[i][];
if(!check(s)) continue;
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
}
else //奇数 那么变成了 '-' 下面同理
{
if(dir[i][]!=)
{
s.x+=dir[i][];
// printf("%d %d %d\n",s.x,s.y,s.time);
if(!check(s)) continue;
s.time+=;
// printf("%d %d %d\n",s.x,s.y,s.time);
field[s.x][s.y]='*';
que.push(s);
}
else
{
s.y+=dir[i][];
if(!check(s)) continue;
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
}
}
else if(field[s.x][s.y]=='-')
{
if(s.time%==)
{
if(dir[i][]!=)
{
s.x+=dir[i][];
if(!check(s)) continue;
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
else
{
s.y+=dir[i][];
if(!check(s)) continue;
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
}
else
{
if(dir[i][]!=)
{
s.x+=dir[i][];
if(!check(s)) continue;
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
else
{
s.y+=dir[i][];
if(!check(s)) continue;
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
}
}
else
{
s.time+=;
field[s.x][s.y]='*';
que.push(s);
}
}
}
}
}
int main()
{
//freopen("a.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
getchar();
for(int i=;i<n;i++)
{
scanf("%s",field[i]);
// printf("%s\n",field[i]);
for(int j=;j<m;j++)
{
if(field[i][j]=='S')
{
s.x=i;
s.y=j;
s.time=;
}
else if(field[i][j]=='T')
{
e.x=i;
e.y=j;
}
}
}
printf("%d\n",bfs());
}
return ;
}

hdu - 1180 诡异的楼梯 (bfs+优先队列)的更多相关文章

  1. hdu 1180 诡异的楼梯 (bfs)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Sub ...

  2. hdu 1180诡异的楼梯(bfs)

    诡异的楼梯 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submis ...

  3. hdu 1180 诡异的楼梯(优先队列)

    Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.  比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向 ...

  4. HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)     ...

  5. HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...

  6. HDU 1180 诡异的楼梯(BFS)

    诡异的楼梯 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. hdu 1180:诡异的楼梯(BFS广搜)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  8. hdu 1180 诡异的楼梯

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  9. HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)

    Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在 ...

随机推荐

  1. JS 学习笔记--10---基本包装类型

    练习中使用的浏览器是IE10,如果有什么错误或者不同意见,希望各位朋友能够指正,练习代码附在后面 1.基本包装类型:    首先是基本类型,但又是特殊的引用类型,因为他们可以调用系统的方法,这种类型就 ...

  2. Java多线程——<七>多线程的异常捕捉

    一.概述 为什么要单独讲多线程的异常捕捉呢?先看个例子: public class ThreadException implements Runnable{ @Override public void ...

  3. Android系统Recovery工作原理

    Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作 http://blog.csdn.net/mu0206mu/article/d ...

  4. svn 分支与合并的使用

      在使用svn的时候我们往往有这样的需求.我们修改某些代码,因为对某项技术不是非常的熟悉,担心自己当前的修改(或者叫测试)会影响到服务器中版本库代码的崩溃等.传统做法我们会手动复制一份代码,然后修改 ...

  5. fiddler 新发现

    就一句话,记录一下 urlreplace baidu.com taobao.com //Fiddler2\Scripts\SampleRules.js 这里发现的 case "urlrepl ...

  6. jquery 插件开发及extend

    以下信息是在看了IBM上的一篇文章(使用 jQuery(中级),第 2 部分: 创建自己的插件)http://www.ibm.com/developerworks/cn/web/wa-aj-jquer ...

  7. __dict__和__slots__

    __dict__: __slots__:

  8. Webpack教程二

    Webpack教程一 开发技巧 启用source-map 现在的代码是合并以后的代码,不利于排错和定位,只需要在config中添加 ... devtool: 'eval-source-map', .. ...

  9. IOS快速集成下拉上拉刷新

    http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5% ...

  10. SSH开发实践part1:Spring与Hibernate整合

    1 之前把SSH看完了,现在从头开始进行项目实践.现在讲整个过程中的点滴记录下来,希望对后来者有参考. 2 SSH是一个轻量级的java开发框架,struts负责MVC开发模式中的controller ...