Hdu-1180(诡异的楼梯)

C - 诡异的楼梯
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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



TLE了...不知道是怎么回事,晕死了...


#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

char map[33][33];

int vis[33][33];

int N, M;

int start_x, start_y;

int end_x, end_y;

int dir_x[4] = {0, 0, 1, -1};

int dir_y[4] = {1, -1, 0, 0};

struct Node
{
	int x;
	int y;
	int step;	
	friend bool operator < (Node a, Node b)
	{
		return a.step > b.step;
	}
};

priority_queue <Node> Q;

void init()
{
	memset(vis, 0, sizeof(vis));
	while (!Q.empty())
	{
		Q.pop();
	}
}

bool judge(int x, int y)
{
	if (x < 0 || x >= N || y < 0 || y >= N || map[x][y] == '*')
	{
		return false;
	}
	return true;
}

int BFS()
{
	Node start;
	start.x = start_x;
	start.y = start_y;
	start.step = 0;
	Q.push(start);
	while (!Q.empty())
	{
		Node temp;
		temp = Q.top();
		Q.pop();
		vis[temp.x][temp.y] = 1;
		if (temp.x == end_x && temp.y == end_y)
		{
			return temp.step;
		}
		for (int i = 0; i < 4; i++)
		{
			int x = temp.x + dir_x[i];
			int y = temp.y + dir_y[i];
			int step = temp.step;
			if (!vis[x][y] && map[x][y] == '-' && dir_x[i] == 0)
			{
				Node node;
				node.x = x;
				node.y = y;
				if (step % 2 != 0)
				{
					step++;
				}
				node.step = step;
				Q.push(node);
				continue;
			}
			if (!vis[x][y] && map[x][y] == '|' && dir_x[i] == 0)
			{
				Node node;
				node.x = x;
				node.y = y;
				if (step % 2 == 0)
				{
					step++;
				}
				node.step = step;
				Q.push(node);
				continue;
			}
			if (!vis[x][y] && map[x][y] == '|' && dir_y[i] == 0)
			{
				Node node;
				node.x = x;
				node.y = y;
				if (step % 2 != 0)
				{
					step++;
				}
				node.step = step;
				Q.push(node);
				continue;
			}
			if (!vis[x][y] && map[x][y] == '-' && dir_y[i] == 0)
			{
				Node node;
				node.x = x;
				node.y = y;
				if (step % 2 == 0)
				{
					step++;
				}
				node.step = step;
				Q.push(node);
				continue;
			}
			if (judge(x, y) && !vis[x][y])
			{
				Node node;
				node.x = x;
				node.y = y;
				node.step = step + 1;
				Q.push(node);
			}
		}
	}
	return -1;
	
}


int main()
{
	while (scanf("%d%d", &N, &M) != EOF)
	{
		init();
		for (int i = 0; i < N; i++)
		{
			scanf("%s", map[i]);
			for (int j = 0; j < M; j++)
			{
				if (map[i][j] == 'S')
				{
					start_x = i;
					start_y = j;
					break;
				}
				if (map[i][j] == 'T')
				{
					end_x = i;
					end_y = j;
					break;
				}
			}
		}
		int ans = BFS();
		printf("%d\n", ans);
	}
//	system("pause");
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值