D - Find a way
D - Find a way
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
Sample Input
Sample Output
#include <iostream>
#include <queue>
#include <string.h>
using namespace std; struct point
{
int x,y;
int step;
};
point py,pm;
int m,n;
char map[][];
int test_y[][];
int test_m[][];
int min_all; void Init()
{
for (int i=;i<=m;i++)
{
for (int j=;j<=n;j++)
{
cin>>map[i][j];
if (map[i][j]=='Y')
{
py.x=i;
py.y=j;
py.step=;
}
if (map[i][j]=='M')
{
pm.x=i;
pm.y=j;
pm.step=;
}
}
}
} int check_y(point t)
{
if (t.x<=m&&t.x>=&&t.y>=&&t.y<=n&&test_y[t.x][t.y]==&&map[t.x][t.y]!='#')
return ;
return ;
} int check_m(point t)
{
if (t.x<=m&&t.x>=&&t.y>=&&t.y<=n&&test_m[t.x][t.y]==&&map[t.x][t.y]!='#')
return ;
return ;
} void bfs()
{
queue<point> Q;
point now,next; while (!Q.empty()) Q.pop();
memset(test_y,,sizeof(int)*(m+)*); now.x=py.x;
now.y=py.y;
now.step=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop(); next.x=now.x+;
next.y=now.y;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y-;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;} next.x=now.x-;
next.y=now.y;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y+;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;}
} while (!Q.empty()) Q.pop();
memset(test_m,,sizeof(int)*(m+)*); now.x=pm.x;
now.y=pm.y;
now.step=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop(); next.x=now.x+;
next.y=now.y;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y-;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;} next.x=now.x-;
next.y=now.y;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y+;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;}
}
} int main()
{
while (cin>>m>>n&&m&&n)
{
Init();
bfs(); min_all=;
for (int i=;i<=m;i++)
{
for (int j=;j<=n;j++)
{
if (map[i][j]=='@' && test_y[i][j]+test_m[i][j] < min_all&& test_y[i][j]!= && test_m[i][j]!= )//
{
min_all=test_y[i][j]+test_m[i][j];
}
}
}
cout<<min_all*<<endl;
}
return ;
}
随机推荐
- C-类型转换(陷阱)
getchar() 返回值为int类型 1.自动类型转换(运算符两边变量类型不同时) 1).两个变量类型自动转换成一样的类型(会根据参数类型自动转换, 而不是直接位转换), 且运算结果也是转换后的类型 ...
- <转>多线程中的lua同步问题
转自 http://www.cnblogs.com/ghost240/p/3526185.html 最近写paintsnow::start时出现了一个非常麻烦的BUG,程序的Release版本大约每运 ...
- asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(五)—— 解决tabs选择已建tab显示但datagrid的toolbar消失的问题
项目需要反复运行,调整bug.发现在选择已有选项卡时,虽然不需要再新建tab,直接跳转到已有的tab上,但问题是显示的datagrid有事会出现toolbar消失的问题.网上也有不少同学出现类似问题, ...
- Linux 性能监控 —— Load Average
一. 简单介绍 top. uptime. cat /proc/loadavg 命令中 Load average: 4.90, 5.51, 5.77 整体含义: 正在执行的任务数量 + 排 ...
- linux mysql 5.7.17 编译安装小记
官方网站中下载源码包: https://dev.mysql.com/downloads/mysql/ 选择下载源码包: 由于官网下载较慢,我选择使用搜狐镜像站进项下载..速度真的快的不是一星半点: 电 ...
- c++ telescoping constructor is NOT supported until c++11
Telescoping constructor: see Effective Java 2nd Edition Item 2 If you want to use telescoping constr ...
- JBoss高危漏洞分析
前言 JBoss是一个基于J2EE的开放源代码应用服务器,代码遵循LGPL许可,可以在任何商业应用中免费使用:JBoss也是一个管理EJB的容器和服务器,支持EJB 1.1.EJB 2.0和EJB3规 ...
- Ubuntu安装Sun JDK
Ubuntu 14.04 下安装 Sun JDK 1.8.0 1.下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...
- Linux HugePage特性
Linux HugePage特性 HugePage,就是指的大页内存管理方式.与传统的4kb的普通页管理方式相比,HugePage为管理大内存(8GB以上)更为高效.本文描述了什么是HugePage, ...
- 手游后台PVP系统网络同步方案总结
游戏程序 平台类型: 程序设计: 编程语言: 引擎/SDK: 概述 PVP系统俨然成为现在新手游的上线标配,手游Pvp系统体验是否优秀,很大程度上决定了游戏的品质.从最近半年上线的新手 ...