博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdoj 1180 搜索 + bfs + 优先队列
阅读量:4216 次
发布时间:2019-05-26

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

#include
#include
#define MAX 22#include
using namespace std;struct Node{ int x, y, s; friend bool operator<(const Node &a, const Node &b){ return a.s > b.s; }};int vis[MAX][MAX];char c;int dir[4][2] = {
{1,0},{-1,0},{0,1},{0,-1}};int n, m;int sx, sy;char map[MAX][MAX];int check(int x, int y){ if(x>=0 && x
=0 && y
pq; node.x = x; node.y = y; node.s = 0; pq.push(node); vis[node.x][node.y] = 1;//标记已经访问 while(!pq.empty()){ t1 = pq.top(); pq.pop(); if(map[t1.x][t1.y] == 'T') return t1.s; //if(map[t2.x][t2.y] == 'T') return t2.s; for(int i = 0; i < 4; ++i){ int nx = t1.x + dir[i][0]; int ny = t1.y + dir[i][1]; if(!check(nx,ny)) continue; t2.x = nx; t2.y = ny; t2.s = t1.s+1; //下一位置是楼梯时候才考虑 否则正常走 //有楼梯时********************************* if(map[nx][ny] == '|' || map[nx][ny] == '-'){ if(t2.s%2 == 0){ //这里为偶数步时候 其之前一步的楼梯应该反向 if(map[nx][ny] == '|') c = '-'; else if(map[nx][ny] == '-') c = '|'; } else c = map[nx][ny]; t2.x += dir[i][0]; t2.y += dir[i][1]; if(!check(t2.x,t2.y)) continue; if(c == '|' && (dir[i][1] == 1 || dir[i][1] == -1) || c == '-'&& (dir[i][0] == 1 || dir[i][0] == -1)){ t2.s += 1;//c表示到达楼梯前一步时候楼梯所处的状态 } //如果人沿着x(上下)走 楼梯横着 等待 //人沿着y(左右)走 楼梯 竖着等待 } //*********************************** vis[t2.x][t2.y] = 1; pq.push(t2); } }//end OF while return -1; }int main() { while(scanf("%d%d",&n,&m)!=EOF){ for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ cin >> map[i][j]; if(map[i][j] == 'S'){ sx = i; sy = j; } } } memset(vis,0,sizeof(vis)); int ans = bfs(sx, sy); printf("%d\n",ans); } return 0; }

转载地址:http://jmimi.baihongyu.com/

你可能感兴趣的文章
【屌丝程序的口才逆袭演讲稿50篇】第七篇:请留意我们身边的风景 【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第八篇:坚持的力量 【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第九篇:春节那些事-过年回家不需要理由【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十一篇:马云乌镇40分钟演讲实录【张振华.Jack】
查看>>
Java并发编程从入门到精通 张振华.Jack --我的书
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十二篇:世界上最快的捷径【张振华.Jack】
查看>>
Android中Java代码和XML布局效率问题
查看>>
android TextView属性大全(转)
查看>>
Conclusion for Resource Management
查看>>
Conclusion for Constructors,Destructors,and Assignment Operators
查看>>
Conclusion for Accustoming Yourself to C++
查看>>
面试题1:赋值运算函数(offer)
查看>>
Mark : MessagePack简介及使用
查看>>
Mark : hive文件存储格式
查看>>
mark : hadoop 四种压缩格式
查看>>
All Things OpenTSDB
查看>>
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>
装饰模式(Decorator),迭代器模式(Iterator),组合模式(composite)
查看>>