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

题目:

输入两个格子的编号a和b(a,b≤10000),求最短距离。例如,19和30的距离为5(一条最短路是19-7-6-5-15-30)。

思路:

如图建立坐标系,然后看两个点的向量如果位于二四象限答案为向量坐标绝对值的和,向量位于一三象限答案为向量坐标绝对值的最大值。

看网上的博客的思路……(菜是原罪)

难点是建立坐标系。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 10005;
int dx[6]={0,-1,-1,0,1,1};//注意走的方向的顺序
int dy[6]={-1,-1,0,1,1,0};
struct Point{
    int x,y;
    Point(int xx=0,int yy=0):x(xx),y(yy){}
};
vector<Point> g;

void init(){
    g.clear();
    g.push_back(Point(0,0));
    g.push_back(Point(0,0));//对应编号1
    g.push_back(Point(1,1));//对应编号2
    int cnt=1;
    while(g.size()<maxn){
        for(int i=0; i<6; i++){
            int tcnt = (i==4?cnt+1:cnt);//向下要比其他方向多走一格
            while(tcnt--){
                int x = g.back().x+dx[i];
                int y = g.back().y+dy[i];
                g.push_back(Point(x,y));
            }
        }
        cnt++;//圈数也是每一个方向重复的个数
    }
}

int main(){
    //FRE();
    init();
    int a,b,ans;
    while(scanf("%d%d",&a,&b) && a){
        int x = g[b].x-g[a].x;
        int y = g[b].y-g[a].y;
       // cout<<"x:  "<<g[b].x<<" y:  "<<g[b].y<<endl;
       // cout<<"x:  "<<g[a].x<<" y:  "<<g[a].y<<endl;
        if((x>0&&y<0) || (x<0&&y>0)){
            ans = abs(x)+abs(y);
        }else{
            ans = max(abs(x),abs(y));
        }
        printf("The distance between cells %d and %d is %d.\n",a,b,ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/sykline/p/10359979.html
Bee