您的位置:首页 > 大数据 > 人工智能

hdu 4308 Saving Princess claire(BFS)

2015-07-16 23:47 537 查看


Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2465 Accepted Submission(s): 877



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



题意:给你一个n*m的图,经过每个‘*’的花费为k,‘#’不能通过,‘P'是传送门,任意两个传送们之间可以随便到达,花费0,求Y 到 C的最少花费。

题解:bfs,遇到第一个P则把所有P进队,因为任意两个P的花费为0.

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<vector>
#include<queue>
#include<cmath>
#define N 5010
#define ll long long

using namespace std;

struct node {
    int num;
    int x,y;
};
queue<node>p;
node a;
int n,m,k;

struct edge {
    int x,y;
} b[350];
int l;

bool vis

;
char s

;
int ans;
int Yi,Yj,Ci,Cj;
int xx[4]= {0,0,1,-1};
int yy[4]= {1,-1,0,0};

void bfs() {
    memset(vis,0,sizeof vis);
    while(p.size()) {
        p.pop();
    }
    a.num=0;
    a.x=Yi,a.y=Yj;
    p.push(a);
    vis[Yi][Yj]=1;
    while(!p.empty()) {
        node t=p.front();
        if(t.x==Ci&&t.y==Cj) {
            ans=t.num;
            break;
        }
        p.pop();
        for(int i=0; i<4; i++) {
            int nx=xx[i]+t.x;
            int ny=yy[i]+t.y;
            a.x=nx;
            a.y=ny;
            a.num=t.num;
            if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]!='#'&&!vis[nx][ny]) {
                if(s[nx][ny]=='P') {
                    for(int j=0; j<l; j++) {
                        a.x=b[j].x;
                        a.y=b[j].y;
                        p.push(a);
                        vis[a.x][a.y]=1;
                    }
                } else if(s[nx][ny]=='*') {
                    a.num+=1;
                    p.push(a);
                } else {
                    p.push(a);
                }
                vis[nx][ny]=1;
            }
        }

    }
}

int main() {
   // freopen("test.in","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k)) {
        l=0;
        for(int i=0; i<n; i++) {
            scanf("%s",s[i]);
            for(int j=0; j<m; j++) {
                if(s[i][j]=='Y') {
                    Yi=i;
                    Yj=j;
                } else if(s[i][j]=='C') {
                    Ci=i;
                    Cj=j;
                } else if(s[i][j]=='P') {
                    b[l].x=i;
                    b[l++].y=j;
                }
            }
        }
        ans=-1;
        bfs();
        if(ans==-1) {
            printf("Damn teoy!\n");
            continue;
        }
        ans*=k;
        printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: