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里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在 ...
随机推荐
- SqlServer 临时表、表变量、函数 替代游标
http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html 临时表 存放在tempdb中 --存储过程中将多表连接结果写入到临时表中, ...
- dblink应用
当我们要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中就必须要创建远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据. 用法例子: (1) ...
- BLUR
ssao的blur遇到个麻烦 花了两三天时间...终于大概知道原因了. 在nvidia的ssao(http://developer.download.nvidia.com/SDK/10.5/direc ...
- C#中Config文件中,特殊符号的书写方法。
App.config: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration& ...
- 单片机模拟 1/2 Bias、1/4 Duty的 LCD 驱动使用方法
工作原理 方式一 根据 LCD 的驱动原理可知,LCD 像素点上只能加上 AC 电压,LCD 显示器的对比度由 COM脚上的电压值减去 SEG 脚上的电压值决定,当这个电压差大于 LCD 的饱 ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
- 对MySQL DELETE语法的详细解析
以下的文章主要描述的是MySQL DELETE语法的详细解析,首先我们是从单表语法与多表语法的示例开始的,假如你对MySQL DELETE语法的相关内容十分感兴趣的话,你就可以浏览以下的文章对其有个更 ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- (.iso)光盘镜像文件的打开与安装
直接解压就可以打开,然后就可以安装.exe文件