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 ...
随机推荐
- winform中文本框的一些案例
项目中经常看到在输入金额时,会加逗号,最近在复习正则表达式,就联系下,界面如下:
- swift闭包传值
不知道原理,就知道这么用的,皮毛上的那一点. 寻思着把以前的项目改成swift的,结果了,,, 反向传值 一. //类似于OC中的typedef typealias sendValueClosure= ...
- mysql5.7版本免安装配置教程
自己花了两天时间才搞清楚,希望对新手有一定帮助,有问题可以联系哦! mysql分为安装版本msi,免安装要压缩版本ZIP,下载网址:http://dev.mysql.com/downloads 免安装 ...
- PHP中常量
PHP中常量 常量就是一种特殊的变量,PHP中的常量值一旦定义,在程序运行过程中不可更改,常量本身也不允许删除. 程序是用于解决现实问题,由两部分组成:代码,数据 常量的定义: 语法1: define ...
- Layout.xml中控件的ID命名方式
控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...
- browserify总结
一.browserify 简介 browserify is a tool for compiling node-flavored commonjs modules for the browser. Y ...
- L001-老男孩教育-Python13期VIP视频-19节-pbb
L001-老男孩教育-Python13期VIP视频-19节-pbb Windows上安装 Python3开发环境 下载:www.python.org >选择Downloads>All re ...
- java 枚举 类 enum
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...
- XML美化工具及其他各种美化工具
在线工具 http://www.ostools.net/codeformat/xml 3464网页常用工具 http://www.3464.com/Tools/CodeFormat/ 在线工具大全 h ...
- RHEL 6.5升级GCC 4.9.3
前提:保证旧版的gcc,g++存在! root用户 1. 下载源码和依赖包源码:新建目录bakwget http://ftp.gnu.org/gnu/gcc/gcc-4.9.3/gcc-4.9.3.t ...