poj_1487 Single-Player Games(高斯消元+自由元)

Single-Player Games
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1479   Accepted: 407

Description

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.
poj_1487 Single-Player Games(高斯消元+自由元)_第1张图片

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.

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
 
  
高斯消元解方程组,包括求出自由元。注意浮点数的精度问题。
我的预处理方法是先按题目给出的第一个图那样建成一棵树,然后再从树根开始往下遍历,这样就方便处理一些了。
 
  
 
  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf 0x3f3f3f3f
#define maxn 30
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

const double eps = 1e-6;

struct Node
{
    char c;
    int num;
    vector children;
    Node(char c, int num) : c(c), num(num) {}
};

char s[300];
int len, n;
int pos;

typedef double Matrix[maxn][maxn];
double x[maxn];
bool free_x[maxn];
Matrix A;

//equ个方程,var个变元 A[r][c], 0 fabs(A[max_r][col])) max_r = r;
        }
        if(max_r != row) for(int c = 0; c <= var; c++) swap(A[max_r][c], A[row][c]);

        if(fabs(A[row][col]) < eps)
        {
            row--;    //说明该col列第row行以下全是0了,则处理当前行的下一列.
            continue;
        }

        //与第row+1~equ行进行消元
        int LCM, ta, tb;
        for(int r = row+1; r < equ; r++)
        {
            if(fabs(A[r][col]) < eps) continue;
            double f = A[r][col] / A[row][col];
            for(int c = col; c <= var; c++) A[r][c] -= f * A[row][c];
        }
    }

    // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0),即矛盾方程
    for (int r = row; r < equ; r++) if (fabs(A[r][col]) > 0) return -1;

    // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
    // row表示有row个不是矛盾方程的非0行,即有row个非自由变元,总变元 - 非自由变量元 = 自由变量元。
    if (row < var)
    {
        //注释处为求多解的自由变量
        // 首先,自由变元有var - row个,即不确定的变元至少有var - row个.
        int num = 0, freeidx;
        for (int r = row - 1; r >= 0; r--)
        {
            num = 0;// 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.
            double temp = A[r][var];
            // 第r行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第row行到第equ行.
            // 同样,第r行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.
            for (int c = 0; c < var; c++)
            {
                if (fabs(A[r][c]) > eps && free_x[c])
                {
                    num++;
                    freeidx = c;
                }
            }
            if (num > 1) continue; // 无法求解出确定的变元.
            
            // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.
            temp = A[r][var];
            for (int c = 0; c < var; c++)
            {
                if (fabs(A[r][c]) > eps && c != freeidx) temp -= A[r][c]*x[c];
            }
            x[freeidx] = temp / A[r][freeidx];
            free_x[freeidx] = 0;
        }
        return var - row;
    }

    // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵。
    // 计算出Xn-1, Xn-2 ... X0.
    double temp;
    for (int r = var - 1; r >= 0; r--)
    {
        temp = A[r][var];
        for(int c = r + 1; c < var; c++)
        {
            temp -= x[c] * A[r][c];
        }
        x[r] = temp / A[r][r];
    }
    return 0;

}

void dfs(Node node, int cot)
{
    if(node.c == '*')
    {
        for(int i = 0; i < node.children.size(); i++)
        {
            dfs(node.children[i], node.children.size() * cot);
        }
    }
    else if(node.c == '$') A[pos][n] += (double)node.num / cot;
    else if(node.c >= 'a' && node.c <= 'z') A[pos][node.c-'a'] += 1.0 / cot;
}

void solve()
{
    stack S1;
    stack S2;
    Node head = Node('*', 0);
    for(int i = 4; i < len; i++)
    {
        if(s[i] == ' ') continue;
        else if(s[i] == '(') S1.push(0);
        else if((s[i] >= '0' && s[i] <= '9')||(s[i] == '-'))
        {
            int num;
            sscanf(s+i, "%d", &num);
            S2.push(Node('$', num));
            S1.top()++;
            if(s[i] == '-') i++;
            while(num/=10) i++;
        }
        else if(s[i] >= 'a' && s[i] <= 'z')
        {
            S2.push(Node(s[i], -1));
            S1.top()++;
        }
        else if(s[i] == ')')
        {
            int c = S1.top();
            S1.pop();
            if(!S1.empty())
            {
                Node t = Node('*', 0);
                for(int j = 1; j <= c; j++)
                {
                    t.children.push_back(S2.top());
                    S2.pop();
                }
                S1.top()++;
                S2.push(t);
            }
            else
            {
                for(int j = 1; j <= c; j++)
                {
                    head.children.push_back(S2.top());
                    S2.pop();
                }
            }
        }
    }
    dfs(head, 1);
    A[pos][n] = -A[pos][n];
    A[pos][pos]--;

}


int main()
{
    int casen = 1;
    while(~scanf("%d", &n) && n)
    {
        memset(A, 0, sizeof(A));
        getchar();

        for(pos = 0; pos < n; pos++)
        {
            gets(s);
            len = strlen(s);
            solve();
        }

        int ans = gauss_elimination(A, n, n);

        printf("Game %d\n", casen);
        casen++;

        for(int i = 0; i < n; i++)
        {
            printf("Expected score for %c ", 'a'+i);
            if(ans == 0) printf("= %.3f\n", x[i]);
            else if(ans == -1 || free_x[i]) printf("undefined\n");
            else printf("= %.3f\n", x[i]);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(poj_1487 Single-Player Games(高斯消元+自由元))