UVA 808 - Bee Breeding(建坐标系+找规律)

题目链接 https://cn.vjudge.net/problem/UVA-808

【题意】
给定两个格子的编号a和b(1<=a,b<=10000)求它们之间的最短距离
这里写图片描述

【思路】
太菜了,这种题根本没思路,看了别人的题解才会做. 大概是建立平面坐标系,一个x轴一个y轴,然后用向量表示每个点,向量位于一三象限时最短路为坐标绝对值之和,位于二四象限时最短路为坐标绝对值的最大值
这里写图片描述

#include<bits/stdc++.h>
using namespace std;

const int maxn=10050;
const int dx[6]={-1,-1,0,1,1,0};
const int dy[6]={0,1,1,0,-1,-1};

struct Point{
    int x,y;
    Point(int xx,int yy):x(xx),y(yy){}
};

vector<Point> g;

void calc(int dir,int num){
    while(num--){
        int x=g.back().x;
        int y=g.back().y;
        x+=dx[dir];
        y+=dy[dir];
        g.push_back(Point(x,y));
    }
}

void init(){
    g.clear();
    g.push_back(Point(0,0));
    g.push_back(Point(0,0));
    g.push_back(Point(1,-1));
    int cnt=1;
    while(g.size()<maxn){
        for(int d=0;d<4;++d){
            calc(d,cnt);
        }
        calc(4,cnt+1);
        calc(5,cnt);
        ++cnt;
    }
}

int main(){
    init();
    int a,b;
    while(scanf("%d%d",&a,&b)==2){
        if(0==a && 0==b) break;
        int x=g[a].x-g[b].x;
        int y=g[a].y-g[b].y;
        int ans;
        if((x<0 && y>0) || (x>0 && y<0)) ans=max(abs(x),abs(y));
        else ans=abs(x)+abs(y);
        printf("The distance between cells %d and %d is %d.\n",a,b,ans);
    }
    return 0;
}
posted @ 2018-08-24 19:25  不想吃WA的咸鱼  阅读(169)  评论(0编辑  收藏  举报