当前位置: 首页 > news >正文

(简单) HDU 2612 Find a way,BFS。

  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.

 

  就是求两个人到某一个KFC的最小值,这个题记得以前做的时候被坑惨了,要注意初始化为INF,以及说人的起始位置要表示为可通过。

 

代码如下:

#include<iostream>
#include<cstring>

using namespace std;

const int INF=1e7;

short map1[205][205];
int que[40005],las,fir;
int ans[205][205];
int ans1[205][205];
int N,M;
int couKFC,Si,Sj,Ei,Ej;

bool judge(int x,int y,int (*rem)[205])
{
    if(x<=0||y<=0||x>N||y>M)
        return 0;

    if(rem[x][y]!=INF)
        return 0;

    if(map1[x][y]==0)
        return 0;

    return 1;
}

void bfs(int x,int y,int (*rem)[205])
{
    las=fir=0;
    int cou=0;
    int temp,t1,t2;

    que[las++]=x*1000+y;
    rem[x][y]=0;

    while(las-fir)
    {
        temp=que[fir++];
        t1=temp/1000;
        t2=temp%1000;
        temp=rem[t1][t2];

        if(map1[t1][t2]==2)
            ++cou;

        if(cou>=couKFC)
            return;

        --t1;
        if(judge(t1,t2,rem))
        {
            rem[t1][t2]=temp+1;
            que[las++]=t1*1000+t2;
        }    
        t1+=2;
        if(judge(t1,t2,rem))
        {
            rem[t1][t2]=temp+1;
            que[las++]=t1*1000+t2;
        }    
        --t1;
        --t2;
        if(judge(t1,t2,rem))
        {
            rem[t1][t2]=temp+1;
            que[las++]=t1*1000+t2;
        }    
        t2+=2;
        if(judge(t1,t2,rem))
        {
            rem[t1][t2]=temp+1;
            que[las++]=t1*1000+t2;
        }
    }
}

int slove()
{
    bfs(Si,Sj,ans);
    bfs(Ei,Ej,ans1);

    int minn=INF;

    for(int i=1;i<=N;++i)
        for(int j=1;j<=M;++j)
            if(map1[i][j]==2)
                if(minn>ans[i][j]+ans1[i][j])
                    minn=ans[i][j]+ans1[i][j];

    return minn*11;
}

int main()
{
    ios::sync_with_stdio(false);

    char c;

    while(cin>>N>>M)
    {
        couKFC=0;

        for(int i=1;i<=N;++i)
            for(int j=1;j<=M;++j)
            {
                cin>>c;
                ans[i][j]=ans1[i][j]=INF;

                switch(c)
                {
                    case 'Y':
                        map1[i][j]=1;
                        Si=i;
                        Sj=j;
                        break;
                    case 'M':
                        map1[i][j]=1;
                        Ei=i;
                        Ej=j;
                        break;
                    case '.':
                        map1[i][j]=1;
                        break;
                    case '#':
                        map1[i][j]=0;
                        break;
                    case '@':
                        map1[i][j]=2;
                        ++couKFC;
                        break;
                }
            }

        cout<<slove()<<endl;
    }

    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whywhy/p/4229942.html

相关文章:

  • ZeroMQ接口函数之 :zmq_bind - 绑定一个socket
  • Objective - C基础: 第三天 - 1.NSString的基本认识
  • Windows Store App JavaScript 开发:页内导航
  • IOS 消息机制(NSNotificationCenter)
  • DataMatrix二维条码源码分析检测识别图像位置
  • 图像切割之(一)概述
  • 关于HTML5本地存储的sessionStorage与localStorage的简单用法
  • 论存储IOPS和Throughput吞吐量之间的关系
  • Objective - C基础: 第五天 - 6.循环引用
  • 服务器的编码
  • Jquery scrollTop animate 實現動態滾動到頁面頂部
  • 多站点IIS用户安全权限设置图解教程
  • c#将http调用返回额json中的有关中文的unicode转换为中文(转)
  • 断点续传
  • T-SQL查询进阶--变量
  • 《Java编程思想》读书笔记-对象导论
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • exports和module.exports
  • java正则表式的使用
  • jquery ajax学习笔记
  • React 快速上手 - 07 前端路由 react-router
  • 分布式任务队列Celery
  • - 概述 - 《设计模式(极简c++版)》
  • 工作手记之html2canvas使用概述
  • 排序算法之--选择排序
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 微信小程序实战练习(仿五洲到家微信版)
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 一、python与pycharm的安装
  • 一道闭包题引发的思考
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • # 数论-逆元
  • (175)FPGA门控时钟技术
  • (MATLAB)第五章-矩阵运算
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .net开发时的诡异问题,button的onclick事件无效
  • [100天算法】-不同路径 III(day 73)
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [ARC066F]Contest with Drinks Hard
  • [BZOJ4016][FJOI2014]最短路径树问题
  • [CareerCup] 13.1 Print Last K Lines 打印最后K行
  • [codevs 2822] 爱在心中 【tarjan 算法】
  • [ERROR] Plugin 'InnoDB' init function returned error
  • [linux学习]apt-get参数解析