hdu 4308 #BFS

来源:互联网 发布:南京大学网络 编辑:程序博客网 时间:2024/05/22 04:13

优先级的广搜

Saving Princess claire_

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


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 3Y*C1 3 2Y#C1 5 2YP#PC
 

Sample Output
3Damn teoy!0
 


#include <stdio.h>#include <string.h>#include <queue>using namespace std;#define N 5000int move[][2] = {0,1,1,0,0,-1,-1,0};int n,m,cost,pp[505][2],top;char mat[N][N];int vis[N][N];struct Point{    int x,y,cost;    Point(int a = 0,int b = 0,int c = 0):x(a),y(b),cost(c){}}p,pt;bool operator<(Point a,Point b){    return a.cost > b.cost;}bool inner(int x,int y){    return x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && mat[x][y] != '#';}int bfs(){    priority_queue<Point> que;    que.push(p);    vis[p.x][p.y] = 1;   // memset(vis,0,sizeof(vis)); //若用memset这里竟然MLE    while(!que.empty())    {        p = que.top();        que.pop();        int i;        for(i = 0; i < 4; ++i)        {            pt = Point(p.x + move[i][0],p.y + move[i][1],p.cost);            if(inner(pt.x,pt.y))            {                if(mat[pt.x][pt.y] == 'C')                    return pt.cost;                if(mat[pt.x][pt.y] == '*')                {                    vis[pt.x][pt.y] = 1;                    pt.cost++;                    que.push(pt);                }                else                {                    int k;                    Point ptt;                    for(int j = 0; j < top; ++j)                    {                        /*                        for(k = 0; k < 4; ++k)                        {                            ptt = Point(pp[j][0] + move[k][0],pp[j][1] + move[k][1],pt.cost);                            if(inner(ptt.x,ptt.y))                            {                                if(mat[ptt.x][ptt.y] == 'C')                                    return ptt.cost;                                if(mat[ptt.x][ptt.y] == '*')                                    ++ptt.cost;                                vis[ptt.x][ptt.y] = 1;                                que.push(ptt);                            }                        }                        */                        if(!vis[pp[j][0]][pp[j][1]])                        {                            vis[pp[j][0]][pp[j][1]] = 1;                            que.push(Point(pp[j][0],pp[j][1],pt.cost));                        }                    }                }            }        }    }    return -1;}int main(){    while(scanf("%d%d%d",&n,&m,&cost) == 3)    {        int i,j;        top = 0;        for(i = 0; i < n; ++i)        {            scanf("%s",mat[i]);            for(j = 0; j < m; ++j)            {                vis[i][j] = 0;                if(mat[i][j] == 'Y')                    p = Point(i,j,0);                else if(mat[i][j] == 'P')                {                    pp[top][0] = i;                    pp[top][1] = j;                    ++top;                }            }        }        int res = bfs();        if(res == -1)            printf("Damn teoy!\n");        else            printf("%d\n",res * cost);    }    return 0;}