(简单) 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;
}

时间: 2024-08-24 02:19:51

(简单) HDU 2612 Find a way,BFS。的相关文章

HDU 2612 Find a way bfs 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两人最大时间最小才对 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int inf=0x3fffffff; char maz[300][301]; int n,

HDU 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): 6251    Accepted Submission(s): 2081 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.

BFS(最短路) HDU 2612 Find a way

题目传送门 1 /* 2 BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 19:36:36 7 File Name :HDOJ_2612.cpp 8 ************************************************

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 -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. ....# ..... @.... 小

HDU 2102 A计划 双层BFS

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位

HDU 1253 胜利大逃亡(BFS)

#include <iostream> #include <cstdlib> #include <cstdio> #include <queue> #include <cstring> using namespace std; struct node{ int x,y,z,step; }; int ma[51][51][51]; int A,B,C,T; int mv[6][3] = {{1,0,0},{0,1,0},{0,0,1},{-1,0,

hdu 1885 Key Task (三维bfs)

题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候  有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到达目标点的路径 就是最小的伤害,因为每一个点的伤害是 不一样的, 这种情况要用优先队列优化, 对伤害优化. 题意:*开始, X出口, b, y, r, g 代表钥匙,分别可以开B, Y, R, G颜色的门, 钥匙可以多次使用.问最短的步骤. 思路:vis[][][]数组开三维,第三维记录状态 是否拿

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an