【HDOJ】1180 诡异的楼梯
bfs+优先队列。wa了N次,才发现可以停留等待楼梯变换方向。
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; #define MAXNUM 55 typedef struct node_st {
int x, y, t;
node_st() {}
node_st(int xx, int yy, int tt) {
x = xx; y = yy; t = tt;
}
friend bool operator < (node_st a, node_st b) {
return a.t > b.t;
}
} node_st; char map[MAXNUM][MAXNUM];
char visit[MAXNUM][MAXNUM];
int n, m;
int begx, begy, endx, endy;
int direct[][] = {{,},{-,},{,},{,-}}; int bfs(int begx, int begy) {
priority_queue<node_st> que;
node_st node;
int i, x, y, nx, ny, t; que.push(node_st(begx, begy, ));
memset(visit, , sizeof(visit));
visit[begx][begy] = ; while ( !que.empty() ) {
node = que.top();
if (map[node.x][node.y] == 'T') {
return node.t;
}
que.pop();
for (i=; i<; ++i) {
x = node.x + direct[i][];
y = node.y + direct[i][];
if (x< || x>=n || y< || y>=m)
continue;
if (visit[x][y] || map[x][y] == '*')
continue;
if (map[x][y] == '.' || map[x][y]=='T') {
que.push(node_st(x,y,node.t+));
visit[x][y] = ;
continue;
}
if (map[x][y]=='|' || map[x][y]=='-') {
nx = x + direct[i][];
ny = y + direct[i][];
t = node.t + ;
if (nx< || nx>=n || ny< || ny>=m)
continue;
if (visit[nx][ny] || map[nx][ny]=='*')
continue;
if ( ((map[x][y]=='|') && ((node.t&)==) && (i==||i==)) ||
((map[x][y]=='|') && ((node.t&)!=) && (i==||i==)) ||
((map[x][y]=='-') && ((node.t&)==) && (i==||i==)) ||
((map[x][y]=='-') && ((node.t&)!=) && (i==||i==)) )
++t;
visit[nx][ny] = ;
que.push(node_st(nx, ny, t));
}
}
} return -;
} int main() {
int i, j; while (scanf("%d %d%*c",&n,&m) != EOF) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == 'S') {
begx = i;
begy = j;
}
}
}
i = bfs(begx, begy);
printf("%d\n", i);
} return ;
}
【HDOJ】1180 诡异的楼梯的更多相关文章
- HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1180 诡异的楼梯
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu 1180 诡异的楼梯 (bfs)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Sub ...
- HDU 1180 诡异的楼梯(BFS)
诡异的楼梯 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 1180诡异的楼梯(bfs)
诡异的楼梯 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submis ...
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...
- HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- C#微信公众号开发 -- (一)开发之前的准备
本系列文章讲述的是利用C#语言开发微信公众号的实例教程,主要是服务号的开发(因为订阅号不能获取微信开发的高级接口) 想要开发微信服务公众号,首先必须要有一个认证的微信服务号,这样才能够使用微信提供的所 ...
- job还是job
declare jobno binary_integer;rm_days number;rm_hour number; --传入的hourmy_hour number; --取出当前时间的ho ...
- 某Python群的入群题目
为了确保不被通过搜索引擎直接搜索题目搜出来,我重新描述下题目: 给n, 求1~n的每个数的约数和 每个约数出现的个数是 n // i个, 出现x次的约数范围是[n // (i + 1) + 1, n ...
- ubuntu 安装phpmyadmin
参考文章: http://www.111cn.net/database/mysql/43957.htm access错误的解决方法 1 首先下载 去官网下载phpmyadmin 2 然后直接 ...
- 初用jquery
---恢复内容开始--- 这两天在顶顶大人的指导下,利用jquery框架做了一个动态切换的小页面.最终效果图: 这么萌萌哒的图片让我觉得一直在测试也没那么累.实现功能如下: 1.打开页面时,自动切换, ...
- 负载均衡-多台机子session不起效:把php.ini中file改为memcache存储
一 开启memcache服务 二 修改php.ini中session配置 php/lib/php.ini session.save_handler = memcache session.save_pa ...
- CentOS 6.4 使用第三方源
1.EPEL源 CentOS 6.x 32-bit (x86/i386):rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/ep ...
- php最短的HTTP响应代码
刚刚发现在CodeProject给我推送了一篇文章叫:the Shortest PHP code for Returning HTTP Response Code 翻译过来就是(PHP最短的HTTP ...
- warning: no newline at end of file
编译错误:warning: no newline at end of file原因:程序结尾需要有一个空行解决办法:在程序末尾多打个回车就行了
- PHP练习题(一)
程序1 .题目: 企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10% : 利润高于10 万元, 低于20 万元时, 低于10万元的部分按10% 提成,高于 10万元的部分,可提 ...