hdu 1180 诡异的楼梯
2016-11-26 11:44
411 查看
http://acm.split.hdu.edu.cn/showproblem.php?pid=1180
Total Submission(s): 13536 Accepted Submission(s): 3419
[/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
HintHint
地图如下:
诡异的楼梯
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 13536 Accepted Submission(s): 3419
[/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
HintHint
地图如下:
#include<cstdio> #include<cstdlib> #include<math.h> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<set> #include<vector> #include<queue> #include<map> #define ms(x) memset( (x),0,sizeof(x) ); using namespace std; typedef long long int ll; int n,m; char v[30][30]; int vis[30][30]; int ret; struct node{ int x,y,step; node(int a,int b,int c){ x=a,y=b,step=c; } }; queue<node> q; int check(int x,int y){ if( x < 1 || y < 1 ) return 1; if( x > n || y > m ) return 1; if( v[x][y] == '*' ) return 1; return 0; } int k[4][2]={ {0,1},{1,0},{0,-1},{-1,0}}; void bfs(int x,int y){ while( !q.empty() ) q.pop(); vis[x][y] = 0; node t( x,y,0 ); v[x][y]='.'; q.push(t); while( !q.empty() ){ t = q.front();q.pop(); if(check(t.x,t.y)) continue; if( v[t.x][t.y] == 'T' ) ret = min(ret,vis[t.x][t.y] ); for(int tk = 0;tk < 4;tk++){ int tx=t.x+k[tk][0],ty=t.y+k[tk][1]; if(check(tx,ty)) continue; if( v[tx][ty] == '.' || v[tx][ty] == 'T') { if( vis[tx][ty] > t.step+1 ) { vis[tx][ty]=t.step+1;q.push( node(tx,ty,t.step+1) );continue;} continue;//别漏了 } char ch = v[tx][ty] ; if( t.step%2 == 1 ){ if( ch == '-' ) ch = '|'; else ch = '-'; } if( ch == '-' ){ if( k[tk][0] == 0 ){ if( vis[tx][ty+k[tk][1]] > t.step+1 ) { vis[tx][ty+k[tk][1]]=t.step+1;q.push( node(tx,ty+k[tk][1],t.step+1) );continue; } } else { if( vis[tx+k[tk][0]][ty] > t.step+2 ) { vis[tx+k[tk][0]][ty]=t.step+2;q.push( node(tx+k[tk][0],ty,t.step+2) );continue; } } } else if( ch == '|' ){ if( k[tk][1] == 0 ){ if( vis[tx+k[tk][0]][ty] > t.step+1 ) { vis[tx+k[tk][0]][ty]=t.step+1;q.push( node(tx+k[tk][0],ty,t.step+1) );continue; } } else{ if( vis[tx][ty+k[tk][1]] > t.step+2 ) { vis[tx][ty+k[tk][1]]=t.step+2;q.push( node(tx,ty+k[tk][1],t.step+2) );continue; } } } } } } int main() { // freopen("E:\\workspace\\acm---C++\\acm\\1.txt","r",stdin); while(scanf("%d%d",&n,&m) != EOF){ if( n==0 || m==0 ){ puts("0");continue; } ret = 0x7fffffff; memset( vis,0x7f,sizeof(vis) ); ms(v); for(int i = 1;i <= n;i++) scanf("%s",v[i]+1); for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++ ){ if( v[i][j] == 'S' ){ vis[i][j] = 0; bfs(i,j); j = m+1,i=n+1; } } } printf("%d\n",ret); } return 0; } /* 附上几组测试数据 5 5 **..T **.*. **|.. **.** S..** 5 5 **..T **.*. **-.. **.** S..** 5 5 .|.-T -*-*| .*.|. -*-** S|.** 5 5 S.... -|-|- ..... -|-|- ....T 1 3 S-T 1 3 S|T 1 5 S|.|T 1 5 S-.-T 1 5 S|.-T 1 5 S-.|T 答案是: 8 7 7 8 1 2 4 3 3 2 */
相关文章推荐
- HDU 1180 诡异的楼梯 (BFS)
- hdu1180诡异的楼梯【优先队列+广搜】
- HDU 1180 诡异的楼梯(BFS+保存自己的状态)
- hdu 1180 诡异的楼梯 (bfs)
- hdu 1180 诡异的楼梯
- 诡异的楼梯 hdu 1180
- hdu 1180 诡异的楼梯 (bfs)
- 【HDU 1180】诡异的楼梯(BFS)
- HDU 1180 诡异的楼梯(BFS+奇偶步数判断)
- HDU 1180 诡异的楼梯(BFS)
- Hdu-1180(诡异的楼梯)
- hdu 1180 诡异的楼梯
- HDU 1180 诡异的楼梯
- HDU-1180 诡异的楼梯
- hdu 1180诡异的楼梯
- HDU1180 诡异的楼梯 广搜 优先队列
- 【Bfs】HDU 1180 诡异的楼梯
- HDU1180 诡异的楼梯[bfs]
- HDU 1180 诡异的楼梯(BFS:时间动态图)
- HDU 1180 诡异的楼梯【广搜】