zoukankan      html  css  js  c++  java
  • Find a way HDU

    题目链接:https://cn.vjudge.net/problem/HDU-2612

    题意:Y和M约在一家KFC碰面,问他俩到KFC的时间和 最少是多少

    思路:bfsY到每个KFC的最短时间,bfsM到每个KFC的最短时间,然后遍历每个KFC他俩到达的时间和,找到最小的即可

    注意:1.可能出现KFC被墙围起来的情况,俩人都到不了,把时间赋初值的时候赋为INF即可

              2. 除了墙的位置,他俩都可以走到 比如,Y可以经过M的位置

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <stack>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #define mem(a,b) memset(a,b,sizeof(a));
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef long long ll;
    int dir[4][2] = {0,1,0,-1,1,0,-1,0};
    const int maxn = 5000005;
    int n,m,suma,vis1[205][205],vis2[205][205],sum1[205][205],sum2[205][205];
    string s[205];
    struct node {
        int x,y,num;
        node(int x1,int y1,int n):x(x1),y(y1),num(n){};
    };
    void BFS1(int x,int y) {//找到Y到KFC的最短时间
        vis1[x][y] = 1;
        queue<node>q;
        q.push(node(x,y,0));
        int sum = 0;
        while(!q.empty()) {
            node temp = q.front();
            q.pop();
            if(s[temp.x][temp.y] == '@')
                sum++;
            if(sum == suma) {
                break;
            }
            for(int i = 0; i < 4; i++) {
                int fx = temp.x + dir[i][0], fy = temp.y + dir[i][1];
                if(fx >=0 && fx < n && fy >= 0 && fy < m && !vis1[fx][fy] && s[fx][fy] != '#')
                {
                    vis1[fx][fy] = 1;
                    sum1[fx][fy] = temp.num + 1;
                    q.push(node(fx,fy,temp.num+1));
                }
            }
        }
    }
    void BFS2(int x,int y) {//找到M到KFC的最短时间
        vis2[x][y] = 1;
        queue<node>q;
        q.push(node(x,y,0));
        int sum = 0;
        while(!q.empty()) {
            node temp = q.front();
            q.pop();
            if(s[temp.x][temp.y] == '@')
                sum++;
            if(sum == suma) {
                break;
            }
            for(int i = 0; i < 4; i++) {
                int fx = temp.x + dir[i][0], fy = temp.y + dir[i][1];
                if(fx >=0 && fx < n && fy >= 0 && fy < m && !vis2[fx][fy] && s[fx][fy] != '#')
                {
                    vis2[fx][fy] = 1;
                    sum2[fx][fy] = temp.num + 1;
                    q.push(node(fx,fy,temp.num+1));
                }
            }
        }
    }
    int main()
    {
        while(cin >> n >> m) {
            mem(vis1,0);
            mem(vis2,0);
            mem(sum1,INF);//最短时间赋为无穷大
            mem(sum2,INF);
            for(int i = 0; i < n; i++) {
                cin >> s[i];
            }
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    if(s[i][j] == '@')
                        suma++;
                }
            }
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    if(s[i][j] == 'Y')
                        BFS1(i,j);
                    if(s[i][j] == 'M')
                        BFS2(i,j);
                }
            }
            int ans = INF;
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    if(s[i][j] == '@') {//遍历每个KFC
                        ans = min(ans,sum1[i][j] + sum2[i][j]);
                    }
                }
            }
            cout << ans*11 << endl;
        }
        return 0;
    }
  • 相关阅读:
    BoundsChecker使用
    完成端口(Completion Port)详解
    VC内存泄露检查工具:VisualLeakDetector
    AcceptEx函数与完成端口的结合使用例子
    IOCP之accept、AcceptEx、WSAAccept的区别
    Visual C++ 6.0安装
    eclipse中在线安装FindBugs
    几种开源SIP协议栈对比
    全情投入是做好工作的基础——Leo鉴书39
    CheckStyle检查项目分布图
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/11377815.html
Copyright © 2011-2022 走看看