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. 快速幂取模 分类: ACM TYPE 2014-08-29 22:01 95人阅读 评论(0) 收藏

    #include<stdio.h> #include<stdlib.h> //快速幂算法,数论二分 long long powermod(int a,int b, int c) ...

  2. 笔直的水管 usaco 背包

    背包dp入门,需要滚动数组: #include<iostream> #include<cstdio> #include<string> #include<cs ...

  3. GameMap(类结构)(不断跟新)

    暂时有个疑问为什么这些需要这么复杂的继承

  4. Maven安装和配置,eclipse创建Maven项目

    提示:使用Maven需要先安装jdk. 下载安装Maven 一.下载最新版的Maven,下载地址:http://maven.apache.org/download.cgi 二.将Maven下载到E:\ ...

  5. WPF编程学习——样式

    本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的 ...

  6. Java 延时常见的几种方法

    1. 用Thread就不会iu无法终止 new Thread(new Runnable() { public void run() { while (true) { test(); try { Thr ...

  7. ZOJ3762 The Bonus Salary!(最小费用最大流)

    题意:给你N个的任务一定要在每天的[Li,Ri]时段完成,然后你只有K天的时间,每个任务有个val,然后求K天里能够获得的最大bonus. 思路:拿到手第一直觉是最小费用最大流,然后不会建图,就跑去想 ...

  8. ios 团购分类页面(9宫格)

    =-= 命名有些错误,但功能实现,以后注意下命名规范 WJViewGroup.h #import <UIKit/UIKit.h> @interface WJViewGroup : UIVi ...

  9. 一、Android NDK编程预备之Java jni简介

    转自:  http://www.eoeandroid.com/thread-264384-1-1.html 游戏开发 视频教程 博客 淘帖     论坛›eoe·Android应用开发区›Androi ...

  10. 【转载】 硬盘主引导记录(MBR)及其结构详解

    硬盘的0柱面.0磁头.1扇区称为主引导扇区,FDISK程序写到该扇区的内容称为主引导记录(MBR).该记录占用512个字节,它用于硬盘启动时将系统控制权交给用户指定的,并在分区表中登记了的某个操作系统 ...