码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5040Instrusive

时间:2015-02-23 23:41:37      阅读:439      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定一个地图,要从‘M‘点到‘T‘点,每次可以往四个方向移动,平时每次移动1格花费1秒。但是由于地图上有一些监控,如果当前所在格被监控看到,就必须躲在纸箱里,躲在纸箱里移动一格的耗时是3秒。而监控的可视范围是它本身所在的一格,以及它朝向的相邻一格。监控每秒会顺时针旋转90度。地图上还有一些‘#‘标记表示不可以进入的。可以在原地停留1秒。

时间卡得有点紧,单纯dp过不了,要用优先队列优化下

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1175    Accepted Submission(s): 353


Problem Description
The legendary mercenary Solid Matt gets a classic mission: infiltrate a military base.

The military base can be seen as an N * N grid. Matt‘s target is in one of the grids and Matt is now in another grid.

In normal case, Matt can move from a grid to one of the four neighbor grids in a second. But this mission is not easy.

Around the military base there are fences, Matt can‘t get out of the base.

There are some grids filled with obstacles and Matt can‘t move into these grids.

There are also some surveillance cameras in the grids. Every camera is facing one of the four direction at first, but for every second, they will rotate 90 degree clockwisely. Every camera‘s sight range is 2, which means that if Matt is in the same grid as the camera, or in the grid that the camera is facing, he will be seen immediately and the mission will fail.

Matt has a special equipment to sneak: a cardbox. Matt can hide himself in the card box and move without being noticed. But In this situation, Matt will have to use 3 seconds to move 1 grid. Matt can also just hide in the cardbox without moving. The time to hide and the time to get out of the cardbox can be ignored.

Matt can‘t take the risk of being noticed, so he can‘t move without cardbox into a grid which is now insight of cameras or from a grid which is now insight of cameras. What‘s more, Matt may be in the cardbox at the beginning.

As a live legend, Matt wants to complete the mission in the shortest time.
 

Input
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

For each test cases, the first line contains one integer:N(1<=N<=500)

In the following N lines, each line contains N characters, indicating the grids.

There will be the following characters:

● ‘.‘ for empty 
● ‘#‘ for obstacle 
● ‘N‘ for camera facing north 
● ‘W‘ for camera facing west 
● ‘S‘ for camera facing south 
● ‘E‘ for camera facing east 
● ‘T‘ for target 
● ‘M‘ for Matt
 

Output
For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.

If Matt cannot complete the mission, output ‘-1‘.
 

Sample Input
2 3 M.. .N. ..T 3 M.. ### ..T
 

Sample Output
Case #1: 5 Case #2: -1
 

Source
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
bool vis[510][510][4];
char pic[510][510];
int tox[4]={-1,1,0,0};
int toy[4]={0,0,-1,1};
int n;
bool light[510][510][4];
struct node
{
	int x,y,t;
	node(){}
	node(int x,int y,int t)
	{
		this->x=x;
		this->y=y;
		this->t=t;
	}
	bool operator <(node one)const
	{
		return t>one.t;
	}
};
int bfs(int x,int y)
{
	priority_queue<node>qq;
	qq.push(node(x,y,0));
	memset(vis,0,sizeof(vis));
	while(qq.size())
	{
		node from=qq.top();
		qq.pop();
		int x=from.x,y=from.y,t=from.t;
		if(vis[x][y][t%4])
			continue;
		vis[x][y][t%4]=1;
		if(pic[x][y]=='T')
			return t;
		if(!vis[x][y][(t+1)%4])
			qq.push(node(x,y,t+1));
		for(int i=0;i<4;i++)
		{
			int nx=x+tox[i],ny=y+toy[i];
			if(nx>=0&&nx<n&&ny>=0&&ny<n)
			{
				if(pic[nx][ny]!='#')
				{
					if(light[x][y][t%4]||light[nx][ny][t%4])
					{
						int nt=t+3;
						if(!vis[nx][ny][nt%4])
							qq.push(node(nx,ny,nt));
					}
					else
					{
						int nt=t+1;
						if(!vis[nx][ny][nt%4])
							qq.push(node(nx,ny,nt));
					}
				}
			}
		}
	}
	return -1;
}
int work()
{
	int sx,sy;
	memset(light,0,sizeof(light));
	for(int t=0;t<4;t++)
		for(int x=0;x<n;x++)
			for(int y=0;y<n;y++)
			{
				if(pic[x][y]=='M')
				{
					sx=x;
					sy=y;
				}
				if(t>0)
				{
					if(pic[x][y]=='N')
						pic[x][y]='E';
					else if(pic[x][y]=='W')
						pic[x][y]='N';
					else if(pic[x][y]=='E')
						pic[x][y]='S';
					else if(pic[x][y]=='S')
						pic[x][y]='W';
				}
				if(pic[x][y]=='N')
				{
					light[x][y][t]=1;
					if(x-1>=0)
						light[x-1][y][t]=1;
				}
				else if(pic[x][y]=='S')
				{
					light[x][y][t]=1;
					if(x+1>=0)
						light[x+1][y][t]=1;
				}
				else if(pic[x][y]=='E')
				{
					light[x][y][t]=1;
					if(y+1<n)
						light[x][y+1][t]=1;
				}
				else if(pic[x][y]=='W')
				{
					light[x][y][t]=1;
					if(y-1>=0)
						light[x][y-1][t]=1;
				}
			}
	return bfs(sx,sy);
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int cs=1;cs<=T;cs++)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%s",pic[i]);
		printf("Case #%d: %d\n",cs,work());
	}
}

HDU5040Instrusive

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/43919761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!