Time Limit: 1000MS | Memory Limit: 10000K |
---|
Playing games is the most fun if other people take part. But other players are not always available if you need them, which led to the invention of single-player games. One of the most well-known examples is the infamous “Solitaire” packaged with Windows, probably responsible for more wasted hours in offices around the world than any other game.
The goal of a single-player game is usually to make “moves” until one reaches a final state of the game, which results in a win or loss, or a score assigned to that final state. Most players try to optimize the result of the game by employing good strategies. In this problem we are interested in what happens if one plays randomly. After all, these games are mostly used to waste time, and playing randomly achieves this goal as well as any other strategy.
Games can very compactly represented as (possibly infinite) trees. Every node of the tree repre- sents a possible game state. The root of the tree corresponds to the starting position of the game. For an inner node, its children are the game states to which one can move in a single move. The leaf nodes are the final states, and every one of them is assigned a number, which is the score one receives when ending up at that leaf.
Trees are defined using the following grammar.
Definition ::= Identifier “=” RealTree
RealTree ::= “(“Tree+”)”
Tree ::= Identifier | Integer | “(“Tree+”)”
Identifier ::= a|b|…|z
Integer ∈ {…,-3,-2,-1,0,1,2,3,…,}
By using a Definition, the RealTree on the right-hand side of the equation is assigned to the Identifier on the left. A RealTree consists of a root node and one or more children, given as a sequence enclosed in brackets. And a Tree is either
. the tree represented by a given Identifier, or
. a leaf node, represented by a single Integer, or
. an inner node, represented by a sequence of one or more Trees (its children), enclosed in brackets.
Your goal is to compute the expected score, if one plays randomly, i.e. at each inner node selects one of the children uniformly at random. This expected score is well-defined even for the infinite trees definable in our framework as long as the probability that the game ends (playing randomly) is 1.
The input file contains several gametree descriptions. Each description starts with a line containing the number n of identifiers used in the description. The identifiers used will be the first n lowercase letters of the alphabet. The following n lines contain the definitions of these identifiers (in the order a, b, …). Each definition may contain arbitrary whitespace (but of course there will be no spaces within a single integer). The right hand side of a definition will contain only identifiers from the first n lowercase letters. The inputs ends with a test case starting with n = 0. This test case should not be processed.
For each gametree description in the input, first output the number of the game. Then, for all n identifiers in the order a, b, …, output the following. If an identifier represents a gametree for which the probability of finishing the game is 1, print the expected score (when playing randomly). This value should be exact to three digits to the right of the decimal point.
If the game described by the variable does not end with probability 1, print “Expected score of id undefined” instead. Output a blank line after each test case.
1
a = ((1 7) 6 ((8 3) 4))
2
a = (1 b)
b = (4 a)
1
a = (a a a)
0
Game 1
Expected score for a = 4.917
Game 2
Expected score for a = 2.000
Expected score for b = 3.000
Game 3
Expected score for a undefined
题意:给你一颗树,然后根据题意中所给的方式建树,然后计算每个未知几节点的期望。
首先,对于已经知道权值的节点,我们可以计算出对一定节点期望的贡献 value×p 。概率为对应的节点到这个节点的概率。我们假设未知节点的期望为 x ,那么我们可以得到方程
不过在处理字符串的时候比较的恶心。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <list>
#include <limits>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#define rr(x) freopen(x,"r",stdin)
#define ww(x) freopen(x,"w",stdout)
using namespace std;
const int Mod = 2;
const double eps = 1e-8;
const double Pi = acos(-1.0);
double ma[30][30];
char str[1100];
int n;
void dfs(int &st,double *a) {
double b[30];
int cnt = 0;
while(str[st]!=')') {
if(str[st] >='a' && str[st]<='z') {
a[str[st]-'a'] += 1;
cnt++;st++;
}
else if(str[st] == ' ') st++;
else if(str[st] == '-'){
st++; double num = 0;
while(str[st]>='0' && str[st]<='9') {
num = num*10+str[st]-'0'; st++;
}
cnt++;
a[n] -=num;
}
else if(str[st]>='0' && str[st]<='9') {
double num = 0;
while(str[st]>='0' && str[st]<='9') {
num = num*10+str[st]-'0';
st++;
}
cnt++;
a[n]+=num;
}
else if(str[st] == '('){
st++;
memset(b,0,sizeof(b));
dfs(st,b); cnt++;
for(int i = 0;i<=n;i++) a[i]+=b[i];
}
}
if(cnt)
for(int i = 0;i<=n;i++) a[i]/=cnt;
st++;
}
double ans[30];
bool vis[30];
int dbcmp(double s) {
if(fabs(s) < eps) return 0;
return s>0?1:-1;
}
int Gauss(int n,int m) {
memset(ans,0,sizeof(ans));
memset(vis,true,sizeof(vis));
int row = 0,col = 0;
for(;row<=n&&col<m;row++,col++) {
int r = row;
for(int i = row+1;i<=n;i++) if(fabs(ma[i][col]) > fabs(ma[r][col])) r = i;
if(dbcmp(fabs(ma[r][col])) == 0) {
row --; continue;
}
for(int i = col;i<=m;i++) swap(ma[r][i],ma[row][i]);
for(int i = row+1;i<=n;i++) {
if(dbcmp(fabs(ma[i][col]))) {
double s = ma[i][col]/ma[row][col];
for(int j = col;j<=m;j++) ma[i][j] = ma[i][j] - ma[row][j]*s;
}
}
}
for(int i = row;i<=n;i++) if(dbcmp(fabs(ma[i][m]))) return -1;
if(row <=n) {
for(int i = row-1;i>=0;i--) {
int num = 0,fr;
for(int j =0 ;j<m;j++) {
if(fabs(ma[i][j]) >eps && vis[j]) {
num++;
fr = j;
}
}
if(num >1) continue;
double temp = ma[i][m];
for(int j = 0;j<m;j++)
if(fabs(ma[i][j])>eps && j!=fr) temp-=ans[j]*ma[i][j];
ans[fr] = temp/=ma[i][fr];
vis[fr] = false;
}
return 1;
}
for(int i = m-1;i>=0;i--) {
double temp = ma[i][m];
for(int j = i+1;j<m;j++) temp-=ma[i][j]*ans[j];
ans[i] = temp/ma[i][i];
}
return 0;
}
int main(){
int z =1;
while(~scanf("%d",&n) && n) {
getchar();
memset(ma,0,sizeof(ma));
for(int i = 0 ;i<n;i++) {
gets(str);
int st = 0;
while(str[st]!='(') st++;
st++;
dfs(st,ma[i]);
ma[i][n] = -ma[i][n];
ma[i][i]--;
}
int an = Gauss(n-1,n);
printf("Game %d\n",z++);
for(int i = 0;i<n;i++) {
printf("Expected score for %c ",i+'a');
if(an == -1) printf("undefined\n");
else {
if(an ==0) printf("= %.3f\n",ans[i]);
else {
if(vis[i]) printf("undefined\n");
else printf("= %.3f\n",ans[i]);
}
}
}
printf("\n");
}
return 0;
}