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. webform 转 MVC 飞一般的感觉

    前言: 浅谈webform与mvc,让开发变得更加简单,这里主要通过比较webform与mvc的开发方式,以下全属个人看法,不完善的地方可以留言补充. 正文: 废话不多说,直接说工作中经常用到的地方 ...

  2. Extjs combobox设置默认值

    转载:http://www.54mask.com/extjs-combobox-default-value.html 相信很多人都遇到了在ExtJS框架中设置combo组件默认值的需求,ExtJS框架 ...

  3. HTML表单元素中disabled的元素的值不会提交到服务器

    一.在HTMl页面的form表单中对disabled的元素的属性和值不会提交到服务器 实例1: <form action="#"> <input type=&qu ...

  4. java反射技术

    Class c2 = Class.forName("com.reflection.Test"); // 对类的寻找,找到一个类,注意不是对象 WifiManager mWifiMa ...

  5. 启动 XPs 代理

    Xps代理:扩展了 1 : 运行sp_configure检查代理XPs 的 值. EXEC SP_CONFIGURE 'agent xps'查看run_value 值是否为0,如果为0:需要更改此设置 ...

  6. c标准库中字符和数字转换的函数

    字符转换为数字: #include<stdlib.h> atoi();    将字符转换为整型                       例:char ch1;int i=atoi(ch ...

  7. swift入门-day01

    Swift 简介 简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 2014 年,在 Apple WWDC 发布 历史 2010 年 7 月,苹果开发者工 ...

  8. java IO文件读写例子(OutputStream,InputStream,Writer,Reader)

    一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...

  9. Codevs 1225 八数码难题

    1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的 ...

  10. Codevs 1183 泥泞的道路

    1183 泥泞的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description CS有n个小区,并且任意小区之间都有两条单向道路 ...