N - Find a way
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; struct node{int x, y, op;};//op等于0的时候代表是Y,1代表M
char G[maxn][maxn];
int v[maxn][maxn][];
int M, N;
int dir[][] = { {,},{,},{,-},{-,} }; int Bfs(node s, node q)
{
queue<node> Q;
int Min = oo;
Q.push(s), Q.push(q);
v[s.x][s.y][s.op] = v[q.x][q.y][q.op] = ; while(Q.size())
{
s = Q.front();Q.pop(); if(G[s.x][s.y] == '@' && v[s.x][s.y][] && v[s.x][s.y][])
Min = min(Min, v[s.x][s.y][]+v[s.x][s.y][]); for(int i=; i<; i++)
{
q = s;
q.x += dir[i][], q.y += dir[i][]; if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y] != '#' && v[q.x][q.y][q.op]==)
{
v[q.x][q.y][q.op] = v[s.x][s.y][s.op] + ;
Q.push(q);
}
}
} return Min-;
} int main()
{
while(scanf("%d%d", &M, &N) != EOF)
{
node s, q;
int i, j; for(i=; i<M; i++)
{
scanf("%s", G[i]);
for(j=; j<N; j++)
{
if(G[i][j] == 'Y')
s.x = i, s.y = j, s.op=;
if(G[i][j] == 'M')
q.x = i, q.y = j, q.op=;
}
} memset(v, , sizeof(v)); int ans = Bfs(s, q); printf("%d\n", ans*);
} return ;
}
随机推荐
- Nginx和Apache共存环境下apache获得真实IP
自从Nginx出现以后,我们都喜欢让 Nginx 跑在前方处理静态文件,然后通过 proxy 把动态请求过滤给 apache.这么有个问题,跑在后方 apache 上的应用获取到的IP都是Nginx所 ...
- [转]Delphi调用cmd的两种方法
delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...
- 226. Invert Binary Tree(C++)
226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- iOS 获取项目名称及版本号
可用于版本让用户手动检测是否有版本更新可用. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFSho ...
- 使用BeanUtils组件
使用BeanUtils组件 前提 1:导入commons-beanutils-1.8.3.jar //根据 本人使用的是1.8.3的版本 2:导入日志包 //就是loggin ...
- ios开发之xcode6中如何添加pch全局引用文件
xcode6中去掉了默认添加pch文件,这就需要我们自己手动添加pch文件了,添加pch文件是为了一些琐碎的头文件引用,加快编译速度! 下面就说下该如何手动添加pch文件: 1.添加一个文件,在oth ...
- select控件变成可输入状态
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php基础知识【函数】(8)xml和变量函数
一.XML函数 参数类型 data --string,需要解析的数据集. parser --resource,一个指向要取得字节索引的 XML 解析器的引用. 1.创建和释放XMl解析器 ...
- python之json
import json import requests re = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=成都') re.en ...
- oe 仓库管理
需求情景: 销售电商, 其中有些产品 为代理销售(公司不管理库存,建立SO后直接由对应的供应商发货即可) 解决方案: SO 生成 DO 时候 , 源库存的取得逻辑 SO-->SHO ...