poj 1487 Single-Player Games
2013-07-31 19:41
363 查看
主要考察表达式的解析和高斯消元!!!
View Code
#include<iostream> #include<stdio.h> #include<algorithm> #include<iomanip> #include<cmath> #include<cstring> #include<vector> #define ll __int64 #define pi acos(-1.0) #define SIZE 1024 using namespace std; const double eps=1e-8; inline bool zero(double x) { return fabs(x)<eps; } struct node { int type,val; node(){} node(int _type,int _val){ type=_type; val=_val; } }an[SIZE]; vector<int> g[SIZE]; double mat[32][32]; int n,ind; char *p; void build(int now) { int t,m; while(*p){ while(*p==' '&&*p) p++; if(!*p) break; if((*p>='0'&&*p<='9')||*p=='-'){ ind++; sscanf(p,"%d%n",&t,&m); p+=m; an[ind]=node(0,t); g[now].push_back(ind); } else if(*p=='(') { ind++; an[ind]=node(-1,0); p++; g[now].push_back(ind); build(ind); } else if(*p==')') { p++; return; } else{ ind++; an[ind]=node(1,*p-'a'); p++; g[now].push_back(ind); } } } void toMat(int now,double tp,int var) { double p; if(g[now].size()) p=tp/g[now].size(); for(int i=0;i<(int)g[now].size();i++){ int x=g[now][i]; if(an[x].type==-1) toMat(x,p,var); else{ if(an[x].type==0) mat[var] +=an[x].val*p; else{ mat[var][an[x].val]-=p; } } } } void init() { ind=0; memset(an,0,sizeof(an)); for(int i=0;i<SIZE;i++) g[i].clear(); } void Gauss_line(int a,int b,int col) { double mul_a=mat[a][col]; double mul_b=mat[b][col]; for(int i=0;i<=n;i++) mat[b][i]=mat[b][i]-mat[a][i]*mul_b/mul_a; } void Gauss() { for(int row=0,col=0;row<n&&col<n;row++,col++){ int ptr=-1; for(int i=row;i<n;i++) if(!zero(mat[i][col])){ ptr=i; break; } if(ptr==-1) continue; else{ for(int i=0;i<=n;i++) swap(mat[row][i],mat[ptr][i]); for(int i=0;i<n;i++) if(i!=row) Gauss_line(row,i,col); } } } double getans(int x) { return mat[x] /mat[x][x]; } bool check(int x) { for(int i=0;i<n;i++) if(i!=x&&!zero(mat[x][i])) return false; return !zero(mat[x][x]); } int main(){ int m,i,j,t=1; char cmd[SIZE]; while(cin>>n&&n){ memset(mat,0,sizeof(mat)); printf("Game %d\n",t++); for(int i=0;i<n;i++){ init(); do{ gets(cmd); }while(*cmd=='\0'); p=cmd; while(*p!='(') p++; build(0); toMat(0,1.,i); mat[i][i]+=1.0; } Gauss(); for(i=0;i<n;i++){ if(check(i)) printf("Expected score for %c = %.3lf\n",'a'+i,getans(i)); else printf("Expected score for %c undefined\n",'a'+i); } printf("\n"); } return 0; }
View Code
相关文章推荐
- poj 1487 Single-Player Games(高斯消元+字符串处理)
- POJ 1487:Single-Player Games 浮点数高斯消元
- POJ 1487:Single-Player Games 浮点数高斯消元
- POJ 1487 Single-Player Games
- POJ 1487 Single-Player Games
- poj 1487 Single-Player Games 高斯消元
- poj_1487 Single-Player Games(高斯消元+自由元)
- 【POJ 1487】Single-Player Games(建方程高消)
- 【POJ1487】——Single-Player Games(高斯消元)
- poj1487--Single-Player Games(高斯消元)+测试数据
- Single-Player Games hdoj
- hdu 3413 poj 3778 Single CPU, multi-tasking
- POJ 1487 高斯消元+模拟……坑爹题~好题
- poj 3779 hdu 3413 Single CPU, multi-tasking
- poj 2138 Travel Games
- poj 3779 Single CPU, multi-tasking(模拟)
- uva 664 single player game
- poj 2138 Travel Games 搜索+自动机水题
- poj 3258 River Hopscotch 【二分查找】(The Frog's Games 相似题)
- POJ 1487 表达式解析+高斯消元法