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_ 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

题目唯一需要注意的是,P是传送带,可进入所有传送带。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int n,m,V;
char map[1001][1001];
int v[1001][1001];
struct node
{
    int x,y,ans;
}q[100001];
int jx[]={1,-1,0,0};
int jy[]={0,0,1,-1};
void bfs(int xx,int yy)
{
    int e=0;
    int s=0;
    memset(v,0,sizeof(v));
    struct node t,f;
    t.x=xx;
    t.y=yy;
    t.ans=0;
    v[t.x][t.y]=1;
    q[e++]=t;
    while(s<e)
    {
        t=q[s++];
        if(map[t.x][t.y]==‘C‘)
        {
            printf("%d\n",t.ans);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            f.x=t.x+jx[i];
            f.y=t.y+jy[i];
            if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&map[f.x][f.y]!=‘#‘&&v[f.x][f.y]==0)
            {
                if(map[f.x][f.y]==‘*‘)
                {
                    f.ans=t.ans+V;
                    q[e++]=f;
                    v[f.x][f.y]=1;
                }
                else if(map[f.x][f.y]==‘C‘)
                {
                    f.ans=t.ans;
                    q[e++]=f;
                    v[f.x][f.y]=1;
                }
                else if(map[f.x][f.y]==‘P‘)
                {
                    for(int j=0;j<n;j++)
                    {
                        for(int k=0;k<m;k++)
                        {
                            if(map[j][k]==‘P‘)
                            {
                                f.ans=t.ans;
                                f.x=j;
                                f.y=k;
                                q[e++]=f;
                                v[f.x][f.y]=1;
                            }
                        }
                    }
                }
            }
        }
    }
    printf("Damn teoy!\n");
    return ;
}
int main()
{
    int xx,yy;
    while(scanf("%d%d%d",&n,&m,&V)!=EOF)
    {
        for(int i=0;i<n;i++)
        scanf("%*c%s",map[i]);
        int j;
        for(int i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(map[i][j]==‘Y‘)
                {
                    xx=i;
                    yy=j;
                    break;
                }
            }
            if(j!=m) break;
        }
        bfs(xx,yy);
    }
    return 0;
}

Saving Princess claire_(hdu 4308 bfs模板题),布布扣,bubuko.com

时间: 2024-12-16 07:24:49

Saving Princess claire_(hdu 4308 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

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

Knight Moves(hdu1372 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731    Accepted Submission(s): 4059 Problem Description A friend of you is doing res

hdu----(4308)Saving Princess claire_(搜索)

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

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

poj2251Dungeon Master(bfs模板题)

题目链接:http://poj.org/problem?id=2251 可以说是bfs的模板题了. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<queue> 5 using namespace std; 6 char pic[32][32][32]; 7 int vis[32][32][32]; 8 int dir[6][3]={0,0,1,0,0,-1,1,0