【HDOJ】2645 find the nearest station
裸BFS。
/* 2645 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 200 typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; int n, m;
queue<node_t> Q;
char map[MAXN][MAXN];
int dist[MAXN][MAXN];
int dir[][] = {
-,,,,,-,,
}; void bfs() {
int i, j, k;
int x, y;
node_t nd; while (!Q.empty()) {
nd = Q.front();
Q.pop();
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (x< || x>=n || y< || y>=m || map[x][y]=='')
continue;
map[x][y] = '';
dist[x][y] = dist[nd.x][nd.y] + ;
Q.push(node_t(x, y));
}
}
} int main() {
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d", &n, &m) != EOF) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == '') {
Q.push(node_t(i, j));
dist[i][j] = ;
}
}
}
bfs();
for (i=; i<n; ++i) {
printf("%d", dist[i][]);
for (j=; j<m; ++j)
printf(" %d", dist[i][j]);
printf("\n");
}
} return ;
}
【HDOJ】2645 find the nearest station的更多相关文章
- hdu 2645 find the nearest station
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2645 find the nearest station Description Since dande ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- ARCGIS二维三维导航
在使用代码前需要先安装arcgis10.0 或者10.1都可以 不过本人建议初学者安装10.0比较容易安装.. 安装方式和二维三维地图的加载网上都有,就不在此一一赘述了. 先从基本的功能开 ...
- xslt语法之---运算符号
<xsl:param name="count">12</xsl:param > <xsl:template match="/" ...
- PHP错误类型及屏蔽方法
1. 注意(Notices)这些都是比较小而且不严重的错误,比如去访问一个未被定义的变量.通常,这类的错误是不提示给用户的,但有时这些错误会影响到运行的结果. 2. 警告(Warnings)这就是稍微 ...
- gridview实现表格编辑功能
.net开发者对gridview应该非常熟悉,数据展现.数据编辑.数据删除,这些gridview都能很好的完成.最近在做东西的时候遇到 这样的一个需求,就是利用gridview实现类似Excel一样直 ...
- IIS注册.net框架及temp文件权限开放
配置好IIS服务之后,在浏览器打开localhost:xx[端口号],提示:无法识别属性 targetFramework=4.0 这是因为项目是4.0框架,配置IIS时,在应用程序池没有选择.net ...
- a标签的背景图在ie8下显示问题
今天遇到个小问题,纠结了很久,分享下 a标签添加背景图,需要给a添加display:block样式 但是在ie8下还是不能显示背景图,开始以为是由于a标签为空造成的,试了下添加内容也没用,后来注意到一 ...
- PreferenceFragment界面透明问题
PreferenceFragment界面默认是透明的 而其布局代码框架为 <PreferenceScreen> ... </PreferenceScreen>,背景色及透明度属 ...
- Android Parcelable Trans byte[]
思路: http://stackoverflow.com/questions/10898116/make-custom-parcelable-containing-byte-array 谢谢, 这位外 ...
- maven项目下tomcat直接启动不了(LifecycleException)。报错如下截图
经查,tomcat项目下的lib中没有jar包,发布的时候没有将jar包发布上去.这个问题在我的博客中以前遇到过.如何将maven的jar发布到项目中,我的博客里面有记载
- 用sed、awk、grep同时匹配多个条件(与模式、或模式)
同时匹配ABC 和 123:sed -n '/ABC/{/123/p}' awk '/ABC/&&/123/{ print $0 }' grep -E '(ABC.*123|123.* ...