Single-Player Games
Single-Player Games |
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.
By using a Definition, the RealTree on the right-hand side of the equation is assigned to the Identifier on the left. A RealTreeconsists 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.
Input
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.Output
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.
Sample Input
1 a = ((1 7) 6 ((8 3) 4)) 2 a = (1 b) b = (4 a) 1 a = (a a a) 0
Sample Output
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
Miguel Revilla
2000-05-22
这道题网上没什么人写,其实不是难题。
题目给出 n 个树,可以从每个树得出一个一次多元多项式,这些多项式同时组成一个线性方程组,这个线性方程组的解就是answer;
这个题目主要要完成树的建立:遇见左括号则向右寻找匹配的右括号,两括号间形成子树;遇见单个字母或数字直接形成叶子节点;
然后对树遍历,遇到叶子节点则回溯到根,以计算叶子节点表示的项的因数;
树遍历完了,线性方程组就建立了;
建好线性方程组然后求解,遇到无解的项要记录,因为结果要求输出undefined的项。(代码中用 isDefined 和 isDef 数组记录)
解线性方程组用高斯消元法。
一点想法:建树只是中间过程,应该有不建树直接求因数的方法,但是建树直观一些,也比较容易思考一些。
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e13;
bool IsZero(double d) {
return fabs(d) < EPS;
}
bool IsEqual(double a, double b) {
return IsZero(a - b);
}
const int MAX_SZ = 50;
struct LinearEquSys {
double a[MAX_SZ][MAX_SZ];
double b[MAX_SZ];
int size;
void Init(int sz) {
assert (sz <= MAX_SZ);
size = sz;
fill (a[0], a[MAX_SZ], 0.0);
fill (b, b + MAX_SZ, 0.0);
}
void GaussianElim(double x[MAX_SZ], bool isDefined[MAX_SZ]) {
for(int i=0; i<size; ++i) {
int t = i;
while(t < size && IsZero(a[t][i])) {
++t;
}
if(! (t < size)) {
continue;
}
swap(b[i], b[t]);
for(int k=i; k<size; ++k) {
swap(a[i][k], a[t][k]);
}
double w = a[i][i];
b[i] /= w;
for(int k=i; k<size; ++k) {
a[i][k] /= w;
}
for(int k=i+1; k<size; ++k) {
double u = a[k][i];
b[k] -= u * b[i];
for(int m=i; m<size; ++m) {
a[k][m] -= u * a[i][m];
}
}
#ifdef _DEBUG
Print();
#endif
}
for (int i=0; i<size; ++i) {
x[i] = b[i];
isDefined[i] = IsEqual (a[i][i], 1);
}
for (int i=size-1; i>=0; --i) {
for (int k=0; k<i; ++k) {
x[k] -= x[i] * a[k][i];
isDefined[k] = (IsZero (a[k][i]) || isDefined[i]) && isDefined[k];
}
}
#ifdef _DEBUG
for(int i=0; i<size; ++i) {
double d = 0;
for(int k=0; k<size; ++k) {
d += a[i][k] * x[k];
}
assert(IsZero(d - b[i]));
}
#endif
}
#ifdef _DEBUG
void Print() {
printf("\n");
for(int i=0; i<size; ++i) {
for(int k=0; k<size; ++k) {
printf("%6.2lf", a[i][k]);
}
printf("%8.2lf\n", b[i]);
}
printf("\n");
}
#endif
};
struct Node {
enum Type { IDENT, NUM, IN_NOD }; // IDENTifier, NUMber, and INner NODe
Type type;
int par;
vector <int> chd;
union {
double num;
char ident;
};
};
#define NODES_MAX 102400
Node node[NODES_MAX];
int nodes = 0;
const string alph = "abcdefghijklmnopqrstuvwxyz";
const string dec = "-+.0123456789";
void Clear () {
for (int i=0; i<nodes; ++i) {
node[i].chd.clear();
}
nodes = 0;
}
void BuildTree (int ind, const string &str) {
#ifdef _DEBUG
printf ("ind = %d, str = %s\n", ind, str.c_str());
#endif
string::size_type i = 0 - 1;
while (++i < str.size()) {
if (str[i] == '(') {
string::size_type k = i;
int bar = 1;
while (++i < str.size() && bar) {
if (str[i] == '(') {
++bar;
} else if (str[i] == ')') {
--bar;
}
}
assert (! bar);
node[ind].chd.push_back (nodes);
node[nodes].par = ind;
BuildTree (nodes++, str.substr (k + 1, --i - 1 - k));
} else if (dec.find (str[i]) != string::npos) {
node[ind].chd.push_back (nodes);
node[nodes].par = ind;
node[nodes].type = Node::NUM;
int n;
sscanf (str.substr (i, string::npos).c_str(), "%lf%n", &node[nodes++].num, &n);
i += n;
} else if (alph.find (str[i]) != string::npos) {
node[ind].chd.push_back (nodes);
node[nodes].par = ind;
node[nodes].type = Node::IDENT;
node[nodes++].ident = str[i];
}
}
assert (node[ind].chd.size() != 0);
node[ind].type = Node::IN_NOD;
}
LinearEquSys linear;
int line;
void Traverse (int ind) {
if (node[ind].chd.size() == 0) {
int i = ind;
double a = 1;
while ((i = node[i].par) != -1) {
a /= node[i].chd.size();
}
if (node[ind].type == Node::NUM) {
linear.b[line] += a * node[ind].num;
#ifdef _DEBUG
printf ("%.3lf, a = %.3lf\n", node[ind].num, a);
#endif
} else {
assert (node[ind].type == Node::IDENT);
linear.a[line][node[ind].ident - 'a'] -= a;
#ifdef _DEBUG
printf ("%c, a = %.3lf\n", node[ind].ident, a);
#endif
}
}
for (int i=0; i<node[ind].chd.size(); ++i) {
Traverse (node[ind].chd[i]);
}
}
int main () {
static int cs = 1;
int n;
while (scanf ("%d", &n) == 1 && n) {
printf ("Game %d\n", cs++);
while (getchar () != '\n') {}
linear.Init (n);
char str[1024];
for (int i=0; i<n; ++i) {
gets (str);
line = i;
linear.a[i][i] = 1;
Clear ();
node[nodes++].par = -1;
BuildTree (0, strchr (str, '=') + 1);
Traverse (0);
}
double x[MAX_SZ];
bool isDef[MAX_SZ];
linear.GaussianElim(x, isDef);
for (int k=0; k<n; ++k) {
if (isDef[k]) {
printf ("Expected score for %c = %.3lf\n", k + 'a', x[k]);
} else {
printf ("Expected score for %c undefined\n", k + 'a');
}
}
printf ("\n");
}
return 0;
}