诡异的楼梯

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 17892 Accepted Submission(s): 4652

Problem Description

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

比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.

Input

测试数据有多组,每组的表述如下:

第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.

Output

只有一行,包含一个数T,表示到达目标的最短时间.

注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.

Sample Input

5 5

**..T

**..

..|..

.
.*.

S....

Sample Output

7

Hint

Hint

地图如下:

Source

Gardon-DYGG Contest 1

【分析】:

解题思路:看到这种类似走迷宫的题第一反应就是搜索,然而这题不只是一般的搜索那么简单,原因就是那个楼梯,走到这个楼梯的时间和位置恰当的话可以让你获得“加速”,因此利用好这个楼梯很关键。注意的是楼梯方向会变,因此走到楼梯边上时要特判一下。

楼梯的方向要与前进的方向一致,这就牵扯到时机问题——如果恰好可以上楼梯(比如楼梯这时候的状态是竖直的,而你恰好在它下方的格子需要往上走),这时候就可以利用这个楼梯(相当于是跳了一步)。但如果状态不合适,那你无法使用这个楼梯,只能选择继续向别的方向走或者原地等待(相当于无法利用这个楼梯获得“加速”)

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
using namespace std; typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e5 + 100;
const int maxm = 100;
const double PI = acos(-1.0);
const double eps = 1e-8;
//const int dx[] = {-1,1,0,0,1,1,-1,-1};
//const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
// 上/右/下/左
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
int n,m;
char a[maxm][maxm];
int vis[maxm][maxm];
int sx,sy,ex,ey;
struct node
{
int x,y,s;
};
bool check(int x,int y)
{
return x>=0 && y>=0 && x<n && y<m && a[x][y]!='*' && !vis[x][y];
} void bfs(int sx,int sy)
{
queue<node> q;
node tmp,nxt;
tmp.x=sx, tmp.y=sy, tmp.s=0;
vis[sx][sy]=1;
q.push(tmp);
while(!q.empty())
{
tmp = q.front();
q.pop();
if(a[tmp.x][tmp.y]=='T')
{
printf("%d\n",tmp.s);
return ;
}
for(int i=0;i<4;i++)//上/右/下/左
{
nxt.x = tmp.x + dx[i];
nxt.y = tmp.y + dy[i];
nxt.s = tmp.s + 1;
// printf("i=%d tmp.x=%d tmp.y=%d tmp.s=%d\n",i,tmp.x,tmp.y,tmp.s);
//
// printf("i=%d nxt.x=%d nxt.y=%d nxt.s=%d\n",i,nxt.x,nxt.y,nxt.s);
// printf("-----------------------------------\n");
if(check(nxt.x,nxt.y))
{
if(a[nxt.x][nxt.y]=='|')
{
nxt.x += dx[i];
nxt.y += dy[i];
// printf("| i=%d tmp.x=%d tmp.y=%d tmp.s=%d\n",i,tmp.x,tmp.y,tmp.s);
//
// printf("|> i=%d nxt.x=%d nxt.y=%d nxt.s=%d\n",i,nxt.x,nxt.y,nxt.s);
// printf("-----------------------------------\n");
if(check(nxt.x,nxt.y))//判断到达楼梯时楼梯的方向和人前进的方向是否一致
{
//横-左右方向||竖-上下方向
//不顺路,原地不动,等待下次开门(如果楼梯正好反向了 时间需要加1)
if((tmp.s%2==0 && (i==1||i==3)) || (tmp.s%2==1 && (i==0||i==2)))
nxt.s+=1; //只需要判断当前的坐标与上一步坐标之间的关系,即可知道当前的方向与之前方向之间的关系即可
}
else continue;
}
else if(a[nxt.x][nxt.y]=='-')
{
nxt.x += dx[i];
nxt.y += dy[i];
// printf("- i=%d tmp.x=%d tmp.y=%d tmp.s=%d\n",i,tmp.x,tmp.y,tmp.s);
//
// printf("- i=%d nxt.x=%d nxt.y=%d nxt.s=%d\n",i,nxt.x,nxt.y,nxt.s);
// printf("-----------------------------------\n");
if(check(nxt.x,nxt.y))//通过当前步数的奇偶性去判断当前楼梯的方向以及是否可以通行
{
if((tmp.s%2==0&&(i==0||i==2))||(tmp.s%2==1&&(i==1||i==3)))
nxt.s+=1;
}
else continue;
}
vis[nxt.x][nxt.y] = 1;
q.push(nxt);
}
}
}
} int main()
{ while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
getchar();
for(int i=0;i<n;i++)
scanf("%s",a[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='S')
{
sx=i;
sy=j;
}
}
} bfs(sx,sy);
} }
/*
5 5
**..T
**.*.
..|..
.*.*.
S....
*/

[优先队列]

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
const int maxn = 21;
int stx,sty,gx,gy;
char map[maxn][maxn];
bool vis[maxn][maxn];
int statue[maxn][maxn];
int dx[4]={0,0,1,-1};
int dy[4]={-1,1,0,0};
struct node{
int x,y,step;
/* friend bool operator <(node a,node b){
return a.step>b.step;
} */
bool operator <(const node &a)const{
return step>a.step;//步数小的先出队列
}
}init;
bool judge(node no){
//如果超出边界,或者该位置是墙,或者当前步数大于之前步数,返回false
if(no.x<0||no.x>=n||no.y<0||no.y>=m||map[no.x][no.y]=='*'||(statue[no.x][no.y]&&no.step>=statue[no.x][no.y]))
return false;
return true;
}
int bfs(){
init.x=stx;init.y=sty;init.step=0;
statue[init.x][init.y]=1;
priority_queue<node>pq;
while(!pq.empty())
pq.pop();
pq.push(init);
while(!pq.empty()){
node past=pq.top();
node now;
pq.pop();
for(int i=0;i<4;i++){
now.x=past.x+dx[i];
now.y=past.y+dy[i];
now.step=past.step+1;
if(!judge(now)) continue;
if(map[now.x][now.y]=='|'){
if(now.x==past.x&&(past.step & 1)==0) //当横着走,且 | 不变
now.step++;
if(now.y==past.y&&(past.step & 1)==1)//当 竖着走 且 |变
{
now.step++;
}
now.x+=dx[i]; now.y+=dy[i];
}
else if(map[now.x][now.y]=='-'){
if(now.x==past.x&&(past.step & 1)==1) //当横着走,且 -变
now.step++;
if(now.y==past.y&&(past.step & 1)==0)//当竖着走 且-不变
now.step++;
now.x+=dx[i]; now.y+=dy[i];
}
if(!judge(now)) continue;
if(map[now.x][now.y]=='T')
return now.step;
statue[now.x][now.y]=now.step;
pq.push(now);
}
}
return 0;
}
int main(){
while(cin>>n>>m){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>map[i][j];
if(map[i][j]=='S')
stx=i,sty=j;
if(map[i][j]=='T')
gx=i,gy=j;
}
memset(statue,0,sizeof(statue));
int ans=bfs();
cout<<ans<<endl;
}
return 0;
}

HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】的更多相关文章

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

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

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

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

  3. UDF——提取指定线上随时间变化的物理量

    Fluent版本:Fluent 19.0 Visual Studio版本:Visual Studio 2013 有时候我们想要实现一些功能,比如:我们在使用Fluent进行瞬态计算的时候,想要获取某条 ...

  4. hdu - 1180 诡异的楼梯 (bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...

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

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

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

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

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

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

  8. hdu 1180 诡异的楼梯

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

  9. hdu 1180 诡异的楼梯(广搜,简单)

    题目 挺简单的一道广搜题,只要用判断时间是偶数还是奇数就可以判断楼梯的方位,但是我这傻逼居然写了那么久啊那么久,我果然秀逗了,,,, #define _CRT_SECURE_NO_WARNINGS # ...

随机推荐

  1. BZOJ4373 算术天才⑨与等差数列(线段树)

    看上去很难维护,考虑找一些必要条件.首先显然最大值-最小值=k*(r-l).然后区间内的数需要模k同余.最后区间内的数两两不同(k=0除外).冷静一下可以发现这些条件组合起来就是充分的了. 考虑怎么维 ...

  2. [洛谷P2482][SDOI2010]猪国杀

    题目大意:猪国杀,又一道大模拟题 题解:模拟,对于一个没有玩过三国杀的人来说,一堆细节不知道,写的十分吃力 卡点:无数,不想说什么了,这告诉我要多玩游戏 C++ Code: #include < ...

  3. [Leetcode] climbing stairs 爬楼梯

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. 【TMD模拟赛】上低音号 链表

    这道题一看有两个出发现点,一枚举点去找边界,想了一会就Pass了...,二是枚举相框,我们最起码枚举两个边界,然后发现平行边界更好处理,然而仍然只有30分,这个时候就来到了链表的神奇应用,我们枚举上界 ...

  5. jQuery.getJSON跨域访问的正确使用方式(史上最傻瓜式解释)

    最近花了2天时间完整的看了一遍 jQuery 的API,其中 $.getJSON(url[, data][, callback]) 方法的跨域访问解释真心看的一头雾水,大家可以从这里感受一下: htt ...

  6. android脱壳之DexExtractor原理分析

    导语: 上一篇我们分析android脱壳使用对dvmDexFileOpenPartial下断点的原理,使用这种方法脱壳的有2个缺点: 1.  需要动态调试 2.  对抗反调试方案 为了提高工作效率, ...

  7. 2016广东工业大学校赛 E题 GDUT-oj1173

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...

  8. 洛谷P1265 公路修建

    P1265 公路修建 177通过 600提交 题目提供者该用户不存在 标签图论 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 long long类型赋值-1为什么… p党80的进 为什么不过 ...

  9. [bzoj 2733]启发式合并权值线段树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 平衡树待学习.从一个博客学到了合并权值线段树的姿势:http://blog.csdn ...

  10. Spring学习--实现 FactoryBean 接口在 Spring IOC 容器中配置 Bean

    Spring 中有两种类型的 bean , 一种是普通的 bean , 另一种是工厂 bean , 即 FactroyBean. 工厂 bean 跟普通 bean 不同 , 其返回的对象不是指定类的一 ...