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 ;
}
随机推荐
- ural 1057 Amount of degrees 【数位dp】
题意:求(x--y)区间转化为 c 进制 1 的个数为 k 的数的出现次数. 分析:发现其满足区间减法,所以能够求直接求0---x 的转化为 c 进制中 1 的个数为k的数的出现次数. 首先用一个数组 ...
- python 页面信息抓取
1. 特点 在python 解析html这篇文章中已经做了初步的介绍,接下来再坐进一步的说明.python抓取页面信息有下面两个特点: 依赖于HTML的架构. 微小的变化可能会导致抓取失败,这取决于你 ...
- 读写锁pthread_rwlock_t的使用(转)
读写锁是用来解决读者写者问题的,读操作可以共享,写操作是排他的,读可以有多个在读,写只有唯一个在写,同时写的时候不允许读. 具有强读者同步和强写者同步两种形式 强读者同步:当写者没有进行写操作,读者就 ...
- 【Python3 爬虫】05_安装Scrapy
Scrapy简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛.框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容 ...
- 【BIEE】18_时间序列函数的使用
三个时间序列函数 AGO: 实现同环比 TO DATE:实现累计指标,如MTD月累计.YTD年累计 Period Rolling:当前时间的x个时间单位开始到y个时间单位结束这一时段内的度量总和 BI ...
- 判断一个字符串是否为合法IP
输入任意一个字符串,判断是否为合法IP bool IsIPAddress(const char * str){ //先判断形式是否合法, //检查是否只包含点和数字 ; str[i] != '\0'; ...
- MongoDB密码设置(基于windows)
参考文档:http://www.cnblogs.com/zengen/archive/2011/04/23/2025722.html MongoDB部署到Windows上后是默认是无权限限制的的. ...
- asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(二)—— easyui的简单实用
下面开始在UserManager.Web中利用easyUI构建web. 1. 先删除自带的controllers.models和views(里面的shared和web.config可以保存)下面的文件 ...
- Openlayer4 - 最好最强大的开源地图引擎
Openlayer4 - 最好最强大的开源地图引擎 # githubhttps://github.com/openlayers/openlayers # 官网http://openlayers.org ...
- webpack 通用环境快速搭建
能用babel编译es2015 . 能热编译.能加载静态资源(js/css/font/image).是一个很通用的开发环境,虽然不智能.但很好扩展 npm 安装列表: # webpack 核心 npm ...