MUTC 1 E - Saving Princess claire_ 迷宫搜索

Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1495    Accepted Submission(s): 558


Problem Description
Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy.
Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess.
The maze can be described as a matrix of r rows and c columns, with grids, such as 'Y', 'C', '*', '#' and 'P', in it. Every grid is connected with its up, down, left and right grids.
There is only one 'Y' which means the initial position when Prince ykwd breaks into the maze.
There is only one 'C' which means the position where Princess claire_ is jailed.
There may be many '*'s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station.
The grid represented by '#' means that you can not pass it. 
It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior.
'P' means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another. 

They say that there used to be some toll stations, but they exploded(surely they didn't exist any more) because of GDM teoy's savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes.
Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn't want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he needs to take his princess back.
 

Input
Multiple cases.(No more than fifty.)
The 1st line contains 3 integers, r, c and cost. 'r', 'c' and 'cost' is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )
Then an r * c character matrix with 'P' no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.
 

Output
One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)
 

Sample Input
   
   
   
   
1 3 3 Y*C 1 3 2 Y#C 1 5 2 YP#PC
 

Sample Output
   
   
   
   
3 Damn teoy! 0
 

Author
BUPT
 

Source
2012 Multi-University Training Contest 1
 

Recommend
zhuyuanchen520
------------------------
直接bfs = =木有优化

------------------------

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int direct[4][2]= { {0,1},{1,0},{-1,0},{0,-1} };

struct POINT
{
    int x;
    int y;
    long long sp;
    POINT()
    {
        x=0;
        y=0;
        sp=0;
    }
    POINT(int _x,int _y):x(_x),y(_y) {}
};

int a[5111][5111];
bool vis[5111][5111];
char map[5111][5111];
POINT P_num[5111];
int P_cnt;
int n,m,c;

long long ans;

queue<POINT>que;
POINT start,end;

bool check(POINT p)
{
    if (p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m)
    {
        return true;
    }
    return false;
}

int bfs(POINT st,char ch)
{
    while (!que.empty()) que.pop();
    memset(vis,0,sizeof(vis));
    POINT tmp,q,ppp;
    q=st;
    vis[q.x][q.y]=true;
    que.push(q);
    while (!que.empty())
    {
        tmp=que.front();
        que.pop();
        for (int i=0; i<4; i++)
        {
            q=tmp;
            q.x+=direct[i][0];
            q.y+=direct[i][1];
            if ( check(q)&&map[q.x][q.y]!='#'&&!vis[q.x][q.y] )
            {
                if (map[q.x][q.y]=='C')
                {
                    return q.sp;
                }
                else if (map[q.x][q.y]=='P')
                {
                    for (int k=0; k<P_cnt; k++)
                    {
                        ppp=P_num[k];
                        ppp.sp=q.sp;
                        if (!vis[ppp.x][ppp.y])
                        {
                            vis[ppp.x][ppp.y]=true;
                            que.push(ppp);
                        }
                    }
                }
                else if (map[q.x][q.y]=='*')
                {
                    q.sp+=1;
                    vis[q.x][q.y]=true;
                    que.push(q);
                }
            }
        }
    }
    return -1;
}



int main()
{
    while (~scanf("%d%d%d",&n,&m,&c))
    {
        for (int i=1; i<=n; i++)
        {
            scanf("%s",map[i]+1);
        }
        P_cnt=0;
        for (int i=1; i<=n; i++)
        {
            for (int j=1; j<=m; j++)
            {
                if (map[i][j]=='Y')
                {
                    start.x=i;
                    start.y=j;
                }
                if (map[i][j]=='C')
                {
                    end.x=i;
                    end.y=j;
                }
                if (map[i][j]=='P')
                {
                    P_num[P_cnt++]=POINT(i,j);
                }
            }
        }
        ans=bfs(start,'C');
        if (ans==-1)
        {
            puts("Damn teoy!");
        }
        else
        {
            ans=ans*c;
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


你可能感兴趣的:(搜索)