码迷,mamicode.com
首页 > 其他好文 > 详细

UVa808 - Bee Breeding(坐标法)

时间:2015-05-12 15:43:32      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定图中蜂窝两点,求最短距离

思路:建坐标系,然后根据图中走的顺序把每个点的坐标求出来,然后利用坐标去求最小距离

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
const int N = 20005;
const int d[5][2]={{-1,1},{0,2},{1,1},{1,-1},{0,-2}};
struct Point{
    int x,y;
    Point(){}
    Point(int _x,int _y){
        x=_x;y=_y;
    }
}p[N];
int a,b;
void init(){
    int pn=1,x=0,y=0;
    p[pn++]=Point(x,y);
    y-=2;
    p[pn++]=Point(x,y);
    for(int i=1;i<=60;i++){
        int j,k;
        for(j=0;j<5;j++){
            for(k=0;k<i;k++){
                x+=d[j][0];y+=d[j][1];
                p[pn++]=Point(x,y);
            }
        }
        y-=2;
        p[pn++]=Point(x,y);
        for(j=0;j<i;j++){
            x--;y--;
            p[pn++]=Point(x,y);
        }
    }
}
int main()
{
    init();
    while(~scanf("%d%d",&a,&b)&&a||b){
        int x=abs(p[a].x-p[b].x);
        int y=abs(p[a].y-p[b].y);
        printf("The distance between cells %d and %d is ",a,b);
        if(y <= x) printf("%d.\n",x);
        else {
            printf("%d.\n",x+(y-x)/2);
        }
    }
    return 0;
}


UVa808 - Bee Breeding(坐标法)

标签:

原文地址:http://blog.csdn.net/a197p/article/details/45669491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!