HDU 1180 诡异楼梯
2015-10-24 13:26
316 查看
诡异的楼梯
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 11140 Accepted Submission(s): 2781
[/b]
[align=left]Problem Description[/align]
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
[align=left]Input[/align]
测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
[align=left]Output[/align]
只有一行,包含一个数T,表示到达目标的最短时间.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
[align=left]Sample Input[/align]
5 5 **..T **.*. ..|.. .*.*. S....
[align=left]Sample Output[/align]
7
Hint
Hint
地图如下:
//刚开始又理解错了题意,真特么佩服自己。。。老是傻逼。。。 //优先队列加bfs,用的时间为奇数时,翻转楼梯方向,楼梯方向与前进方向不一致时,等待一分钟,所以加一。另外楼梯不要标记,可重复经过 //总结:首先要理解题意!!! #include <iostream> #include <cstdio> #include <cstring> #include <cctype> #include <set> #include <queue> #include <vector> using namespace std; char s[30][30]; int m, n, k; int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; bool use[30][30]; struct node { int x, y, cnt; bool operator< (const node a) const { return cnt > a.cnt; } } e, p; struct mm { int x, y; } tmp[500]; void change(int f) //奇数时改变楼梯方向 { if(f & 1) { for(int i = 0; i < k; i++) if(s[tmp[i].x][tmp[i].y] == '|') s[tmp[i].x][tmp[i].y] = '-'; else s[tmp[i].x][tmp[i].y] = '|'; } } void bfs(int x, int y) { priority_queue <node> que; e.x = x, e.y = y, e.cnt = 0; que.push(e); use[x][y] = true; while(! que.empty()) { p = que.top(); que.pop(); if(s[p.x][p.y] == 'T') { printf("%d\n", p.cnt); break; } change(p.cnt); for(int i = 0; i < 4; i++) { e.x = p.x + dx[i], e.y = p.y + dy[i]; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) { e.cnt = p.cnt + 1; if(s[e.x][e.y] == '|') { if(i == 0) { e.x = e.x - 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, que.push(e); } else if(i == 1) { e.y = e.y + 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e); } else if(i == 2) { e.x = e.x + 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, que.push(e); } else { e.y = e.y - 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e); } } else if(s[e.x][e.y] == '-') { if(i == 0) { e.x = e.x - 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e); } else if(i == 1) { e.y = e.y + 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, que.push(e); } else if(i == 2) { e.x = e.x + 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e); } else { e.y = e.y - 1; if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false) use[e.x][e.y] = true, que.push(e); } } else { use[e.x][e.y] = true; que.push(e); } } } change(p.cnt); } } int main() { while(~ scanf("%d%d", &m, &n)) { int x, y; memset(use, false, sizeof use); k = 0; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) { scanf(" %c", &s[i][j]); if(s[i][j] == 'S') x = i, y = j; if(s[i][j] == '-' || s[i][j] == '|') tmp[k].x = i, tmp[k].y = j, k++; } bfs(x, y); } return 0; }
相关文章推荐
- 前端Javascript书籍分享
- linux中文手册
- 20135231 —— 第六周任务总结报告
- 第八周 项目二-建立链串算法库
- 读《大道至简》第四章有感
- 简单工厂模式
- MongoDB 学习笔记三 JAVA调用MongoDB
- Hibernate-映射关系- 基于主键映射的1<->1映射
- struts.valueStack (Action 中属性存放的原理)
- 如何在苹果官网下载旧版本的Xcode
- C语言:一组数据中只有一个数字出现了一次。其他所有数字都是成对出现的。请找出这个数字。(
- UVa-10006 Carmichael Numbers
- 在thinkphp里面执行原生的sql语句
- hdu5505
- poj 2503 hash
- @import libxmlSimu not fund的解决方法
- oracle 第23章 RMAN 备份恢复
- Ignatius and the Princess IV---hdu1029(动态规划或者sort)
- qt中的拖拽及其使用技巧
- iOS ARC 内存管理要点