ECNU1104-BFS
简单的BFS
题意:多起点多终点

/*
简单的BFS
题意:多起点多终点
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = ;
const int inf = 0x3f3f3f3f; int dis[ maxn ][ maxn ];
char mat[ maxn ][ maxn ];
const int dx[]={,,,-};
const int dy[]={,-,,}; queue<int>q; void init(){
memset( dis,0x3f,sizeof( dis ) );
while( !q.empty() )
q.pop();
} bool in( int x,int y,int n,int m ){
if( x>=&&x<n && y>=&&y<m ) return true;
else return false;
} void bfs( /*int x,int y,*/int n,int m ){
/*int curx = x;
int cury = y;
int curt = 0;
q.push( curx );
q.push( cury );
q.push( curt );*/
while( !q.empty() ){
int curx = q.front();q.pop();
int cury = q.front();q.pop();
int curt = q.front();q.pop();
for( int i=;i<;i++ ){
int nx = curx+dx[i];
int ny = cury+dy[i];
int nt = curt + ;
if( in( nx,ny,n,m ) && dis[ nx ][ ny ]>nt ){
dis[ nx ][ ny ] = nt;
q.push( nx );
q.push( ny );
q.push( nt );
}
}
}
return ;
} int main(){
int n,m;
//freopen( "in.txt","r",stdin );
while( scanf("%d%d",&n,&m)== ){
init();
for( int i=;i<n;i++ ){
scanf("%s",mat[i]);
for( int j=;j<m;j++ ){
if( mat[ i ][ j ]=='' ){
dis[ i ][ j ] = ;
q.push( i );
q.push( j );
q.push( );
}
}
}
bfs( n,m );
for( int i=;i<n;i++ ){
for( int j=;j<m;j++ ){
if( j== ) printf("%d",dis[ i ][ j ]);
else printf(" %d",dis[ i ][ j ]);
}
printf("\n");
}
}
return ;
}
ECNU1104-BFS的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
- ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)
//POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...
随机推荐
- Contoso 大学 - 使用 EF Code First 创建 MVC 应用
原文 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 Contoso 大学 Web 示例应用演示了如何使用 EF 技术创建 ASP.NET MVC 应用.示例中的 Co ...
- Cocos2d-x中背景音乐播放暂停与继续
背景音乐播放暂停与继续似乎我们很少使用,事实上也正是如此,背景音乐播放暂停与继续实例代码如下: SimpleAudioEngine::getInstance()->pauseBackground ...
- C#中Excel的导入和导出的几种基本方式
在上一篇(http://www.cnblogs.com/fengchengjushi/p/3369386.html)介绍过,Excel也是数据持久化的一种实现方式.在C#中.我们常常会与Excel文件 ...
- Ajax和JSON基础
Ajax (核心是XMLHttpRequest对象) 1.XMLHttpRequest对象: request=new XMLHttpRequest() 支持Firefox opera Safari ...
- 函数 sort,unique,stable_sort,count_if,谓词
bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...
- memset与NULL区别
memset与NULL区别 NULL与0 的区别 为什么强调一个malloc对应一个free 在一个结构体malloc,然后free,但是她的成员变量的malloc并没有free,还需要特别的free ...
- Qt自定义菜单项
经常会看到一些菜单的部分项是由几个按钮组成的,如酷狗.QQ.360都有类似菜单,对于常规的菜单项,图标 + 文字 实现一个事件,很容易完成,那么怎么自定义菜单项呢? Qt提供了支持,就是利用QWidg ...
- Trie树入门及训练
什么叫Trie树? Trie树即字典树. 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本 ...
- Apache 编译安装
# wget http://www.apache.org/dist/httpd/httpd-2.2.9.tar.gz (此处我是直接用的下载好的包) # tar -zxvf httpd-2.2.9. ...
- asp.net导出Excel 按照预定格式,以及解决导出乱码
protected void ToExcel() { //新建一个Gridview,原因:避免当前窗口GridView外层没有直接跟form标签,从而避免“gridview1未包含在run='serv ...