原题直通车:HDU 4308 Saving Princess claire_

分析: 两次BFS分别找出‘Y’、‘C’到达最近的‘P’的最小消耗。再算出‘Y’到‘C’的最小消耗,比较出最小值

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
const int inf=0xFFFFFFF;
int n,m,k;
char f[5005][5005];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int dis[5005][5005];
struct node{
int x,y,cost;
node(int a,int b,int c){
x=a, y=b, cost=c;
}
};
int BFS(node st,node et,int &p){
queue<node>M;
M.push(st);
while(!M.empty()){
node rt=M.front(); M.pop();
if(f[rt.x][rt.y]==f[et.x][et.y]) return rt.cost;
for(int i=0;i<4;++i){
node ne=rt;
ne.x+=dx[i], ne.y+=dy[i];
if(ne.x<0||ne.y<0||ne.x>=n||ne.y>=m||f[ne.x][ne.y]=='#') continue;
if(f[ne.x][ne.y]=='P'){
if(p>ne.cost) p=ne.cost;
continue;
}
if(f[ne.x][ne.y]=='*') ne.cost+=k;
if(ne.cost<dis[ne.x][ne.y]){
dis[ne.x][ne.y]=ne.cost;
M.push(ne);
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&k)){
int ci,cj,yi,yj;
for(int i=0;i<n;++i){
scanf("%s",f[i]);
for(int j=0;j<m;++j){
if(f[i][j]=='C')
ci=i, cj=j;
else if(f[i][j]=='Y')
yi=i, yj=j;
dis[i][j]=inf;
}
}
node cc(ci,cj,0), yy(yi,yj,0);
int cp=inf, yp=inf;
int t1=BFS(cc,yy,cp);
int t2=BFS(yy,cc,yp);
if(t1==-1){
if(cp!=inf&&yp!=inf) printf("%d\n",cp+yp);
else puts("Damn teoy!");
}
else {
if(cp!=inf&&yp!=inf&&t1>cp+yp) printf("%d\n",cp+yp);
else printf("%d\n",t1);
}
}
return 0;
}

HDU 4308 BFS Saving Princess claire_的更多相关文章

  1. Saving Princess claire_(hdu 4308 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...

  2. hdu 4308 Saving Princess claire_

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

  3. 2012 #1 Saving Princess claire_

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

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

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

  5. hdu 4308 Saving Princess claire_ BFS

    为了准备算法考试刷的,想明确一点即可,全部的传送门相当于一个点,当遇到一个传送门的时候,把全部的传送门都压入队列进行搜索 贴代码: #include <iostream> #include ...

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

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

  7. BFS(最短路) HDOJ 4308 Saving Princess claire_

    题目传送门 题意:一个(r*c<=5000)的迷宫,起点'Y‘,终点'C',陷阱‘#’,可行路‘*’(每走一个,*cost),传送门P,问Y到C的最短路 分析:一道最短路问题,加了传送门的功能, ...

  8. ZOJ 3369 Saving Princess

    Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  9. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

随机推荐

  1. php 迭代器与和生成器

    php有很多功能强大的接口,其中ArrayAccess 与 Iterator 的配合使用可以让对象与数组一样有着灵活的访问性. 当然,用ArrayAccess 与 Iterator 配合可以用来对付数 ...

  2. Ghost win7 系统安装(虚拟机)

    1.将Ghost.iso添加到数据存储iso文件中,启动虚拟机,根据相关提示,文件格式化系统硬盘,完成操作后关机: 2.进入BIOS,设置从CDROM启动系统(否则系统会提示找到引导文件): 3.将系 ...

  3. DNS域欺骗攻击详细教程之Linux篇

    .DNS域欺骗攻击原理 DNS欺骗即域名信息欺骗是最常见的DNS安全问题.当一 个DNS服务器掉入陷阱,使用了来自一个恶意DNS服务器的错误信息,那么该DNS服务器就被欺骗了.DNS欺骗会使那些易受攻 ...

  4. Oracle EBS-SQL (PO-1):检查供货比例异常.sql

    select distinct msr.sourcing_rule_name                     名称 , msi.description                      ...

  5. 如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    http://blog.chinaunix.net/uid-20662820-id-4023733.html http://www.faqs.org/faqs/snmp-faq/part2/ http ...

  6. perl 继承写法

    use base (Critter); 和 BEGIN{ require Critter; @ISA=qw/Critter/; } 这两种写法是等价

  7. HDU 5700 区间交(线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5700 [题目大意] 给出一个长度为n的数列和m个区间,现在求k个区间,使得他们的区间交内的数列项和 ...

  8. java常量池理解

    String类两种不同的创建方式 String s1 = "zheng"; //第一种创建方式 String s2 = new String("junxiang" ...

  9. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  10. FieldInfo.IsSpecialName Property【转】

    Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttribute ...