hdu 1180 诡异的楼梯

http://acm.split.hdu.edu.cn/showproblem.php?pid=1180


诡异的楼梯

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 13536    Accepted Submission(s): 3419


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
地图如下:
 



#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

*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值