BFS。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define INF 0x3fffffff typedef struct node_st {
int x, y, s;
node_st() {}
node_st(int xx, int yy, int ss) {
x = xx; y = yy; s = ss;
}
} node_st; char visit[][];
char map[][];
int step[][][];
int n, m;
int direct[][] = {{-,},{,},{,-},{,}}; void bfs(int x, int y, int sel) {
int i, s;
queue<node_st> que;
node_st node; memset(visit, , sizeof(visit));
memset(step[sel], , sizeof(step[sel]));
visit[x][y] = ;
que.push(node_st(x,y,)); while ( !que.empty() ) {
node = que.front();
que.pop();
s = node.s + ;
for (i=; i<; ++i) {
x = node.x + direct[i][];
y = node.y + direct[i][];
if (x< || x>=n || y< || y>=m)
continue;
if (visit[x][y])
continue;
visit[x][y] = ;
if (map[x][y] == '#')
continue;
if (map[x][y] == '@')
step[sel][x][y] = s;
que.push(node_st(x,y,s));
}
}
} int main() {
int i, j, tmp, min;
int yx, yy, mx, my; while (scanf("%d %d%*c", &n, &m) != EOF) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == 'Y') {
yx = i;
yy = j;
} else if (map[i][j] == 'M') {
mx = i;
my = j;
}
}
}
bfs(yx, yy, );
bfs(mx, my, );
min = INF;
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
if (step[][i][j] && step[][i][j]) {
tmp = step[][i][j] + step[][i][j];
if (tmp < min)
min = tmp;
}
}
}
printf("%d\n", min*);
} return ;
}

【HDOJ】2612 Find a way的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

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

  2. 【HDOJ】【3506】Monkey Party

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

  3. 【HDOJ】【3516】Tree Construction

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

  4. 【HDOJ】【3480】Division

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

  5. 【HDOJ】【2829】Lawrence

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

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

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

  7. 【HDOJ】【3530】Subsequence

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

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

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

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. OLEDB读取EXCEL表格时,某些字段为空,怎么办?

    转载:http://blog.sina.com.cn/s/blog_53864cba01011cbn.html   前些日子,写了一个Excel导入数据库的共同Batch,突然有一天发现当我修改Exc ...

  2. 20151214 jquery插件代码备份

    ;(function ($) { //局部性的 /*$.fn.extend({ 'nav' : function (color) { $(this).find('.nav').css({ 'list- ...

  3. Swift 语法须知

    什么是swift? swift是 2014 WWDC 发布的一款脚本语言. 使用Swift的好处: OC ARC    最大的困难  内存管理 而  swift  不用担心内存方面.   简洁 ,功能 ...

  4. javascript 基础2第12节

    1. <html> <head> <title>javascript基础</title> </head> <body> 1.Nu ...

  5. javascript格式化指定的日期对象

    /* * 格式化Date对象为:“2015-04-17 10:20:00” * var dateObj = new Date(); */ function formartDate(dateObj){ ...

  6. 基于SSM框架的简易的分页功能——包含maven项目的搭建

    新人第一次发帖,有什么不对的地方请多多指教~~ 分页这个功能经常会被使用到,我之前学习的时候找了很多资源,可都看不懂(笨死算了),最后还是在朋友帮助下做出了这个分页.我现在把我所能想到的知识 做了一个 ...

  7. HOW TO: Creating your MSI installer using Microsoft Visual Studio* 2008

    Quote from: http://software.intel.com/en-us/articles/how-to-creating-your-msi-installer-using-visual ...

  8. 服务器卡死,重启报错: INFO: task blocked for more than 120 seconds

    问题:服务器负载很高,但是CPU利用率不高.服务器经常夯住,网站打不开,SSH连接非常不稳定,输入命令夯住. 重启服务器报错: INFO: task blocked for more than 120 ...

  9. (转) sphinx 高亮显示搜索词

    http://hi.baidu.com/tewuapple/item/7a7bc34adbda24a8df2a9fe5  (转)

  10. 点击listview 的列头对其item进行自动排序

    若要自定义排序顺序,必须编写一个实现 IComparer 接口的类,并将 ListViewItemSorter 属性设置为该类的一个对象.当设置 ListViewItemSorter 属性值时,将自动 ...