纯BFS+优先队列扩展。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string.h>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. bool vis[5100];
  9. char str[5100];
  10.  
  11. struct point{
  12. int x,y;
  13. int cost;
  14. bool operator < (const point &a)const{
  15. return cost>a.cost;
  16. }
  17. }st,en,passort[550],tmp,pushed;
  18. int pp;
  19. priority_queue<point>que;
  20. int dir[4][2]={
  21. {0,1},{0,-1},{1,0},{-1,0}
  22. };
  23.  
  24. int work(int r,int c,int cost){
  25. memset(vis,false,sizeof(vis));
  26. int x,y;
  27. st.cost=0;
  28. que.push(st);
  29. vis[st.x*c+st.y]=true;
  30. while(!que.empty()){
  31. tmp=que.top();
  32. que.pop();
  33. for(int i=0;i<4;i++){
  34. x=tmp.x+dir[i][0],y=tmp.y+dir[i][1];
  35. if(x>=0&&x<r&&y>=0&&y<c){
  36. if(str[x*c+y]!='#'&&!vis[x*c+y]){
  37. if(str[x*c+y]=='*'){
  38. pushed.x=x;
  39. pushed.y=y;
  40. pushed.cost=tmp.cost+cost;
  41. que.push(pushed);
  42. vis[x*c+y]=true;
  43. }
  44. else if(str[x*c+y]=='P'){
  45. for(int ps=0;ps<pp;ps++){
  46. pushed.x=passort[ps].x;
  47. pushed.y=passort[ps].y;
  48. pushed.cost=tmp.cost;
  49. que.push(pushed);
  50. vis[x*c+y]=true;
  51. }
  52. }
  53. else if(str[x*c+y]=='C')
  54. return tmp.cost;
  55. }
  56. }
  57. }
  58. }
  59. return -1;
  60. }
  61.  
  62. int main(){
  63. int r,c,cost;
  64. while(scanf("%d%d%d",&r,&c,&cost)!=EOF){
  65. getchar();
  66. int beg=0;
  67. pp=0;
  68. for(int i=0;i<r;i++){
  69. gets(str+beg);
  70. for(int k=beg;k<beg+c;k++)
  71. if(str[k]=='Y'){
  72. st.x=i;
  73. st.y=k-beg;
  74. }
  75. else if(str[k]=='C'){
  76. en.x=i;
  77. en.y=k-beg;
  78. }
  79. else if(str[k]=='P'){
  80. passort[pp].x=i;
  81. passort[pp].y=k-beg;
  82. pp++;
  83. }
  84. beg+=c;
  85. }
  86. int ans=work(r,c,cost);
  87. if(ans==-1) printf("Damn teoy!\n");
  88. else
  89. printf("%d\n",ans);
  90. while(!que.empty())
  91. que.pop();
  92. }
  93. return 0;
  94. }

  

HDU 4308 Contest 1的更多相关文章

  1. HDU 4308 BFS Saving Princess claire_

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

  2. hdu 4308 Saving Princess claire_

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

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

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

  4. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

  5. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  6. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  7. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. hdu 4308 Saving Princess claire_ BFS

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

  9. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

随机推荐

  1. BA-siemens-点位类型表

    X(超级点)  输入 0-10v 4-20ma(不可用) Ni 1000 Pt 1000 10k & 100k 热敏电阻 数字输入 脉冲计数输入 输出 0-10v 4-20ma(不可用) 数字 ...

  2. failed to sync branch You might need to open a shell and debug the state of this repo.

    failed to sync branch You might need to open a shell and debug the state of this repo. i made some c ...

  3. 压力工具代码及epoll使用

    服务器编程 P347的压力工具代码不错,对于epoll用的好,可以看.

  4. ASP.NET—011:JavaScript报错常见问题

    相信大家都写过JavaScript.JS由于语法以及自己须要实现的业务的原因,可能在一个页面上要写长篇大论.或者单独写js文件写了好几百K.JS不可否认给Web编程带了很多的方便. 可是假设JS发生了 ...

  5. 金蝶KIS标准版与金蝶K3的差别

    一.数据库  金蝶KIS标准版使用MS Access数据库.该数据库适用于小规模的数据处理,是比較经济的数据库解决方式,但当单个表的数据记录超过5万条时.执行的速度和稳定性都将受到一定程序的影响. K ...

  6. robot framework环境配置

    1.Robot framework的安装 作用:web自动化测试框架. RF框架是基于python 的,所以一定要有python环境.网上可以自行查找. 下载地址:https://pypi.pytho ...

  7. redis.conf 配置项说明

    redis.conf 配置项说明如下: Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程   daemonize no 当Redis以守护进程方式运行时,Redis ...

  8. 剑指offer——06旋转数组的最小数字(Python3)

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...

  9. c++面向对象程序设计 谭浩强 第二章答案

    类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void ...

  10. 求包含每个有序数组(共k个)至少一个元素的最小区间

    title: 求包含每个有序数组(共k个)至少一个元素的最小区间 toc: false date: 2018-09-22 21:03:22 categories: OJ tags: 归并 给定k个有序 ...