/* Problem I: Single-Player Games * Author: Mark Dettinger * * Algorithm: * Each tree definition yields one linear equation in a,b,...z. * The expected value for a given tree t is determined by solving the * linear equation system that contains the equations of the trees on * which t depends. If the determinant of this system is zero, the value * is undefined. */ #include #include #include #include #define EPS (1E-9) #define DBG(x) typedef struct { double coeff[32]; } polynom; FILE *input; int game=0; /* number of game */ double a[32][32]; /* the linear equation system */ int num_equations; /* number of equations */ double b[32][32]; /* sub-system */ int n; /* equations in sub-system */ int p; /* read head position during parsing */ char s[1000]; /* tree definition during parsing */ int depends_on[32][32]; /* dependencies between identifiers */ void ws() { while (s[p] && isspace(s[p])) p++; } polynom tree (int i) { polynom result; int j; DBG(printf("enter tree at position %c\n",s[p])); for (j=0; j<=num_equations; j++) result.coeff[j] = 0.0; if (s[p]=='(') { int length; polynom q,sum; p++; ws(); for (j=0; j<=num_equations; j++) sum.coeff[j] = 0.0; for (length=0; s[p]!=')'; length++) { q = tree(i); for (j=0; j<=num_equations; j++) sum.coeff[j] += q.coeff[j]; } p++; ws(); for (j=0; j<=num_equations; j++) result.coeff[j] = sum.coeff[j]/length; } else if (isalpha(s[p])) { result.coeff[s[p]-'a'] = 1.0; depends_on[i][s[p]-'a'] = 1; p++; ws(); } else if (s[p] == '-' || isdigit(s[p])) { sscanf(s+p,"%lf",&result.coeff[num_equations]); while (s[p] && (s[p] == '-' || isdigit(s[p]))) p++; ws(); } else { printf("error!\n"); assert(0); } return result; } void print_matrix() { int i,j; for (i=0; ifabs(b[pivot][j])) pivot = i; /* exchange row j and pivot row */ for (k=0; k<=n; k++) { tmp = b[j][k]; b[j][k] = b[pivot][k]; b[pivot][k] = tmp; } if (fabs(b[j][j])=j; k--) b[i][k] -= b[i][j]*b[j][k]/b[j][j]; } /* gaussian elimination: backward phase*/ for (j=n-1; j>=0; j--) { x[j] = b[j][n]/b[j][j]; for (i=j-1; i>=0; i--) { b[i][n] -= x[j]*b[i][j]; b[i][j] = 0; } } DBG(print_matrix2()); /* print result */ printf("Expected score for %c = %.3f\n",eq+'a',x[i3]); finished: } printf("\n"); } int main() { input = fopen("games.in","r"); assert(input!=NULL); while (read_case()) solve_case(); fclose(input); return 0; }