Intel's Ronler Acres Plant

Silicon Forest
If the type is too small, Ctrl+ is your friend

Tron Battle dot C

// Tron Battle

#include
#include
#include
#include

#define EOL "\n"
#define DEBUG   1
#define ECHO    0
#define echoprintj(p)   eprint(#p, p, "  ")
#define echoprint( p)   eprint(#p, p, EOL)

#define UP      0
#define LEFT    1
#define DOWN    2
#define RIGHT   3

#define ROWS    20
#define COLUMNS 30

int field[ROWS][COLUMNS];      // y then x
int XP;
int YP;
int cleared[4];

void eprint(char* ptr, int p, char* eol)
{
    if (DEBUG)  fprintf(stderr, "%s: %d%s",   ptr, p, eol);
    else
    if (ECHO )  fprintf(stderr,     "%d%s",        p, eol);
}  

void clear_field(int id)
{
    if (!cleared[id])
    {
        for (int x=0; x            for (int y=0; y                if (field[y][x] == id)
                    field[y][x] = -1;
        cleared[id] = 1;
    }
}

void show_field(void)
{
    for (int y=0; y    {
        for (int x=0; x            fprintf(stderr, "%c ", field[y][x] + '0');
        fprintf(stderr, EOL);
    }
}


int look_for_long(void)     // Got score of 33.40, place 129
{
    int maxc = 0;
    int maxi;
    int dx;
   
    for (dx=0; dx<4 br="" dx="">    {
        int count = 0;
        int xx = XP;
        int yy = YP;
        switch (dx)
        {
        case UP:    for (int yy=YP-1; yy>=0; yy--)
                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count++;
                    }
                    break;
                   
        case LEFT:  for (int xx=XP-1; xx>=0; xx--)
                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count++;
                    }
                    break;
                   
        case DOWN:  for (int yy=YP+1; yy                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count++;
                    }
                    break;
                   
        case RIGHT:    for (int xx=XP+1; xx                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count++;
                    }
                    break;
                   
        }
        echoprintj(dx);
        echoprint(count);
        if (maxc < count)
        {
            maxc = count;
            maxi = dx;
        }
    }
    return maxi;
}

int count_horizontal(int x2, int yy)
{
    int count = 0;
   
    for (int xx=x2-1; xx>=0; xx--)
    {
        if (field[yy][xx]!=-1)
            break;
        count++;
    }
    for (int xx=x2; xx    {
        if (field[yy][xx]!=-1)
            break;
        count++;
    }
    return count;
}

int count_vertical(int xx, int y2)
{
    int count = 0;
   
    for (int yy=y2-1; yy>=0; yy--)
    {
        if (field[yy][xx]!=-1)
            break;
        count++;
    }
    for (int yy=y2; yy    {
        if (field[yy][xx]!=-1)
            break;
        count++;
    }
    return count;
}

int look_for_long_and_wide(void)     // Got score of 33.31, place 129
{
    int maxc = 0;
    int maxi;
    int dx;
   
    for (dx=0; dx<4 br="" dx="">    {
        int count   = 0;
        int xx      = XP;
        int yy      = YP;
        switch (dx)
        {
        case UP:    for (int yy=YP-1; yy>=0; yy--)
                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count += count_horizontal(xx, yy);
                    }
                    break;
                   
        case LEFT:  for (int xx=XP-1; xx>=0; xx--)
                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count += count_vertical(xx, yy);
                    }
                    break;
                   
        case DOWN:  for (int yy=YP+1; yy                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count += count_horizontal(xx, yy);
                    }
                    break;
                   
        case RIGHT:    for (int xx=XP+1; xx                    {
                        if (field[yy][xx]!=-1)
                            break;
                        count += count_vertical(xx, yy);
                    }
                    break;
                   
        }
        echoprintj(dx);
        echoprint(count);
        if (maxc < count)
        {
            maxc = count;
            maxi = dx;
        }
    }
    return maxi;
}

int valid(int dx)
{
    switch(dx)
    {
    case UP:        return ( YP   >  0    ) && (field[YP-1][XP  ]==-1);
    case LEFT:      return ( XP   >  0    ) && (field[YP  ][XP-1]==-1);
    case DOWN:      return ((YP+1)< ROWS  ) && (field[YP+1][XP  ]==-1);
    case RIGHT:     return ((XP+1)    default:        return 0;
    }
}


int main()
{
    int dx = -1;
   
    memset(field,  -1, sizeof( field ));
    memset(cleared, 0, sizeof(cleared));
   
    while (1)
    {
        int N; // total number of players (2 to 4).
        int P; // your player number (0 to 3).
   
        scanf("%d%d", &N, &P); fgetc(stdin);
   
        for (int i = 0; i < N; i++)
        {
            int X0; // starting X coordinate of lightcycle (or -1)
            int Y0; // starting Y coordinate of lightcycle (or -1)
            int X1; // starting X coordinate of lightcycle (can be the

                    // same as X0 if you play before this player)
            int Y1; // starting Y coordinate of lightcycle (can be the 

                    // same as Y0 if you play before this player)
            scanf("%d%d%d%d", &X0, &Y0, &X1, &Y1); fgetc(stdin);
            if ((X0 < 0) || (X1 < 0) || (Y0 < 0) || (Y1 < 0))
                clear_field(i);
            else
            {
                field[Y0][X0] = i;
                field[Y1][X1] = i;
            }
            if (i==P)
            {
                XP = X1;
                YP = Y1;
            }
        }
//      show_field();
//        if (!valid(dx))   removing this statement boosted our score
//                         
to 36.48 and our rank to 75
            dx = look_for_long_and_wide();
        switch(dx)
        {
        case UP:        printf("UP"     EOL);   break;
        case LEFT:      printf("LEFT"   EOL);   break;
        case DOWN:      printf("DOWN"   EOL);   break;
        case RIGHT:     printf("RIGHT"  EOL);   break;
        default:        assert(0);
        }
    }
}

No comments: