2019 校内赛 RPG的地牢猎手(bfs+优先队列)
Problem Description
地牢表示为n行m列的块矩阵,其中每个块只可以是障碍块、入口、出口或数字块,角色不能通过障碍块,其余块均可任意通行,角色经过数字块时疲劳度会增加相应的数字(经过出入口不产生疲劳)。
地牢有若干入口和1个出口,角色可以任意选择入口。
抵达出口时的疲劳值越少,地牢提供的奖励越多,为了尽可能获得更多的奖励,问从进入地牢到离开地牢所产生的疲劳值最少是多少,如果无法抵达出口,输出-1。
图示:障碍块 ’X’,数字块 ’0’~’9’,入口 ‘S’,出口 ‘E’。
Input
随后为t组输入,每组测试中,第一行为矩阵行列数n m,随后n行长度为m的字符串表示地牢。
t<=100
1<=n,m<=100
Output
Sample Input
1
3 3
SXS
123
XEX
Sample Output
3
思路:因为只有一个终点 所以从终点开始搜索 每次用优先队列弹出最优点的情况
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n,m;
char G[][];
bool vis[][];
int ans;
struct node{
int x,y,v;
friend operator < (node a,node b){
return a.v>b.v;
}
};
void bfs(int ex,int ey){
priority_queue<node> q;
node t; t.x=ex; t.y=ey; t.v=;
q.push(t);
while(!q.empty()){
node temp=q.top();
q.pop();
if(G[temp.x][temp.y]=='S'){
ans=temp.v;
return ;
}
for(int i=;i<;i++){
int xx=temp.x+dir[i][];
int yy=temp.y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&!vis[xx][yy]&&G[xx][yy]!='X'){
vis[xx][yy]=;
node tt; tt.x=xx; tt.y=yy;
if(G[xx][yy]>=''&&G[xx][yy]<='')
tt.v=temp.v+G[xx][yy]-'';
else tt.v=temp.v;
q.push(tt);
}
}
}
ans=-;
}
int main(){
ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--){
memset(vis,,sizeof(vis));
cin>>n>>m;
int ex,ey;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
cin>>G[i][j];
if(G[i][j]=='E')
ex=i,ey=j;
}
bfs(ex,ey);
cout<<ans<<endl;
}
return ;
}
2019 校内赛 RPG的地牢猎手(bfs+优先队列)的更多相关文章
- 2019 校内赛 RPG的天赋分支(贪心)
Problem Description 很多游戏都有天赋树的概念,天赋树的不同分支具有不同的属性加成,那么合理选择分支就非常重要了.Luke最近沉迷一款RPG游戏,它的天赋树机制如下:角色具有n个可选 ...
- hdu6578 2019湖南省赛D题Modulo Nine 经典dp
目录 题目 解析 AC_Code @ 题目 第一题题意是一共有{0,1,2,3}四种数字供选择,问有多少个长度为n的序列满足所有m个条件,每个条件是说区间[L,R]内必须有恰好x个不同的数字. 第二题 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- 【Java基础】for循环实现在控制台打印水仙花数
代码: /* * 需求:在控制台输出所有的”水仙花数” * * 分析: * 什么是水仙花数呢? * 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身. * 举例:153就是一个水仙花数. ...
- Mybatis测试用例
package cn.zhangxueliang.mybatis.mapper; import static org.junit.Assert.*; import java.io.InputStrea ...
- Git发生SSL certificate problem: certificate ha错误的解决方法
这两天,不知道为什么,用Git提交代码到服务器时,总出现SSL certificate problem: unable to get local issuer certificate while ac ...
- Netty ByteBuf 和 String 转换
参考https://blog.csdn.net/o1101574955/article/details/81024102 参考http://youyu4.iteye.com/blog/2361959 ...
- python之路--关于线程的一些方法
一 . 线程的两种创建方式 from threading import Thread # 第一种创建方式 def f1(n): print('%s号线程任务'%n) def f2(n): print( ...
- python函数、模块、包
一.函数 定义函数: def fun_name(para_list): coding def fun_name(para_list): coding return xxx 使用函数,fun_name( ...
- spring boot 启动脚本
启动的时候 在 boot_class 中有个:com.sankuai.qcs.regulation.shanghai.App 这是spring boot的配置,在 bin/run_main.sh中 ...
- 用 Python分析朋友圈好友的签名
需要用到的第三方库: numpy:本例结合wordcloud使用 jieba:对中文惊进行分词 PIL: 对图像进行处理(本例与wordcloud结合使用) snowlp:对文本信息进行情感判断 wo ...
- supervisor /var/run/supervisor/supervisor.sock not found 或者/tmp/supervisor.sock not found
刚按装完supervisor,这时候用supervisorctr -c supervisor.conf 会报错: /var/run/supervisor/supervisor.sock not fou ...
- QTP 自动货测试桌面程序-笔记(添加控件仓库并关联到action)
录制或编写脚本前先添加行为对应的控件仓库: 心得:多个测试action使用的同一个窗体的仓库单独放于一个仓库中,可以在多个action中添加关联使用 将同一个窗体的控件只放于一个仓库中,减少使用时的名 ...