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[][];
int v[][];
struct node
{
int x,y,ans;
}q[];
int jx[]={,-,,};
int jy[]={,,,-};
void bfs(int xx,int yy)
{
int e=;
int s=;
memset(v,,sizeof(v));
struct node t,f;
t.x=xx;
t.y=yy;
t.ans=;
v[t.x][t.y]=;
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=;i<;i++)
{
f.x=t.x+jx[i];
f.y=t.y+jy[i];
if(f.x>=&&f.x<n&&f.y>=&&f.y<m&&map[f.x][f.y]!='#'&&v[f.x][f.y]==)
{
if(map[f.x][f.y]=='*')
{
f.ans=t.ans+V;
q[e++]=f;
v[f.x][f.y]=;
}
else if(map[f.x][f.y]=='C')
{
f.ans=t.ans;
q[e++]=f;
v[f.x][f.y]=;
}
else if(map[f.x][f.y]=='P')
{
for(int j=;j<n;j++)
{
for(int k=;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]=;
}
}
}
}
}
}
}
printf("Damn teoy!\n");
return ;
}
int main()
{
int xx,yy;
while(scanf("%d%d%d",&n,&m,&V)!=EOF)
{
for(int i=;i<n;i++)
scanf("%*c%s",map[i]);
int j;
for(int i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(map[i][j]=='Y')
{
xx=i;
yy=j;
break;
}
}
if(j!=m) break;
}
bfs(xx,yy);
}
return ;
}
 

Saving Princess claire_(hdu 4308 bfs模板题)的更多相关文章

  1. HDU 4308 Saving Princess claire_(简单BFS)

    求出不使用P点时起点到终点的最短距离,求出起点到所有P点的最短距离,求出终点到所有P点的最短距离. 答案=min( 不使用P点时起点到终点的最短距离, 起点到P的最短距离+终点到P的最短距离 ) #i ...

  2. HDU 4308 BFS Saving Princess claire_

    原题直通车:HDU 4308 Saving Princess claire_ 分析: 两次BFS分别找出‘Y’.‘C’到达最近的‘P’的最小消耗.再算出‘Y’到‘C’的最小消耗,比较出最小值 代码: ...

  3. hdu 4308 Saving Princess claire_

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Description Princess cla ...

  4. 2012 #1 Saving Princess claire_

    Saving Princess claire_ Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

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

    Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  6. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  7. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

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

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  9. HDU 2138 Miller-Rabin 模板题

    求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...

随机推荐

  1. Android开发训练之第五章——Building Apps with Connectivity & the Cloud

    Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...

  2. 【大数据系列】windows搭建hadoop开发环境

    一.安装JDK配置环境变量 已经安装略过 二.安装eclipse 已经安装略过 三.安装Ant 1.下载http://ant.apache.org/bindownload.cgi 2.解压 3.配置A ...

  3. Android studio快捷键设置

    Android Studio格式化代码设置和代码风格设置.代码提示键  http://blog.csdn.net/u010156024/article/details/48207145 Android ...

  4. 安装cnpm

    使用淘宝镜像的cnpm $ npm install -g cnpm --registry=https://registry.npm.taobao.org

  5. async 与await

    一. async 与await (https://segmentfault.com/a/1190000007535316) 1.async 是“异步”的简写,而 await 可以认为是 async w ...

  6. Cracking the Coding Interview(String and array)

    1.1实现一个算法判断一个字符串是否存在重复字符.如果不能利用另外的数据结构又该如何实现? My solution: /** *利用类似一个hash table的计数 *然后检查这个hash tabl ...

  7. 23种设计模式之享元模式(FlyWeight)

    享元模式是一种对象结构型模式,通过运用共享技术,有效地支持大量细粒度的对象.系统只使用少量的对象,而这些对象都很相似,状态变化很小,对象使用次数增多.享元对象能做到共享的关键是区分内部状态和外部状态. ...

  8. dhroid - NetJSONAdapter 网络化的adapter

    关于adapter 我想对于大家来说已经不陌生了,基本应用都会用的很多,不知道现在你是不是还是按一定的套路写很多代码去实现adapter我想大多数人还是写个adapter继承自baseadapter ...

  9. mysql判断一个字符串是否包含某几个字符

    使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0

  10. Java工程师之Redis实战系列教程前言&目录

    系列前言 Java工程师之Redis实战系列教程,同其他教程一样,均是在下学习笔记,本系列主要参考自<Redis-in-action>,将书本中的有趣的例子转化为能解决特定问题的示例程序, ...