poj3288(图论)

Mall Mania
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 921 Accepted: 294

Description

Waterloo has two giant shopping malls, each enclosing several city blocks. Kim and Pat like to shop and to walk within the malls but they don't like walking between the malls because such walking does not contribute directly to the task at hand: shopping. They would therefore like to know the minimum crossing distance between the malls.

Each city block is a unit square delimited by streets and avenues. Streets run east-west and avenues run north-south. Both are identified by consecutive integers between 0 and 2000. (Lower numbered avenues are west of higher numbered avenues and lower numbered streets are south of higher numbered streets.) Streets and avenues are narrow so their thickness may be assumed to be 0.

Each mall is a contiguous set of complete city blocks. By contiguous we mean that any two blocks are connected by some sequence of blocks such that consecutive pairs of blocks in the sequence share a side. The malls do not intersect and do not surround any empty blocks; that is, the blocks not in any mall are themselves contiguous.

Input

Standard input contains several test cases. Each test case contains the description of the two malls. Each mall's description consists of an integer p ≥ 4, the perimeter of the mall, followed by one or more lines containing p pairs (a,s) giving the coordinates of the avenue-street intersections contained in the perimeter, in clockwise order. A line containing 0 follows the last test case.

Output

For each case, output a single integer d -- the minimum walking distance between the malls, assuming that Kim and Pat always walk along streets and avenues.

Sample Input

4
0 0 0 1 1 1 1 0
6
4 3 4 2 3 2
2 2 2 3
3 3
0

Sample Output

2

Source


#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

typedef struct nodep
{
    int x,y,s;
} point;
point p[4008004];
point q[4008004];
point Q[4008004];

int maps[2005][2005];
int step[2005][2005];
int d[4][2] = {0,1,0,-1,1,0,-1,0};

int bfs( int n, int m, int Lx, int Ly, int Hx, int Hy )
{
    for ( int i = Lx ; i <= Hx ; ++ i )
        for ( int j = Ly ; j <= Hy ; ++ j )
            step[i][j] = 4008004;

    int move = 0,save = 0;
    for ( int i = 0 ; i < n ; ++ i )
    {
        Q[save ++] = p[i];
        step[p[i].x][p[i].y] = 0;
    }
    for ( int i = 0 ; i < m ; ++ i )
        step[q[i].x][q[i].y] = -1;

    while ( move < save )
    {
        point New,now = Q[move ++];
        for ( int i = 0 ; i < 4 ; ++ i )
        {
            New.x = now.x + d[i][0];
            New.y = now.y + d[i][1];
            New.s = now.s + 1;
            if ( New.x >= Lx && New.x <= Hx && New.y >= Ly && New.y <= Hy )
            {
                if ( step[New.x][New.y] == -1 ) return New.s;
                if ( step[New.x][New.y] > now.s + 1 )
                {
                    step[New.x][New.y] = now.s + 1;
                    Q[save ++] = New;
                }
            }
        }
    }
    return -1;
}

int main()
{
    int n,m;
    while ( scanf("%d",&n) && n )
    {
        int lx = 2002,ly = 2002,hx = 0,hy = 0;
        for ( int i = 0 ; i < n ; ++ i )
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            p[i].s = 0;
            if ( lx > p[i].x ) lx = p[i].x;
            if ( ly > p[i].y ) ly = p[i].y;
            if ( hx < p[i].x ) hx = p[i].x;
            if ( hy < p[i].y ) hy = p[i].y;
        }
        scanf("%d",&m);
        for ( int i = 0 ; i < m ; ++ i )
        {
            scanf("%d%d",&q[i].x,&q[i].y);
            q[i].s = -1;
            if ( lx > q[i].x ) lx = q[i].x;
            if ( ly > q[i].y ) ly = q[i].y;
            if ( hx < q[i].x ) hx = q[i].x;
            if ( hy < q[i].y ) hy = q[i].y;
        }

        printf("%d\n",bfs( n, m, lx, ly, hx, hy ));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值