hdu - 1180 诡异的楼梯 (bfs+优先队列)
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+优先队列)的更多相关文章
- hdu 1180 诡异的楼梯 (bfs)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Sub ...
- hdu 1180诡异的楼梯(bfs)
诡异的楼梯 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submis ...
- hdu 1180 诡异的楼梯(优先队列)
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向 ...
- HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...
- HDU 1180 诡异的楼梯(BFS)
诡异的楼梯 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu 1180 诡异的楼梯
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在 ...
随机推荐
- POJ2676-Sudoku(数独)
想了好久没想到好的解决办法,参考了 http://user.qzone.qq.com/289065406/blog/1303713313 大致题意: 九宫格问题,也有人叫数独问题 把一个9行9列的网格 ...
- 【BZOJ】【3238】【AHOI2013】diff(差异)
题目链接:www.lydsy.com/JudgeOnline/problem.php?id=3238 后缀数组 这题题面给的暗示性就很强啊……一看就是要用后缀xx一家的算法,由于本蒻只会后缀数组所以就 ...
- mysql导出导入某张表
一般表数据少的话都用图形界面了,看着比较方便. 如果表中数据比较多,用图形界面极容易卡死,这个时候就要用到命令行了. 用命令行导出导入大量数据还是比较快的,方法如下: 导出库db1中的表table1: ...
- 引擎设计跟踪(九.14.2g) 将GNUMake集成到Visual Studio
最近在做纹理压缩工具, 以及数据包的生成. shader编译已经在vs工程里面了, 使用custom build tool, build命令是调用BladeShaderComplier, 并且每个文件 ...
- STMPClient 发送邮件显示 不允许使用邮件名称.
在.net 2.0,3.5, 针对某些邮箱(还不清楚是什么样的邮件) , 使用微软自带的DLL发送邮件会提示不允许使用邮件名称 .... 使用Jmail可以发送. 解决方案: 1. ...
- jetty启动报错Unsupported major.minor version 51.0
主要是JDK版本的问题,需要将Eclipse的Jdk版本设置为1.7的才可以,编译级别也设置为1.7,然后删除maven项目路径,D:\WORK\workspace\xxx\target下的所有文件, ...
- eclipse 自动 注释
在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...
- NGUI 实现 透明底图遮罩 && 人物像素变黑
今天 UI 那边要求实现一个 透明底图遮罩 与 变黑 的效果. 刚开始考虑使用 shader 实现一个 网上搜了一下,发现了这个,但是底图需要不透明才行,不然他会把 底图的不遮罩部分的透明部分 进行颜 ...
- ArrayList和Iterator的用法
import java.util.ArrayList; import java.util.Iterator; public class ArrayListTest { public static vo ...
- cf div2 236 D
D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...