裸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的更多相关文章

  1. hdu 2645 find the nearest station

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2645 find the nearest station Description Since dande ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. xcode升级或者重新安装后不能编译的解决方法

    昨天由于xcode有一些问题,因此进行了重新安装,结果安装好后进行编译,没有进行任何改动的代码出现了两个fatal error 查看错误信息为什么的header has allready build, ...

  2. Restart-ServiceEx.psm1

    详细描述 利用WMI的Win32_Service类重启指定计算机上的服务. Restart-ServiceEx cmdlet 通过WMI的Win32_Service类向指定计算机(ComputerNa ...

  3. cocos2dx 的Hello world的简单分析

    Node之间的关系: 场景AppDelegate.cpp又要由导演去调用然后进行表演: // create a scene. it's an autorelease object auto scene ...

  4. WCF 用netTcpbinding,basicHttpBinding 传输大文件

    问题:WCF如何传输大文件 方案:主要有几种绑定方式netTcpbinding,basicHttpBinding,wsHttpbinding,设置相关的传输max消息选项,服务端和客户端都要设置,tr ...

  5. jquery实现很简单的DIV拖动

    今天用jquery实现了一个很简单的拖动...实现思路很简单  如下: 在thickbox 弹出层内实现拖拽DIV,那么得进行一下相对宽高的运算:必须加上相对于可见窗口宽高和弹出层宽高之间的差:    ...

  6. sql-从查询结果创建一个永久表

    语法: select x into new_tableName  from ori_tableName 例如: SELECT [site] ,[day] ,[val]/31.4 [val] into ...

  7. jdbc - 连接数据库的url

    MySql: driver:com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/database_name SQL Server 2008: ...

  8. 在iframe中获取iframe外的对象

    parent.document.getElementById("dom ID"); $($(parent.document.getElementById("video-i ...

  9. javascript基础学习(四)

    javascript之流程控制语句 学习要点: 表达式语句含义 选择语句:if.if...else.switch 循环语句:while.do...while.for.for...in 跳转语句:bre ...

  10. 显示scrollbar

    修改CSS overflow的值 overflow: 参考MDN 例子: overflow: auto or scroll