博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2354(bfs求最短路)
阅读量:4630 次
发布时间:2019-06-09

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

题目链接:

思路:初始化step[][]==inf,然后如果当前点p.step<step[p.x][p.y],则入优先队列。

1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 #define inf 1<<30 8 struct Node{ 9 int x,y,step;10 bool operator < (const Node &p) const {11 return p.step
Q;24 Node p,q;25 for(int i=1;i<=m;i++){26 p.x=1,p.y=i,p.step=1;27 Q.push(p);28 }29 while(!Q.empty()){30 p=Q.top();31 Q.pop();32 if(p.x==n)return p.step;33 for(int i=0;i<4;i++){34 q.x=p.x+dir[i][0];35 q.y=p.y+dir[i][1];36 q.step=p.step;37 if(q.x<1||q.x>n||q.y<1||q.y>m)continue;38 if(map[q.x][q.y]!=map[p.x][p.y])q.step++;39 if(Step[q.x][q.y]>q.step){40 Step[q.x][q.y]=q.step;41 Q.push(q);42 }43 }44 }45 return 1;46 }47 48 int main(){49 // freopen("1.txt","r",stdin);50 int _case;51 scanf("%d",&_case);52 while(_case--){53 scanf("%d%d",&n,&m);54 for(int i=1;i<=n;i++)55 scanf("%s",map[i]+1);56 int ans=bfs();57 printf("%d\n",ans);58 }59 return 0;60 }
View Code

 

转载于:https://www.cnblogs.com/wally/archive/2013/06/03/3114960.html

你可能感兴趣的文章
SQL性能优化没有那么神秘
查看>>
【转载】“error LNK1169: 找到一个或多个多重定义的符号”的解决方法
查看>>
网格的铺设问题——骨牌
查看>>
关于二级菜单的问题
查看>>
jenkins-svn配置
查看>>
spring-listener&spring-task注解版本
查看>>
centos7安装mongodb3.4
查看>>
Python学习【第七篇】基本数据类型
查看>>
Java 插入排序
查看>>
restTemplate工具类
查看>>
如何修改配置以修复ThinkPad 小红帽滚轮失效?
查看>>
metasploit-smb扫描获取系统信息
查看>>
表达式处理
查看>>
三国谋士智商前20名
查看>>
mysql优化1
查看>>
在C#中怎样推断线程当前所处的状态
查看>>
H5项目常见问题汇总及解决方案
查看>>
C#设置本地网络(DNS、网关、子网掩码、IP)
查看>>
Oracle数据库查看表空间是否为自增的
查看>>
asp.net图片浏览器效果
查看>>