hdu 4308 Saving Princess claire(BFS)

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[N][N];
char s[N][N];
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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 07:32:12

hdu 4308 Saving Princess claire(BFS)的相关文章

hdu 4308 Saving Princess claire_ BFS

为了准备算法考试刷的,想明白一点就行,所有的传送门相当于一个点,当遇到一个传送门的时候,把所有的传送门都压入队列进行搜索 贴代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; const int MAXN = 5000+50; int r,c,f,si,sj,e

HDU 4308 Saving Princess claire_

BFS问题. 题意是问 王子救公主 需要花费多少钱.每路过一个 * 就要支付 cost 那么多的钱.求最短. 多个P 传送点就 多个点进队即可. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream&g

HDU 4308 最短路或者bfs

Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2435    Accepted Submission(s): 864 Problem Description Princess claire_ was jailed in a maze by Grand Demon Monster(GDM)

HDU 5025 Saving Tang Monk(BFS+状压)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel,

hdu 5025 Saving Tang Monk (bfs+状态压缩)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 41    Accepted Submission(s): 10 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi

hdu 5025 Saving Tang Monk(bfs)

题目链接 题意: 给出n*n的网格,有且只有一个K(孙悟空)和一个T(唐僧),最多有m把钥匙,最多5条蛇,每走一格的时间为1,走到蛇的格子(杀蛇时间为1)的时间为2, 取钥匙要按照顺序来,问能救到唐僧,如果可以输出最短时间. 分析:

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

2012 #1 Saving Princess claire_

Saving Princess claire_ Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4308 Description Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy. Out of anger, little Prince ykwd

ZOJ 3369 Saving Princess

Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 336964-bit integer IO format: %lld      Java class name: Main Special Judge Saving princesses is always a hard work. Ivan D'Ourack is planning t