HDU 5040

来源:互联网 发布:诺基亚6120c软件 编辑:程序博客网 时间:2024/05/01 18:45

       一道网络赛的搜索题,不过题目貌似又有点没说清。

       这道题目实际上意思是如果同一时刻当前位置和下一个位置没被监控,那么就可以跳过去,而我开始错误的理解成这一时刻的当前位置和下一秒的下一个位置没有被监控就可以跳过去。产生这种偏差主要是由于对每步的时间是用在移动上还是用在格子上等待的理解不同。

      这道题和之前那题差不多,不过时间卡的更紧,一般做法会TLE,所以要用优先队列优化,即把普通队列换成优先队列,从优先队列取出的第一个到达目标的时间就是答案。

代码(C++):

#include <iostream>#include <cstdio>#include <queue>#include <cstring>#define MAX 500#define INF (1<<30)using namespace std;const int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};struct Step{    int x;    int y;    int t;    bool operator< (Step s) const{        return t>s.t;    }};char matrix[MAX][MAX];int n,time[MAX][MAX][4],gi[MAX][MAX][4];void chg_tag(int x,int y,int d){    int i,nx,ny;    gi[x][y][0]=gi[x][y][1]=gi[x][y][2]=gi[x][y][3]=1;    for(i=0;i<4;i++)    {        nx=x+dir[(i+d)%4][0];        ny=y+dir[(i+d)%4][1];        if(nx>=0&&ny>=0&&nx<n&&ny<n) gi[nx][ny][i]=1;    }}int bfs(int mx,int my,int tx,int ty){    int i,j,nx,ny,k;    Step tmp,step;    priority_queue<Step> qs;    time[mx][my][0]=0;    tmp.x=mx;    tmp.y=my;    tmp.t=0;    qs.push(tmp);    while(!qs.empty())    {        step=qs.top();        qs.pop();        if(step.x==tx&&step.y==ty) return step.t;        for(i=0;i<4;i++)        {            nx=step.x+dir[i][0];            ny=step.y+dir[i][1];            if(nx>=0&&ny>=0&&nx<n&&ny<n&&matrix[nx][ny]!='#')            {                tmp.x=nx;                tmp.y=ny;                tmp.t=step.t;                for(j=0;j<3;j++)                {                    k=(step.t+j)%4;                    if(gi[step.x][step.y][k]==0&&gi[nx][ny][k]==0)                    {                        j++;                        break;                    }                }                tmp.t+=j;                if(time[nx][ny][tmp.t%4]==-1||time[nx][ny][tmp.t%4]>tmp.t)                {                    time[nx][ny][tmp.t%4]=tmp.t;                    qs.push(tmp);                }            }        }    }    return -1;}int main(){    //freopen("in.txt","r",stdin);    int t,ca,i,j,mx,my,tx,ty,ans;    scanf("%d",&t);    for(ca=1;ca<=t;ca++)    {       scanf("%d",&n);       memset(gi,0,sizeof(gi));       memset(time,-1,sizeof(time));       for(i=0;i<n;i++)       {           getchar();           for(j=0;j<n;j++)           {               scanf("%c",&matrix[i][j]);               if(matrix[i][j]=='N') chg_tag(i,j,0);               else if(matrix[i][j]=='E') chg_tag(i,j,1);               else if(matrix[i][j]=='S') chg_tag(i,j,2);               else if(matrix[i][j]=='W') chg_tag(i,j,3);               else if(matrix[i][j]=='M') mx=i,my=j;               else if(matrix[i][j]=='T') tx=i,ty=j;           }       }       ans=bfs(mx,my,tx,ty);       printf("Case #%d: %d\n",ca,ans);    }    return 0;}

题目(http://acm.hdu.edu.cn/showproblem.php?pid=5040):

Instrusive

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

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
23M...N...T3M.. ###..T
 

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

0 0