ACM UVA (10182)

369阅读 1评论2009-04-16 chnos
分类:

/*
 * UVA Problem: 10182
 * Xiaofeng Ye
 *
 * 1 round 0
 * 2---7 round 1
 * 8---19 round 2
 * 20--37 round 3
 * 38--61 round 4
 *
 * The last number of the round = 3*round(round+1)+1
 * eg: 7=1*3(1+1)+1
 */



#include <math.h>
#include <stdio.h>

int get_the_round(int willi_pos);
int get_pos_in_round(int willi_pos, int round);

struct position {
    int x;
    int y;
};

struct position pos_1to7[7] = {
    {0,0}, {0, 1}, {-1,1}, {-1,0}, {0,-1}, {1,-1}, {1,0}
};

#define UP_LEFT(pos) (pos->x)--
#define UP(pos) (pos->y)--
#define UP_RIGHT(pos) ((pos->)x++; (pos->y)--)
#define DOWN_RIGHT(pos) (pos->x)++
#define DOWN(pos) (pos->y)++
#define DOWN_LEFT(pos) ((pos->x)--; (pos->y)++)

int main(int argc, char **argv)
{
    int the_round;
    int willi_pos;
    int i;
    struct position pos2; /* The position of 2 */
    struct position final_pos;
    struct position *p_final_pos = &final_pos;
    int pos_in_round;
    int n;
    int found_flag = 0;

    pos2.x = 0;
    pos2.y = 1;
    final_pos = pos2;
    while (!feof(stdin)) {
        scanf("%d", &willi_pos);
        the_round = get_the_round(willi_pos);
        pos_in_round = get_pos_in_round(willi_pos, the_round));
        for (i = 0; i < the_round-1; i++) {
            DOWN_RIGHT(p_final_pos);
        }
        if (1 == pos_in_round) {
            printf("%d %d\n", p_final_pos->x, p_final_pos->y);
            continue;
        }
        n = 0;
        for (i = 0; i < the_round-1; i++) {
            DOWN_LEFT(p_final_pos);
            n++;
            if (n == pos_in_round-1) {
                printf("%d %d\n", p_final_pos->x, p_final_pos->y);
                break;
            }
        }
    }
}

int get_the_round(int willi_pos)
{
    int tmp;

    if (willi_pos == 1) {
        return 0;
    }
    tmp = ceil((ceil(sqrt(12*(willi_pos-1)+9))-3)/6);
    if (3*tmp*(tmp+1)+1 < willi_pos) {
        return tmp+1;
    } else if (3*(tmp-1)*tmp+1 < willi_pos) {
        return tmp;
    }
}

int get_pos_in_round(int willi_pos, int round)
{
    if (round == 0) {
        return 1;
    }
    return willi_pos - (3*(round-1)*round+1);
}

上一篇:ACM UVA (10181)
下一篇:ACM UVA (10138)

文章评论