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. 实时同步lsyncd

    实时同步lsyncd 1 lsyncd 1.1 lsyncd 简介 Lsyncd使用文件系统事件接口(inotify或fsevents)来监视对本地文件和目录的更改.Lsyncd将这些事件整理几秒钟, ...

  2. PPT | Docker定义存储-让应用无痛运行

    编者注: 本文为9月27日晚上8点有容云平台存储架构师张朝潞在腾讯课堂中演讲的PPT,本次课堂为有容云主办的线上直播Docker Live时代●Online Meetup-第三期:Docker定义存储 ...

  3. scrapyd schedule.json setting 传入多个值

    使用案例: import requests adder='http://127.0.0.1:6800' data = { 'project':'v1', 'version':'12379', 'set ...

  4. JAVA基础——Switch条件语句

    JAVA基础——switch 条件语句 switch语句结构: switch(表达式){ case值1: 语句体1: break: case值2: 语句体2: break: case值3: 语句体3: ...

  5. 【Java笔记】【Java核心技术卷1】chapter3 D4变量

    package chapter3; public class D4变量 { public static final int BBB=100; //类常量 public static void main ...

  6. (十五)c#Winform自定义控件-键盘(二)

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

  7. Selenium Webdriver元素定位的八种常用方式【转】

    在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素.其中By类的常用定位方式共八种,现分别介绍如下 ...

  8. Spring入门(十):Spring AOP使用讲解

    1. 什么是AOP? AOP是Aspect Oriented Programming的缩写,意思是:面向切面编程,它是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 可以认为AOP是 ...

  9. N个tomcat之间实现Session共享(写的不错,转来的)

    以下文章写的比较不错,转来的. tomcat的session共享设置如此简单为什么很少人去用.这个我说的重点. 1.自身的session如果服务器不在同一个网段会有session失效(本人使用的是阿里 ...

  10. Oracle笔记_查询

    1 单条件查询 select -- from -- where 条件 -- = > >= < <= != <> -- 单引号用于数据表示字符串 -- 双引号用于数据 ...