zoukankan      html  css  js  c++  java
  • HDU 2822

    Dogs

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2208    Accepted Submission(s): 838



    Problem Description
    Prairie dog comes again! Someday one little prairie dog Tim wants to visit one of his friends on the farmland, but he is as lazy as his friend (who required Tim to come to his place instead of going to Tim's), So he turn to you for help to point out how could him dig as less as he could.

    We know the farmland is divided into a grid, and some of the lattices form houses, where many little dogs live in. If the lattices connect to each other in any case, they belong to the same house. Then the little Tim start from his home located at (x0, y0) aim at his friend's home ( x1, y1 ). During the journey, he must walk through a lot of lattices, when in a house he can just walk through without digging, but he must dig some distance to reach another house. The farmland will be as big as 1000 * 1000, and the up left corner is labeled as ( 1, 1 ).
     
    Input
    The input is divided into blocks. The first line in each block contains two integers: the length m of the farmland, the width n of the farmland (m, n ≤ 1000). The next lines contain m rows and each row have n letters, with 'X' stands for the lattices of house, and '.' stands for the empty land. The following two lines is the start and end places' coordinates, we guarantee that they are located at 'X'. There will be a blank line between every test case. The block where both two numbers in the first line are equal to zero denotes the end of the input.
     
    Output
    For each case you should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig.
     
    Sample Input
    6 6
    ..X...
    XXX.X.
    ....X.
    X.....
    X.....
    X.X...
    3 5
    6 3

    0 0
    Sample Output
    3
    Hint
    Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).
     
    Source
    /*****
    题意:给出一个矩阵,然后给出两个点,让求链接这两个点需要打的洞的最小值
              ‘.’代表空位置,‘X’代表是洞;
    做法:bfs + 优先队列
    *****/
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<cmath>
    #include<stdio.h>
    #include<queue>
    using namespace std;
    #define maxn 1100
    #define INF 1000000
    int Edge[maxn][maxn];
    char ch[maxn][maxn];
    int dx[4] = {0,0,-1,1};
    int dy[4] = {1,-1,0,0,};
    int n,m;
    int temp = 0;
    int sx,sy,ex,ey;
    int vis[maxn][maxn];
    struct Node
    {
        int x;
        int y;
        int step;
        Node()
        {
            x = 0;
            y = 0;
            step =0;
        }
    };
    struct cmp
    {
        bool operator () (const Node &a,const Node &b)
        {
            return a.step>b.step;
        }
    };
    int  check(Node a)
    {
        if(a.x >= 1 && a.x <=n && a.y>=1 && a.y <=m) return 1;
        return 0;
    }
    priority_queue<Node,vector<Node>,cmp >que;
    int bfs()
    {
        while(!que.empty()) que.pop();
        Node now,tmp;
        now.x = sx;
        now.y = sy;
        now.step = 0;
        que.push(now);
        vis[sx][sy] = 1;
        while(!que.empty())
        {
            tmp = que.top();
            que.pop();
            if(tmp.x == ex && tmp.y == ey) return tmp.step;
            for(int i=0; i<4; i++)
            {
                now.x = tmp.x + dx[i];
                now.y = tmp.y + dy[i];
                if(check(now) && vis[now.x][now.y] == 0)
                {
                    if(Edge[now.x][now.y] == 0) now.step = tmp.step;
                    else
                    {
                        now.step = tmp.step + 1;
                    }
                    que.push(now);
                    vis[now.x][now.y] = 1;
                }
            }
        }
    }
    
    int main()
    {
    //#ifndef ONLINE_JUDGE
    //    freopen("in1.txt","r",stdin);
    //#endif
        while(~scanf("%d %d",&n,&m))
        {
            if(n == 0 && m == 0) break;
            memset(Edge,INF,sizeof(Edge));
            memset(vis,0,sizeof(vis));
            for(int i=1; i<=n; i++)
            {
                scanf("%s",ch[i]);
                for(int j=0; j<m; j++)
                {
                    if(ch[i][j] == 'X') Edge[i][j+1] = 0;
                }
            }
            getchar();
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(Edge[i][j] == 0) continue;
                    Edge[i][j] = min(Edge[i-1][j],min(Edge[i+1][j],min(Edge[i][j-1],Edge[i][j+1]))) + 1;
                }
            }
            temp = 0;
            scanf("%d %d",&sx,&sy);
            scanf("%d %d",&ex,&ey);
            getchar();
            int temp = bfs();
            printf("%d
    ",temp);
        }
        return 0;
    }
  • 相关阅读:
    Unix / 类 Unix shell 中有哪些很酷很冷门很少用很有用的命令?(转)
    5分钟用Spring4 搭建一个REST WebService(转)
    一本好看的书————《反欺骗的艺术——世界传奇黑客的经历分享》
    国内外从事CV相关的企业
    定积分解法
    c++中&amp;和&amp;&amp;有什么差别
    树莓派相关-树莓派串口配置方法
    Linux下df与du两个命令的差别?
    veridata实验举例(2)验证表BONUS与表SALGRADE两节点同步情况
    Java替代C语言的可能性
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4357934.html
Copyright © 2011-2022 走看看