hdu1180

诡异的楼梯

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


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

Source

Gardon-DYGG Contest 1


上来看上去不难  但是。。好几次看错题   

首先 有几个注意点   

梯子是动态的   时间是偶数时为原来状态   奇数时改变

从梯子的一面走到梯子的另一面 只需要单位1时间

走的过程中梯子不动  

根据这些信息 我们就能推出来  当 时间为奇数时并且你的下一步时梯子  此时你走的方向和梯子的方向相同时不需要加时间

也就是 算上走上梯子的时间为偶数时 是不需要额外加时间的 

相反  奇数时则意味着  你要等待梯子转动正确的方向到你的面前 所有此时时间需要额外加1

#include<bits/stdc++.h>
using namespace std;
const int maxn = 22;
struct node{
	int x, y;
	int step;
	node(){
	}
	node(int xx,int yy,int sstep){
		x = xx;
		y = yy;
		step = sstep;
	}
	bool operator < (const node & a) const
	{
		return a.step < step;
	}
};
int n, m;
int visit[maxn][maxn];
char mp[maxn][maxn];
int sx, sy, ex, ey;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
bool check(node a)
{
	if(a.x < 1 || a.y < 1 || a.x > m || a.y > n || visit[a.x][a.y] || mp[a.x][a.y] == '*')
	return false;
	
	return true;
}
int bfs()
{
	char c;
	priority_queue<node> q;
	node start(sx,sy,0);
	q.push(start);
	visit[sx][sy] = 1;
	
	while(!q.empty())
	{
		node tmp1 = q.top();
		q.pop();
		for(int i = 0;i < 4;i ++)
		{
			node tmp2 = tmp1;
			tmp2.x += dir[i][0];
			tmp2.y += dir[i][1];
			tmp2.step ++;
			if(check(tmp2) && (mp[tmp2.x][tmp2.y] == '|' || (mp[tmp2.x][tmp2.y] == '-' )))
			{
				if(tmp2.step % 2 == 1)
				{
					if(mp[tmp2.x][tmp2.y] == '|') c = '-';
					else if(mp[tmp2.x][tmp2.y] == '-') c = '|';
				}
				else
				{
					c = mp[tmp2.x][tmp2.y];
				}
				tmp2.x += dir[i][0];  
                tmp2.y += dir[i][1];  
                if((c == '-' && (dir[i][1] == -1 || dir[i][1] == 1)) || (c == '|' && (dir[i][0] == -1 || dir[i][0] == 1)))  
                {  
                    tmp2.step += 1;  
                }  
			}
			if(check(tmp2))
			{
				if(tmp2.x == ex && tmp2.y == ey) return tmp2.step;
				
				visit[tmp2.x][tmp2.y] = 1;
				q.push(tmp2);
			}
		}
	}
}

int main()
{
	while(cin >> m >> n)
	{
		memset(visit,0,sizeof(visit));
		for(int i = 1;i <= m;i ++)
		{
			for(int j = 1;j <= n;j ++)
			{
				cin >> mp[i][j];
				
				if(mp[i][j] == 'S')
				{
					sx = i;
					sy = j;
				}
				if(mp[i][j] == 'T')
				{
					ex = i;
					ey = j;
				}
			}
		}
		
		cout << bfs() << endl;
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/soul_97/article/details/80201368