Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.  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. 

InputThe input contains multiple test cases.  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 OutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.Sample Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66 开始的时候想都没想就先找出所有的中间点,然后计算每个中间点,Y和M到他们的最短距离,然后求出最短的。
果然超时
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[maxn],vis[][];
char mapn[][];
struct point{
int x,y;
};
point p[maxn];
struct node{
int x,y,step;
};
void bfs(int a,int b,int c,int d,int cnt){
node p;
p.x = a,p.y = b,p.step = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
if(tmp.x == c && tmp.y == d){
sum[cnt] += tmp.step;
return;
}
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
memset(sum,,sizeof(sum));
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
if(mapn[i][j] == '@'){
p[num].x = i,p[num].y = j,num++;
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y' || mapn[i][j] == 'M'){
for(int k=;k<num;k++){
memset(vis,,sizeof(vis));
bfs(i,j,p[k].x,p[k].y,k);
}
}
}
}
sort(sum,sum+num);
cout << sum[] * << endl;
}
return ;
}

后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
#define inf 0x1f1f1f1f
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[][][],vis[][],z;
char mapn[][];
struct node{
int x,y,step;
};
void bfs(int a,int b){
memset(vis,,sizeof(vis));//在这里重置vis数组
node p;
p.x = a,p.y = b,p.step = ;
vis[p.x][p.y] = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
sum[tp.x][tp.y][z] = tp.step;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
sum[i][j][] = inf;
sum[i][j][] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
}
}
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y'){
z = ;
bfs(i,j);
}
if(mapn[i][j] == 'M'){
z = ;
bfs(i,j);
}
}
}
int ans = inf;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == '@'){
ans = min(ans,sum[i][j][] + sum[i][j][]);
}
}
}
cout << ans* << endl;
}
return ;
}

[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  2. [kuangbin带你飞]专题一 简单搜索(回顾)

    A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...

  3. [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

    //Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...

  4. [kuangbin带你飞]专题一 简单搜索 棋盘问题

    题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...

  5. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  6. [kuangbin带你飞]专题一 简单搜索 回顾总结

    第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队

  7. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  8. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  9. Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

随机推荐

  1. rocksdb编译步骤——Java、Golang、mac

    如果不是必要不建议自己编译rocksdb,编译的过程比较耗时费力.现在已经有很多编译好的文件可供使用. Java <!-- https://mvnrepository.com/artifact/ ...

  2. abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十二)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  3. Android活动(Activity)创建及生命周期

       Activity是Android的门面,可以与用户进行互动的重要模块,凡是在应用中可以看到的东西,都是放在活动中的.   在学习新的技术时,我喜欢将需要学习的技术与自己懂得技术进行类似比较,而活 ...

  4. ext container的使用的场景

    container 是 panel 简化,他称之为容器,而panel则是面板. 如果不需要类似Ext.panel.Panel,Ext.window.Window和Ext.tab.Panel 等功能,则 ...

  5. maven 打包并导出 lib 第三方jar

    一. maven 导出lib 包 执行命令 mvn dependency:copy-dependencies -DoutputDirectory=target/lib 或者在 eclipse 中执行, ...

  6. 第二十二章 跳出循环-shift参数左移-函数的使用 随堂笔记

    第二十二章 跳出循环-shift参数左移-函数的使用 本节所讲内容: 22.1 跳出循环 22.2 Shift参数左移指令 22.3 函数的使用 22.4 实战-自动备份mysql数据库和nginx服 ...

  7. NLP(十四)自制序列标注平台

    背景介绍   在平时的NLP任务中,我们经常用到命名实体识别(NER),常用的识别实体类型为人名.地名.组织机构名,但是我们往往也会有识别其它实体的需求,比如时间.品牌名等.在利用算法做实体识别的时候 ...

  8. 上手mongodb

    上手MongoDB MongoDB 是一个跨平台的,面向文档的数据库,如果你了解spring-data-jpa的使用, 那么恭喜你,你已经可以使用mongodb做开发了 使用这种类型的数据库还是挺方便 ...

  9. 【linux】【qt5界面】【系统托盘图标的实现】

    前言: 博主最近在做一个聊天软件,虽然技术不咋滴,但遇到点干货肯定是要跟大家分享的啦.下面就给大家分享一个qt实现程序隐藏才系统托盘的技巧. 装备: 系统:linux, qt版本:5.9.2,GCC: ...

  10. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...