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 ;
}
随机推荐
- EntityFramework在root目录web.config中的配置设置
未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...
- 进程识别号(PID)的理解
PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...
- AppStore上架规则
1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...
- python中如何使用help命令?
python下 help()使用方法 查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules ...
- php 备份数据库脚本
<?php// 备份数据库$host = "localhost";$user = "root"; //数据库账号$password = "123 ...
- javascript——面向对象程序设计(4)
<script type="text/javascript"> //1.继承 //2.原型链 //3.借用构造函数 //4.组合继承 //5.原型式继承 //6.寄生式 ...
- mysql锁死的现象判断
一般发生表锁死这种低级问题,就有两种情况:1.程序员水平太菜,2.程序逻辑错误. 一旦发生系统会出现超时,关键是有可能你看不到正在活动的php进程,而系统的慢查询日志也不会记录,只能通过show fu ...
- (转载)css垂直水平居中的整理
方法一 .demo1 { width:180px; height:180px; line-height:180px; *font-size:160px; border:1px solid #ddd; ...
- js事件处理 —— 详解
对于JS事件处理分为四部分: 1.html事件处理程序 直接添加到HTML结构中 解析:用html处理程序可以直接在button元素里直接调用,但是维护性不是很方便 <!DOCTYPE html ...
- WdatePicker.js 日期时间插件
支持功能: 1.支持常规在input单击或获得焦点时调用,还支持使用其他的元素如:<img><div>等触发WdatePicker函数来调用弹出日期框 @1.input 调用: ...