hdoj 2612 find a way (两次bfs)

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6221    Accepted Submission(s): 2070

Problem Description

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.

Input

The 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

Output

For 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
[email protected]
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

Author

yifenfei

Source

奋斗的年代

一开始写成了让对没个肯德基店做一次bfs,会TLE

之后发现然对两个人分别做一次bfs就行

只是遇到肯德基店的 时候,因为不能确定这个是否是做优的,所以不return,继续搜下去.

/*************************************************************************
    > File Name: code/2015summer/searching/N.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年07月25日 星期六 13时54分49秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int N= 2E2+5;
char st[N][N];
int n,m;
int d[N][N];
int dd[N][N];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
struct node
{
    int x,y;
}kfc[N];
node M,Y;
void bfs(int xo,int yo)
{
    memset(d,-1,sizeof(d));
    queue<int>x;
    queue<int>y;
    x.push(xo);
    y.push(yo);
    d[xo][yo]=0;
    while (!x.empty())
    {
       int px=x.front();x.pop();
       int py=y.front();y.pop();
       for ( int i = 0 ; i < 4 ; i++ )
       {
         int nx = px+dx[i];
         int ny = py+dy[i];
         if (nx>=0&&nx<n&&ny>=0&&ny<m&&d[nx][ny]==-1&&st[nx][ny]!=‘#‘)
         {
              d[nx][ny]=d[px][py]+1;
             x.push(nx);
             y.push(ny);
         }
       }

    }
  //  cout<<"res1:"<<res<<endl;
}
void bfs2(int xo,int yo)
{
    memset(dd,-1,sizeof(dd));
    queue<int>x;
    queue<int>y;
    x.push(xo);
    y.push(yo);
    dd[xo][yo]=0;
    while (!x.empty())
    {
      int px = x.front();x.pop();
      int py =y.front();y.pop();
      for ( int i = 0 ;  i <4 ; i++ )
      {
        int nx = px +dx[i];
        int ny = py +dy[i];
        if (nx>=0&&nx<n&&ny>=0&&ny<m&&dd[nx][ny]==-1&&st[nx][ny]!=‘#‘)
        {
            dd[nx][ny]= dd[px][py]+1;
            x.push(nx);
            y.push(ny);
        }
      }
    }

}

int main()
{
    while (scanf("%d %d",&n,&m)!=EOF)
    {
      for ( int i = 0 ; i < n ; i++ )
      {
        cin>>st[i];
      }
      int k = 0;
      for ( int i = 0 ;  i < n ; i++)
      {
        for ( int j = 0 ; j < m ; j++ )
        {
            if (st[i][j]==‘Y‘)
            {
              Y.x=i;
               Y.y=j;
            }
            if (st[i][j]==‘M‘)
            {
               M.x=i;
              M.y=j;
             }
        }
      }
      bfs(Y.x,Y.y);
      bfs2(M.x,M.y);
      int ans = 99999999;
      for ( int i =  0 ; i < n ; i++ )
      {
        for ( int j = 0 ;  j < m ; j++ )
        {
            if (st[i][j]==‘@‘)
            {
             // cout<<d[i][j]<<"  "<<dd[i][j]<<endl;
              if (d[i][j]==-1||dd[i][j]==-1) continue;
              ans = min(ans,d[i][j]+dd[i][j]);
            }
        }
      }

      cout<<ans*11<<endl;
    }

    return 0;
}
时间: 2024-10-16 19:29:24

hdoj 2612 find a way (两次bfs)的相关文章

Problem N HDU 2612 Find a way (两次BFS求最值)

N - Find a way Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2612 Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei hav

hdu-2612 两次bfs

http://acm.hdu.edu.cn/showproblem.php?pid=2612 两次bfs, 记录到每个KFC的最短时间.选取最短时间. #include <stdio.h> #include <iostream> #include <string> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #inc

uva11624 - Fire! 两次bfs

题目链接 Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the

HDU 2612 -Find a way (注重细节BFS)

主题链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了.今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差点儿相同.仅仅是这题有个小坑,须要注意 1.Y不能经过M.M不能经过Y.也就是说有Y和M的格子,能够默觉得是墙 2.必须是Y和M都能到达的KFC才行,仅仅是当中一个到达不行 比例如以下列数据:答案既不是22 也不是 88 而是110,左下角的KFC满座条件 5 5 Y..#@ ...M. ....# ..... @

求树的直径【两遍BFS】

两遍BFS.从任意一个点出发,第一遍可以找到直径的一端,从这端出发即可找到另外一端. 证明:从U点出发,到达V[画个图便清晰了] 1.如果U在直径上,则V一定是直径的一个端点. 2.如果U不在直径上.U,V线一定和直径有交点(如果没有交点,从U引一条路到直径交于U'.[反证]).有交点则V一定是直径另一端. 代码:[举例] int path(int x){ //从x出发,求直径 mem(vis,-1); while(!Q.empty()) Q.pop(); Q.push(x); vis[x]=0

URAL 1145. Rope in the Labyrinth(两次BFS啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1145 1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are paralle

UVa 1599 Ideal Path (两次BFS)

题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好做,事实用两次BFS, 第一次是倒序BFS,目的是得到从结点 i 到结点n的最短距离,然后再从第一个点开始到最后一个,要保证在查找时,每经过一点要让d值恰好减少1, 直到终点,这也是一个BFS,因为这个字典序在某个结点是一样的,所以是两个BFS,我超时了好几次,因为少写了一个vis, 一定要细心,

hihocoder#1050 : 树中的最长路(树中最长路算法 两次BFS找根节点求最长+BFS标记路径长度+bfs不容易超时,用dfs做TLE了)

#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一棵二叉树!还可以拼凑成一棵多叉树——好吧,其实就是更为平常的树而已. 但是不管怎么说,小Ho喜爱的玩具又升级换代了,于是他更加爱不释手(其实说起来小球和木棍有什么好玩的是吧= =).小Ho手中的这棵玩具树现在由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不

FZU 2196 Escape (两次BFS)

[题目链接]:click here~~ [题目大意]: Description 小明进入地下迷宫寻找宝藏,找到宝藏后却发生地震,迷宫各处产生岩浆,小明急忙向出口处逃跑.如果丢下宝藏,小明就能迅速离开迷宫,但小明并不想轻易放弃自己的辛苦所得.所以他急忙联系当程序员的朋友你(当然是用手机联系),并告诉你他所面临的情况,希望你能告诉他是否能成功带着宝藏逃脱. Input 有多组测试数据. 每组测试数据第一行是一个整数T,代表接下去的例子数.(0<=T<=10) 接下来是T组例子. 每组例子第一行是两