Find a way

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

AC代码:
#include<stdio.h>
#include<memory.h>
int step[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char map[201][201];
int z[201][201],v[201][201],Y_x,Y_y,M_x,M_y,n,m;
struct none
{
    int x,y;
    int count;
}q[40001];
void BFS(int x,int y)
{
    int i;
    int fornt,end,tempx,tempy,count;
    memset(z,0,sizeof(z));
    fornt=1;end=1;
    q[end].x=x;
    q[end].y=y;
    q[end].count=0;
    end++;
    while(fornt!=end)
    {
        for(i=0;i<4;i++)
        {
            tempx=q[fornt].x+step[i][0];
            tempy=q[fornt].y+step[i][1];
            if(tempx>=0&&tempx<n&&tempy>=0&&tempy<m)
            {
                if(map[tempx][tempy]!='#'&&z[tempx][tempy]==0)
                {
                    q[end].x=tempx;
                    q[end].y=tempy;
                    q[end].count=q[fornt].count+1;
                    end++;
                    z[tempx][tempy]=1;
                    if(map[tempx][tempy]=='@')
                    {
                        v[tempx][tempy]+=q[end-1].count;
                    }
                }
            }
        }
        fornt++;
    }
}
int main()
{
    freopen("IO.txt","r",stdin);
    int i,j,min;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='Y')
                {
                    Y_x=i;
                    Y_y=j;
                }
                if(map[i][j]=='M')
                {
                    M_x=i;
                    M_y=j;
                }
            }
        min=999999999;
        memset(v,0,sizeof(v));
        BFS(Y_x,Y_y);
        BFS(M_x,M_y);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                if(min>v[i][j]&&v[i][j]!=0)
                    min=v[i][j];
            }
        printf("%d/n",min*11);
    }
    return 0;
}

解题思路:
此题要用求出最短的路程,可以用广搜,因为有多个KFC,所以难以知道怎么才是最短的,可以先把Y去每个KFC的路程求出,存在V数组中,然后把M去每个KFC的路程求出,加到V数组中,这样只要遍历V就可以得出最短的路径了,两次广搜加遍历

Find a way hdu2612相关推荐

  1. HDU2612 Find a Way BFS

    题意: yifenfei和merceki要去KFC聚会,给出一个地方的地图,n*m,有若干个个KFC,然后他们每走一步需要11分钟(注意:这里时间不能重叠的,比如yi走了一步,me也是走了一步,则一共 ...

  2. HDU-2612 Find a way

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  3. HDU2612(BFS算法)

    Problem Descrption Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Le ...

  4. hdu2612——Find a way

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  5. Hdu2612 Find a way

    Find a way Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  6. 22.11.27补卡 HDU-2612 bfs

    这题dfs没法写, 因为dfs没法处理最优解的情况 分别记录每个人走每一个kfc的最短路径 然后一个双重循环搜最小值就好了 初始化记得初始化为最大值, 不然有可能影响最min取值 ps: 这题我用st ...

  7. 【HDU - 2612】Find a way(bfs)

    -->Find a way 直接上Chinese  Descriptions: hsj和lsh最近迷上了pokemon go的游戏.在双十一大物期中考试来临之前,他们想抓一只稀有土拨鼠来攒攒人品 ...

  8. ACM训练计划建议(转)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  9. ACM的分类训练题集

    1.数论 大概有素数测试(筛法),扩展欧几里得算法,同余模运算,高斯消元,中国剩余定理,莫比乌斯反演等等. 我不擅长这方面(数学烂,还好后期团队里有两位数学大神),不发表评论. 推荐题目: 同余模运算 ...

最新文章

  1. 重磅直播|多模态融合SLAM技术分享!
  2. 【java】File的使用:将字符串写出到本地文件,大小0kb的原因
  3. php中的构造函数和析构函数,php 中构造函数和析构函数
  4. 知识图谱 ppt_PPT|知识图谱的关键技术及其智能应用
  5. 我的MAXSCRIPT笔记
  6. 数据结构学习之选择排序
  7. 【物理笑话】学过物理的人才能看懂的笑话,你能看明白几个?
  8. Lambda表达式和流API:基本示例
  9. 常见数据结构List之LinkedList
  10. HDU 1117 免费馅饼 二维动态规划
  11. CTO用“汉德公式”来定责,我大写的服!
  12. 【游戏】基于matlab GUI音乐时钟设计【含Matlab源码 1104期】
  13. 机械设计基础复习重点
  14. 《价值投资 从看懂财务报表开始》 读书笔记
  15. 2022N1叉车司机题库及在线模拟考试
  16. Bundle adjustment学习
  17. 正北坐标系和车辆坐标系下的heading转换
  18. 2019 徐州 icpc 树状数组套线段树 H - Yuuki and a problem
  19. python如何创建excel文件_python创建Excel文件数据的方法
  20. 常用sql语句(备忘)

热门文章

  1. 站台「亚马逊云科技中国峰会」,我成了「开发者大讲堂」演讲嘉宾~
  2. 如何批量将PDF转换为图片?
  3. Qt笔记(三十七)之解决QWebSocket收发中文乱码问题
  4. 数据库事务、隔离级别及其应用
  5. java 通配符_Java中的通配符匹配
  6. 问: 主管难做 该如何管理不听话的老员工?
  7. Flutter集成Unity
  8. python airflow_airflow的使用方法
  9. UnityShader入门精要——Unity中的渲染优化技术(二)
  10. 媒体查询在html中怎么写,HTML5中的媒体查询