HDU 4308 BFS+优先队列
2016-07-14 20:11
281 查看
点击打开链接
题意:从Y走到C,#代表不能走,走*的话要花费C元,P是传送门可以到达任意一个P,问最小花费
思路:直接优先队列模拟一下就行,BFS搜一下,P直接记录,遇到了就判断它能到达的点能不能走就行了,easy
题意:从Y走到C,#代表不能走,走*的话要花费C元,P是传送门可以到达任意一个P,问最小花费
思路:直接优先队列模拟一下就行,BFS搜一下,P直接记录,遇到了就判断它能到达的点能不能走就行了,easy
#include <queue> #include <stdio.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=5010; int n,m,k,sx,sy,ex,ey; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; bool vis[maxn][maxn]; char str[maxn][maxn]; struct edge{ int x,y,step,flag; friend bool operator< (edge n1,edge n2) {return n1.step>n2.step;} }; struct pos{ int x,y; pos(int a,int b){x=a;y=b;} }; vector<pos>G; int bfs(){ priority_queue<edge>que; edge c,ne; memset(vis,0,sizeof(vis)); c.x=sx,c.y=sy,c.step=0; vis[c.x][c.y]=1; que.push(c); while(!que.empty()){ c=que.top();que.pop(); if(c.x==ex&&c.y==ey) return c.step; if(str[c.x][c.y]=='P'){ int len=G.size(); for(int ll=0;ll<len;ll++){ pos ttt=G[ll]; if(vis[ttt.x][ttt.y]) continue; ne.x=ttt.x;ne.y=ttt.y;ne.step=c.step; vis[ne.x][ne.y]=1; que.push(ne); } } for(int i=0;i<4;i++){ int xx=c.x+dir[i][0]; int yy=c.y+dir[i][1]; if(xx<0||xx>n-1||yy<0||yy>m-1||str[xx][yy]=='#') continue; if(vis[xx][yy]) continue; ne.x=xx;ne.y=yy;ne.step=c.step; if(str[xx][yy]=='*') ne.step+=k; vis[ne.x][ne.y]=1; que.push(ne); } } return -1; } int main(){ while(scanf("%d%d%d",&n,&m,&k)!=-1){ G.clear(); for(int i=0;i<n;i++) scanf("%s",str[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(str[i][j]=='Y') sx=i,sy=j; if(str[i][j]=='C') ex=i,ey=j; if(str[i][j]=='P') G.push_back(pos(i,j)); } } int ans=bfs(); if(ans==-1) printf("Damn teoy!\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- 尚学堂 JAVA DAY12 java程序执行时内存的分配
- CODE[VS]-最小数和最大数-整数处理-天梯青铜
- perl 面向对象 new方法
- 洛谷 P2296 [NOIP2014 D2T2] 寻找道路
- Google Guava中Preconditions的用法,让前置条件判断变得更优雅
- 基于GPU计算的贴图生成工具Knald
- UDP之丢包原因及具体问题分析
- JavaWEB小知识学习--验证码生成
- 数据库(学习整理)----3--Oracle创建表和设置约束
- git 恢复丢失的文件
- perl 面向对象 new方法
- perl 面向对象 new方法
- html概括
- MFC知识点(DDX_Control 与 DDX_Text ,ON_COMMAND和ON_MESSAGE)
- MFC中,要实现某功能必须先将GDI对象选入设备描述表?
- C++中类与对象的讲解(通俗易懂)
- 尚学堂 JAVA DAY11 概念总结
- web项目设计中框架的数据流
- MySQL使用存储过程循环插入数据
- 区间选点问题---poj-1328