博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ-1649 Rescue---BFS+优先队列
阅读量:6962 次
发布时间:2019-06-27

本文共 1356 字,大约阅读时间需要 4 分钟。

题目链接:

题目大意:

天使的朋友要去救天使,a是天使,r 是朋友,x是卫兵。每走一步需要时间1,打倒卫兵需要另外的时间1,问救到天使所用的最少时间。注意存在救不到的情况。

思路:

BFS搜索,由于打倒卫兵时间为2,所以用BFS+优先队列做,每次出队时间最少的拓展,每个格子只走一次才是最优解

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 typedef long long ll;14 const int maxn = 2e2 + 10;15 const int INF = 1e9 + 7;16 int T, n, m, cases;17 int dir[][2] = { 1,0,0,1,-1,0,0,-1};18 struct node19 {20 int x, y, time;21 bool operator <(const node& a)const22 {23 return time > a.time;24 }25 node(){}26 node(int x, int y, int time):x(x), y(y), time(time){}27 };28 char Map[maxn][maxn];29 bool vis[maxn][maxn];30 bool judge(int x, int y)31 {32 return (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && Map[x][y] != '#');33 }34 void bfs(int x, int y)35 {36 memset(vis, 0, sizeof(vis));37 priority_queue
q;38 q.push(node(x, y, 0));39 vis[x][y] = 1;40 while(!q.empty())41 {42 node now = q.top();43 q.pop();44 if(Map[now.x][now.y] == 'r')45 {46 cout<
<
> n >> m)69 {70 int sx, sy;71 for(int i = 0; i < n; i++)72 {73 cin >> Map[i];74 for(int j = 0; j < m; j++)if(Map[i][j] == 'a')sx = i, sy = j;75 }76 bfs(sx, sy);77 }78 return 0;79 }

 

转载于:https://www.cnblogs.com/fzl194/p/8746070.html

你可能感兴趣的文章
不间断向左滚动代码
查看>>
CentOS服务器安全设置
查看>>
rhel和centos软件包管理
查看>>
我的友情链接
查看>>
select 数据绑定
查看>>
EMC PowerPath
查看>>
解决Win7与2003/XP网络访问错误问题
查看>>
关于android:windowNoTitle的问题
查看>>
随机修改nginx端口脚本及思路
查看>>
我的友情链接
查看>>
关于Office365\2016\2013:客户端Excel2016后无法打开xls\xlsx
查看>>
linux下实现ssh免密登录
查看>>
安装jar到本地maven仓库
查看>>
雅虎天气城市代码
查看>>
nginx网站基本配置过程
查看>>
用Python访问SqlServer,window和linux下的不同操作
查看>>
离群点、杆杠值、强影响点整合到一幅图
查看>>
服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
查看>>
我的友情链接
查看>>
centos6.4 搭建svn服务器
查看>>