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 ...
随机推荐
- EventLog监控
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/logs/eventlogs/ http://etuto ...
- ASP.NET缓存中Cache过期的三种策略
原文:ASP.NET缓存中Cache过期的三种策略 我们在页面上添加三个按钮并双击按钮创建事件处理方法,三个按钮使用不同的过期策略添加ASP.NET缓存. <asp:Button ID=&quo ...
- 从一个实例,看new FunctionName()的内部机制
下面的代码: function Dog(name) { this.name = name; Dog.prototype = { shout: function() { alert("I am ...
- Unity3d 打包时报错 CommandInvokationFailure: Unable to convert classes into dex format. See the Console for details.
今天打包带有Android插件的unity3d 项目是,报错CommandInvokationFailure: Unable to convert classes into dex format. S ...
- 网易云课堂_程序设计入门-C语言_第三周:循环_1奇偶个数
1 奇偶个数(5分) 题目内容: 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据.程序输出读到的数据中的奇数和偶数的个数. 输入格式: 一系列正整数,整数的范围是(0,1 ...
- g++编译cpp文件
gdb调试c程序打不到断点的原因可能是编译c文件的时候没有加-g选项,-g选项是编译加debug信息的,不加是打不到断点的 g++编译cpp文件 g++ -g -c *.cpp 编译 g+ ...
- 实现一个做双向NAT的虚拟网卡
问题描写叙述与解决方式 还是老问题.Linux系统中通过iptables配置的NAT无法在双向通信环境中使用,你无法配置一条NAT规则实现对两个方向主动发起的流量做NAT,解决问题的方案有好几种: 1 ...
- 浅谈android的selector,背景选择器
shape和selector的结合使用 (2013-04-07 11:11:00) 转载▼ 分类: android 1.Shape (1)作用:XML中定义的几何形状 (2)位置:res/draw ...
- android_Intent对象初步(Activity传统的价值观念)
说明:初步Intent物.主要使用Intent对象在Activity之间传递数据的方法. 样例:由MainActivity→OtherActivity的跳转过程中,把数据传递给OtherActivit ...
- 第三章SignalR在线聊天例子
第三章SignalR在线聊天例子 本教程展示了如何使用SignalR2.0构建一个基于浏览器的聊天室程序.你将把SignalR库添加到一个空的Asp.Net Web应用程序中,创建用于发送消息到客户端 ...