Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as C by lena7 ( 13 years ago )
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>

#define n 3
#define debug 0

typedef uint8_t byte;
typedef unsigned int uint;
typedef unsigned long long ulong;

enum { white = 0, black, pass };

byte f[n] = {0};
byte g[n+1];
bool stop_flag = 0;

// f(w) -> g(f,w)
byte *f_to_g(byte f[])
{
        for (uint w = 0; w <= n; ++w) {
                if (w == 0)
                        g[w] = (bool)(f[0] == black);
                else if (w == n)
                        g[w] = (bool)(f[n-1] == white);
                else
                        g[w] = (bool)((f[w-1] == white && f[w] != white) ||
                                      (f[w-1] != black && f[w] == black));
#if debug
                printf("g[%d] = %d\n",w,g[w]);
#endif
        }
        return g;
}

ulong fact(ulong k)
{
        return (k == 0) ? 1 : k*fact(k-1);
}

uint binom(uint m, uint k)
{
        return fact(m)/fact(k)/fact(m-k);
}

uint prob(byte g[])
{
        uint win = 0;
        for (uint k = 0; k <= n; ++k)
                if (g[k])
                        win += binom(n,k);
        return win;
}

byte *next_f(byte *f)
{
        bool carry = 1;
        for (int k = n-1; k >= 0; --k) {
                f[k] += carry;
                carry = 0;
                if (f[k] > 2) {
                        if (k == 0) {
                                stop_flag = 1;
                                break;
                        }
                        f[k] -= 3;
                        carry = 1;
                } else
                        break;
        }
        return f;
}

char answer_letter(byte k)
{
        if (k == white)
                return 'w';
        else if (k == black)
                return 'b';
        else
                return '-';
}

int main(void)
{
        uint pr, best_pr = 0;
        byte best_f[n] = {0};

        while (stop_flag == 0) {
                pr = prob(f_to_g(f));
                if (pr > best_pr) {
                        best_pr = pr;
                        memcpy(best_f, f, sizeof(f));
                }
                next_f(f);
        }

        printf("%d:\t", n);
        for (uint k = 0; k < n; ++k)
                printf("%c ", answer_letter(best_f[k]));
        printf("\t-> %u/%u\n", best_pr, 1 << n);
}

 

Revise this Paste

Your Name: Code Language: