poj1487--Single-Player Games(高斯消元)+测试数据

题目链接:点击打开链接

题目大意:给出从根节点遍历到所有叶子节点,每个子节点对父节点来说概率相同,计算最终得到的结果的期望

给出n个字符串,也就可以得到n个方程,用高斯消元解出结果,注意,可能存在不能被计算出来的值,要注意判断,判断方式,从1到n找到第一个Map[i][j]不为0的值,也就是当前方程要求的值,如果从j向后存在Map[i][k]不为0,并且ans[k]还没有被计算出来,那么这个ans[j]也不能被解出来。

注意:可能输出-0.000,这是错误的

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
#pragma comment(linker,"/STACK:102400000,102400000")
#define eqs 1e-8
struct node{
    double a[26] ;
    double k ;
};
int n , len , id , vis[26] ;
double Map[26][26] , a[26] , ans[26] ;
char str[1000000] , s[30] ;
node dfs() {
    int i , k = 0 , flag = 0 , num = 0 ;
    char ch ;
    node p , q ;
    p.k = 0 ;
    memset(p.a,0,sizeof(p.a)) ;
    while( ch = str[id++] ) {
        if( ch == ' ' ) continue ;
        else if( ch == '(' ) {
            q = dfs() ;
            for(i = 0 ; i < n ; i++)
                p.a[i] += q.a[i] ;
            p.k += q.k ;
            num++ ;
        }
        else if( ch  == '-' ) {
            flag = 1 ;
        }
        else if( ch >= '0' && ch <= '9' ) {
            k = k*10 + ch - '0' ;
            if( str[id] >= '0' && str[id] <= '9' ) continue ;
            if( flag ) k = -k ;
            flag = 0 ;
            p.k += k ;
            k = 0 ;
            num++ ;
        }
        else if( ch >= 'a' && ch <= 'z' ) {
            p.a[ ch-'a' ] += 1.0 ;
            num++ ;
        }
        else if( ch == ')' ) {
            for(i = 0 ; i < n ; i++)
                p.a[i] /= (num*1.0) ;
            p.k /= (num*1.0) ;
            return p ;
        }
    }
}
void solve() {
    int i , j , k , l , num = 0 ;
    double temp ;
    for(i = 0 , k = 0 ; i < n && k < n ; i++ , k++) {
        for(j = i ; j < n ; j++)
            if( fabs(Map[j][k]) > eqs ) break ;
        if( j >= n ) {
            i-- ;
            continue ;
        }
        if( i != j ) {
            for(l = k ; l < n ; l++)
                swap(Map[i][l],Map[j][l]) ;
            swap(a[i],a[j]) ;
        }
        for(j = i+1 ; j < n ; j++) {
            if( fabs(Map[j][k]) < eqs ) continue ;
            temp = Map[j][k]/Map[i][k] ;
            for(l = k ; l < n ; l++)
                Map[j][l] = Map[i][l]*temp - Map[j][l] ;
            a[j] = a[i]*temp - a[j] ;
        }
        num++ ;
    }
    memset(ans,0,sizeof(ans)) ;
    memset(vis,0,sizeof(vis)) ;
    for(i = n-1 ; i >= 0 ; i--) {
        for(k = 0 ; k < n ; k++)
            if( fabs(Map[i][k]) > eqs ) break ;
        if( k >= n ) continue ;
        for(j = k+1 ; j < n ; j++) {
            if( !vis[j] && fabs(Map[i][j]) > eqs ) break ;
        }
        if( j < n ) continue ;
        for(j = k+1 ; j < n ; j++) {
            a[i] -= ans[j]*Map[i][j] ;
        }
        ans[k] = a[i] / Map[i][k] ;
        if( fabs(ans[k]) < eqs ) ans[k] = 0.0 ;
        vis[k] = 1 ;
    }
    for(i = 0 ; i < n ; i++) {
        if( !vis[i] )
            printf("Expected score for %c undefined\n", 'a'+i) ;
        else
            printf("Expected score for %c = %.3f\n", 'a'+i, ans[i]) ;
    }
    printf("\n") ;
}
int main() {
    int i , j , step = 0 ;
    char ch , k ;
    node p ;
    //freopen("1.in","r",stdin) ;
	//freopen("2.out","w",stdout) ;
    while( scanf("%d", &n) && n ) {
        getchar() ;
        for(i = 0 ; i < n ; i++) {
            gets(str) ;
            id = 5 ;
            p = dfs() ;
            for(j = 0 ; j < n ; j++)
                Map[i][j] = p.a[j] ;
            Map[i][ str[0]-'a' ] -= 1.0 ;
            a[i] = -p.k ;
        }
        printf("Game %d\n", ++step) ;
        solve() ;
    }
    return 0 ;
}

数据:

1

a = ((1 7) 6 ((8 3) 4))

2

a = (1 b)

b = (4 a)

1

a = (a a a)

3

a = (1 b c)

b = (a 2 c)

c = (a b 3)

5

a = (e (15 d (57 a) (45 (75 c))) b 189)

b = (d (a -29 30) ((b c)) ((51 a) (a 67) (e 79) (e a)))

c = (((d)))

d = (-1 (-98) ((a) c) (d e e (-81) ((a))) 58 (b))

e = (a b c d a b c d a b c d a b c d a b c d a b c d)

2

a = (1 (2 (3 (4 (5 (6 (7 (8 (9 (10 (11 (12 (13 (14 (15 b)))))))))))))))

b = (b b)

5

a = ((((((((((((((a))))))))))))))

b = (13 17 18 b d)

c = (a b c d)

d = (b 8 (d 7))

e = ((e) ((7)) (((((e))))))

7

a = ((4 5) 2 ((1 3) 7))

b = (9)

c = ((((((((((((((((((((((((((((((((((((99))))))))))))))))))))))))))))))))))))

d = ((1)(1)(1))

e = (1 (2 (3 (4 (5 (6 (7 (8 (9 (10 20))))))))))

f = (-1 1)

g = (-1 -2 -3 -4 -5 -6 1998 -1111)

2

a    =   (    17    (    18     b  ))

b  =           (      -19            a       (    8  a   )    ( 4   9))

26

a = ((-17 a) (z (z 3)))

b = (b 8 c g i h j)

c = ((a c m) (p r o g r a m m i n g) (c o n t e s t))

d = (1 6 (1 6 (1 6 (1 6 d d))))

e = ((((f)) g) h)

f = (17 18 19 (20 21))

g = (((((7 (n) 8))) 9 b d))

h = (((((((1 2 3 4 5 6 7 8) (8 7 6 5 4 3 2 1)))))))

i = (((((h a c)))))

j = ((1) (11) (1998))

k = ((((((((k l))))))))

l = (l k)

m = (1 9 2 0 -1)

n = (0 e)

o = (o h n o (8))

p = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 p p p p p p p p p p p p p p)

q = ((((((((w))))))))

r = (m c m)

s = (1 p 1 p 1 p 1 p s s s s s s s o m)

t = (a d f e g t s r p)

u = (u v w x y z)

v = (a a a a v v v v)

w = ( w y w y )

x = ((2) m o r e)

y = (q)

z = (1 -9 9 -8)

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

Game 4

Expected score for a = 1.750

Expected score for b = 2.000

Expected score for c = 2.250

Game 5

Expected score for a = 67.521

Expected score for b = 24.617

Expected score for c = 4.685

Expected score for d = 4.685

Expected score for e = 25.377

Game 6

Expected score for a undefined

Expected score for b undefined

Game 7

Expected score for a undefined

Expected score for b = 14.611

Expected score for c undefined

Expected score for d = 10.444

Expected score for e = 7.000

Game 8

Expected score for a = 3.667

Expected score for b = 9.000

Expected score for c = 99.000

Expected score for d = 1.000

Expected score for e = 2.008

Expected score for f = 0.000

Expected score for g = 108.250

Game 9

Expected score for a = 13.759

Expected score for b = 3.034

Game 10

Expected score for a = -6.042

Expected score for b = 120.778

Expected score for c = 5.593

Expected score for d = 3.500

Expected score for e = 15.712

Expected score for f = 18.625

Expected score for g = 35.224

Expected score for h = 4.500

Expected score for i = 1.350

Expected score for j = 670.000

Expected score for k undefined

Expected score for l undefined

Expected score for m = 2.200

Expected score for n = 7.856

Expected score for o = 6.785

Expected score for p = 0.000

Expected score for q undefined

Expected score for r = 3.331

Expected score for s = 1.299

Expected score for t = 8.956

Expected score for u undefined

Expected score for v = -6.042

Expected score for w undefined

Expected score for x = 6.006

Expected score for y undefined

Expected score for z = -1.750

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-18 05:26:56

poj1487--Single-Player Games(高斯消元)+测试数据的相关文章

hdu 3915 高斯消元

Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 285 Problem Description Mr.Frost is a child who is too simple, sometimes naive, always plays some simple but i

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

线代之高斯消元

数学上,高斯消元法(或译:高斯消去法),是线性代数规划中的一个算法,可用来为线性方程组求解.但其算法十分复杂,不常用于加减消元法,求出矩阵的秩,以及求出可逆方阵的逆矩阵.不过,如果有过百万条等式时,这个算法会十分省时.一些极大的方程组通常会用迭代法以及花式消元来解决.当用于一个矩阵时,高斯消元法会产生出一个"行梯阵式".高斯消元法可以用在电脑中来解决数千条等式及未知数.亦有一些方法特地用来解决一些有特别排列的系数的方程组. 2968: Lights  Time Limit(Common

HDU5088——Revenge of Nim II(高斯消元&amp;矩阵的秩)(BestCoder Round #16)

Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided th

HDU5088(高斯消元)

Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 443    Accepted Submission(s): 157 Problem Description Nim is a mathematical game of strategy in which two players take turns r

hdu-5833 Zhu and 772002(高斯消元)

题目链接: Zhu and 772002 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1006    Accepted Submission(s): 348 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test the

poj1830--开关问题(高斯消元问题1)

开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6021   Accepted: 2290 Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关,如果为关就变为开.你的目标是经过若干次开关操作后使得最后N个开关达到一个特定的状态.对于任意一个开关,最多只能进行一次开关操作

hdu3949 XOR xor高斯消元

XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1364    Accepted Submission(s): 402 Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base number A

hdu 3992 AC自动机上的高斯消元求期望

Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 391    Accepted Submission(s): 109 Problem Description There was a crazy typewriter before. When the writer is not very sober, it