HDU 4308 BFS Saving Princess claire_
原题直通车: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_的更多相关文章
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- hdu 4308 Saving Princess claire_
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Description Princess cla ...
- 2012 #1 Saving Princess claire_
Saving Princess claire_ Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu 4308 Saving Princess claire_ BFS
为了准备算法考试刷的,想明确一点即可,全部的传送门相当于一个点,当遇到一个传送门的时候,把全部的传送门都压入队列进行搜索 贴代码: #include <iostream> #include ...
- HDU 4308 Saving Princess claire_(简单BFS)
求出不使用P点时起点到终点的最短距离,求出起点到所有P点的最短距离,求出终点到所有P点的最短距离. 答案=min( 不使用P点时起点到终点的最短距离, 起点到P的最短距离+终点到P的最短距离 ) #i ...
- BFS(最短路) HDOJ 4308 Saving Princess claire_
题目传送门 题意:一个(r*c<=5000)的迷宫,起点'Y‘,终点'C',陷阱‘#’,可行路‘*’(每走一个,*cost),传送门P,问Y到C的最短路 分析:一道最短路问题,加了传送门的功能, ...
- ZOJ 3369 Saving Princess
Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
随机推荐
- PHP面试题之设计模式
设计模式是技术面试的时候难免会被问到的一个问题,特别会让你举例说明各种设计模式的使用场景. 使用设计模式可以减轻我们的工作量,优化我们的代码. 设计模式非常的多,这里介绍单例模式,工厂模式,组合模式, ...
- hdu 5446 Unknown Treasure 中国剩余定理+lucas
题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...
- bugfree搭建
- 用正则表达式抓取网页中的ul 和 li标签中最终的值!
获取你要抓取的页面 const string URL = "http://www.hn3ddf.gov.cn/price/GetList.html?pageno=1& ...
- Hibernate摘记
原理: 1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.xml中的<mapping reso ...
- Mono For Android中简单实现按钮的动画效果
Android中动画的分Tween Animation和Frame Animation,本节主要讲Tween Animation的实现. 一般是通过XML文件来定义动画的,具体如下: 1.在项目res ...
- ecshop的小总结
1 为防止非法调用自己的页面,在被包含页面加上: if (!defined('IN_ECS')) {die('Hacking attempt'); } 2 在主动包含页面要定义define('IN_ ...
- php 验证身份证有效性,根据国家标准GB 11643-1999 15位和18位通用
//验证身份证是否有效 function validateIDCard($IDCard) { if (strlen($IDCard) == 18) { return check18IDCard($ID ...
- 量身定制顺美男女西服、衬衫、大衣、T恤等 - 北京58同城
量身定制顺美男女西服.衬衫.大衣.T恤等 - 北京58同城 量身定制顺美男女西服.衬衫.大衣.T恤等 发布时间:2014-04-11浏览2次
- ubuntu下tcpdump使用
Ubuntu默认是安装好了tcpdump工具的,如果没有安装的话使用sudo apt-get install tcpdump即可安装. (如果遇到tcpdump: no suitable devi ...