URAL - 1145 Rope in the Labyrinth

/*
题意: 找出两点间的最长距离.
解题思路:
        1. 树上面求最长路简单路(无环). 就是树的直径问题.
        2. 树的直径问题经典: 两遍BFS即可.
           问题分析:
                (1). 一开始任取一个点u进行搜索查找出距离点u最远距离的点v和长度.
                (2). 第二次BFS则从第一次中的点v找出距离点v最远距离的点的路径长度.
        3. 问题正确性.
           证明:
                (1). 情况1: u在最长路上, 那么v一定是最长路的一端.
                    反证法: v不是在最长路的一端, 即有一个v1使得(u->v1)是最长路的一部分,
                            就有: dist(u->v1) > dist(u->v); 矛盾.
                            即: v在最长路的一端. (第二次BFS就可以找出距离v最远的点的长度.)
                (2). 情况2: u不再最长路上, 则有点u到点v的路与最长路一定有一个交点c.
                            并且(c->v)与最长路的后半段是重合的. 即v一定是最长路的一端.
*/
# include <stdio.h>
# include <algorithm>
# include <iostream>
# include <queue>
# include <string.h>
using namespace std;
struct node
{
    int x;
    int y;
    int step;
} ;
int vx[4]= {0,0,1,-1};
int vy[4]= {1,-1,0,0};
int n,m;
char a[1010][1010];
int vis[1010][1010];
int sum;
int judge(int tx,int ty)
{
    if(tx>=0&&tx<n&&ty>=0&&ty<m)
        return 1;
    return 0;
}
node bfs(node a1)
{
    int i;
    node front,rear,ans;
    memset(vis,0,sizeof(vis));
    front.x=a1.x;
    front.y=a1.y;
    front.step=a1.step;
    queue<node>q;
    q.push(front);
    vis[a1.x][a1.y]=1;
    while(!q.empty())
    {
        front=q.front();
        q.pop();
        for(i=0; i<4; i++)
        {
            int xxx=front.x+vx[i];
            int yyy=front.y+vy[i];
            if(!vis[xxx][yyy]&&a[xxx][yyy]=='.'&&judge(xxx,yyy))
            {
                rear.x=xxx;
                rear.y=yyy;
                rear.step=front.step+1;
                vis[xxx][yyy]=1;
                q.push(rear);
            }
        }
    }
    return rear;
}
int main()
{
    int i,j,flag;
    node tt;
    while(~scanf("%d%d",&m,&n))
    {
        for(i=0; i<n; i++)
        {
            getchar();
            scanf("%s",a[i]);
        }
        for(i=0; i<n; i++)
        {
            flag=0;
            for(j=0; j<m; j++)
            {
                if(a[i][j]=='.')
                {
                    tt.x=i;
                    tt.y=j;
                    tt.step=0;
                    flag=1;

                    break;
                }
            }
            if(flag)
                break;
        }
        sum=0;
        node ans=bfs(tt);
        ans.step=0;
        bfs(ans);
        printf("%d\n",bfs(ans).step);
    }
    return 0;
}

用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值