码迷,mamicode.com
首页 > 其他好文 > 详细

[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612

时间:2017-09-16 17:21:37      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:tmp   hdu   most   iostream   output   ios   learning   clu   def   

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[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[maxn],vis[205][205];
char mapn[205][205];
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 = 0;
    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=0;i<4;i++){
            int xx = tmp.x + dx[i];
            int yy = tmp.y + dy[i];
            if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!=#){
                vis[xx][yy] = 1;
                node tp;
                tp.x = xx,tp.y = yy,tp.step = tmp.step + 1;
                q.push(tp);
            }
        }
    }
}
int main()
{
    while(cin >> n >> m){
        num = 0;
        memset(sum,0,sizeof(sum));
        if(!n || !m){
            break;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin >> mapn[i][j];
                if(mapn[i][j] == @){
                    p[num].x = i,p[num].y = j,num++;            
                }
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mapn[i][j] == Y || mapn[i][j] == M){
                    for(int k=0;k<num;k++){
                        memset(vis,0,sizeof(vis));
                        bfs(i,j,p[k].x,p[k].y,k);
                    }
                }
            }
        }
        sort(sum,sum+num);
        cout << sum[0] * 11 << endl;
    }
    return 0;
}

后面是先计算好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[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[205][205][2],vis[205][205],z;
char mapn[205][205];
struct node{
    int x,y,step;
};
void bfs(int a,int b){
    memset(vis,0,sizeof(vis));//在这里重置vis数组
    node p;
    p.x = a,p.y = b,p.step = 0;
    vis[p.x][p.y] = 1;
    queue<node> q;
    q.push(p);
    while(!q.empty()){
        node tmp = q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int xx = tmp.x + dx[i];
            int yy = tmp.y + dy[i];
            if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!=#){
                vis[xx][yy] = 1;
                node tp;
                tp.x = xx,tp.y = yy,tp.step = tmp.step + 1;
                sum[tp.x][tp.y][z] = tp.step;
                q.push(tp);
            }
        }
    }
}
int main()
{
    while(cin >> n >> m){
        num = 0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                sum[i][j][0] = inf;
                sum[i][j][1] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
            }
        }
        if(!n || !m){
            break;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin >> mapn[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mapn[i][j] == Y){
                    z = 0;
                    bfs(i,j);
                }
                if(mapn[i][j] == M){
                    z = 1;
                    bfs(i,j);
                }
            }
        }
        int ans = inf;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mapn[i][j] == @){
                    ans = min(ans,sum[i][j][0] + sum[i][j][1]);
                }
            }
        }
        cout << ans*11 << endl;
    }
    return 0;
}

 

[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612

标签:tmp   hdu   most   iostream   output   ios   learning   clu   def   

原文地址:http://www.cnblogs.com/l609929321/p/7531681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!