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 ;
}
随机推荐
- wpf 大控件 打印 将控件转成 xps格式 并分页打印
//PayRollPrintList:要打印的 list 可换成自己要打印的类型 private List<PayRoll> _PayRollPrintList = new List< ...
- 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- 段落排版--中文字间距、字母间距(letter-spacing, word-spacing)
中文字间隔.字母间隔设置: 如果想在网页排版中设置文字间隔或者字母间隔就可以使用 letter-spacing 来实现,如下面代码: h1{ letter-spacing:50px; } ... ...
- 数字证书文件cer和pfx的区别
作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...
- Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志
一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...
- maven+jetty项目在tomcat部署
步骤1:项目打包 clean install 步骤二:拷贝war 包到tomcat下 步骤三:修改server.xml文件的端口 步骤四:启动tomcat,注意jetty的项目是不需要带项目名的,To ...
- Singleton 模式
个人认为 Singleton 模式是设计模式中最为简单.最为常见.最容易实现,也是最应该熟悉和掌握的模式.且不说公司企业在招聘的时候为了考察员工对设计的了解和把握,考的最多的就是 Singleton ...
- php之图片处理类缩略图加水印
用到两个image系统函数 imagecopymerge — 拷贝并合并图像的一部分 imagecopyresampled — 重采样拷贝部分图像并调整大小 /* 如何知道图片的大小和类型 无法确认调 ...
- nginx 编译选项
内容有些多,一眼看来难免头昏脑胀,但坚持看完,相信你一定会有所收获. nginx参数: --prefix= 指向安装目录 --sbin-path 指向(执行)程序文件(nginx) --conf-pa ...
- 解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
重新启动服务器,访问web服务发现无法浏览,登陆服务器之 后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr ...