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 ...
随机推荐
- JavaScript学习笔记(三)this关键字
this是Javascript的关键字,代表在函数运行时,自动生成一个内部对象,只能在函数内部使用.例如: function test() { this.x = 1; } 随着函数的使用场合不同,th ...
- windows开机启动nginx
1 .http://www.cuplayer.com/player/PlayerCode/Nginx/2014/0919/1577.html 2. http://www.cnblogs.com/xus ...
- Controller中获取输入参数注解使用总结
1.处理request的uri部分的参数(即restful访问方式):@PathVariable. 当使用restful访问方式时, 即 someUrl/{paramId}, 这时的参数可通过 @Pa ...
- Mvc4.0 提取 Cookie 里的东西
/// <summary> /// 提取Cookie /// </summary> /// <returns></returns> public sta ...
- 做ie8css样式时浏览器默认杂项模式遇到的一个小坑
1 进行ie浏览器的样式兼容的时候,首先要确保打开浏览器浏览网页的时候的文本模式要为当前浏览器的"标准模式",注意<!DOCTYPE html>不缺失不错误,以免浏览器 ...
- Maven命令行创建web项目,并部署到jobss当中(解决No plugin found for prefix 'jboss-as' in the current project and in the plugin groups [org.apache.maven.plugins,问题)
首件创建项目:此处可参照:http://maven.apache.org/guides/mini/guide-webapp.html mvn archetype:generate -DgroupId= ...
- iOS的Ping++支付接入步骤(详细)
Ping++ SDK 代码下载地址: https://github.com/CoderLeezhen/PingppDemo 参考链接: https://www.pingxx.com/guidance/ ...
- 剑指offier第4题
/* 问题1:替换字符串,是在原来的字符串上做替换,还是新开辟一个字符串做替换! 问题2:在当前字符串替换,怎么替换才更有效率(不考虑java里现有的replace方法). 从前往后替换,后面的字符要 ...
- 解压tomcat后一闪而过的问题
解压tomcat压缩包后,直接点击startup会出现一闪而过. 免安装的tomcat双击startup.bat后,启动窗口一闪而过,而且tomcat服务未启动. 原因是:在启动tomcat是,需要读 ...
- 关于上次我写的那个ATM程序 ,程序没有什么错,但是有些麻烦,两个类中有好多成员函数重复,因此我把ATM重新写了一边。
不好意思!但是现在这个程序比上次那个好多了,而且没有重复,程序看起来比较简练,以下是新程序: #include<iostream>#include<string>using n ...