hdu - 2645 find the nearest station (bfs水)
2015-06-02 21:04
375 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2645
找出每个点到距离最近的车站的距离。
直接bfs就好。
找出每个点到距离最近的车站的距离。
直接bfs就好。
#include <cstdio> #include <queue> #include <cstring> using namespace std; int n,m; int maze[200][200],dis[200][200],vis[200][200]; int dir[4][2]={-1,0,1,0,0,1,0,-1}; struct point { int x,y,step; }; int bfs(int a,int b) { // printf("%d %d\n",a,b); memset(vis,0,sizeof(vis)); queue<point>que; point s; s.x=a;s.y=b;s.step=0; que.push(s); vis[s.x][s.y]=1; while(!que.empty()) { point e=que.front();que.pop(); // printf("%d %d %d\n",e.x,e.y,e.step); if(maze[e.x][e.y]) return e.step; for(int i=0;i<4;i++) { s.x=e.x+dir[i][0]; s.y=e.y+dir[i][1]; if(!vis[s.x][s.y]&&s.x>=0&&s.x<n&&s.y>=0&&s.y<m) { vis[s.x][s.y]=1; s.step=e.step+1; que.push(s); } } } } int main() { // freopen("a.txt","r",stdin); char s[200]; while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) { scanf("%s",s); for(int j=0;j<m;j++) { maze[i][j]=s[j]-'0'; //printf("%d\n",maze[i][j]); } } memset(dis,0,sizeof(dis)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(!maze[i][j]) dis[i][j]=bfs(i,j); for(int i=0;i<n;i++) { for(int j=0;j<m-1;j++) printf("%d ",dis[i][j]); printf("%d\n",dis[i][m-1]); } } return 0; }
相关文章推荐
- 手机电池
- redis 模糊查询?
- python lambda表达式
- python sort and sorted
- C语言初探 之 printf压栈顺序(printf("%d %d %d %d %d %d\n",a++, ++a, a++, ++a, a++, ++a ))
- google blog (易經大意 登載)
- vim编辑器详解
- Mac电脑下配置maven环境变量
- openLTE编译cmake时出错,not found gnuradio
- Windows7下安装MongoDB
- 会员卡管理系统技术解析(十一)会员卡作废记录查询
- Android与MVC设计模式
- 实现医生工作站的处方模板功能的代码(十一)
- 谱聚类--SpectralClustering
- oracle用户密码过期的处理方法
- 在IDL中调用自定义Python Module
- Block 介绍
- 伪列 伪表
- C语言学习-01第一个C语言程序
- php 获取当前时间的毫秒数程序代码