hdu3359 高斯消元(实数)模板:给定高斯模糊后的矩阵求原矩阵
额很裸的现在才看到==
就是把原矩阵设N×M个未知量直接代入高斯消元==
目前手打基本上无压力了,下面把5009做了高斯消元可以放一下了==
(突然发现两个问题,整数的高消还没怎么写过?小数精度大的还没看到过?额这周末前弄好
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 #define eps 1e-10 7 int n,m; 8 int cal(int i,int j) 9 { 10 return (i-1)*m+j; 11 } 12 double g[105][105],x[105],a[15][15]; 13 int row,col; 14 int guass() 15 { 16 int tr,tc,i,j,max_r; 17 for (tr=1,tc=1;tc<=col;tc++) 18 { 19 max_r=tr; 20 for (i=tr;i<=row;i++) 21 if (fabs(g[i][tc])>fabs(g[max_r][tc])) max_r=i; 22 if (fabs(g[max_r][tc])<eps) return 0; 23 if (max_r!=tr) 24 { 25 for (j=tc;j<=col;j++) swap(g[tr][j],g[max_r][j]); 26 swap(x[tr],x[max_r]); 27 } 28 for (j=tc+1;j<=col;j++) g[tr][j]/=g[tr][tc]; 29 x[tr]/=g[tr][tc]; g[tr][tc]=1; 30 for (i=1;i<=row;i++) 31 if (i!=tr) 32 { 33 for (j=tc+1;j<=col;j++) g[i][j]-=g[i][tc]*g[tr][j]; 34 x[i]-=g[i][tc]*x[tr]; g[i][tc]=0; 35 } 36 tr++; 37 } 38 return 1; 39 } 40 int main() 41 { 42 int judge=0,d,i,j,id,tmp_id,ans,ti,tj; 43 while (~scanf("%d%d%d",&m,&n,&d)&&n) 44 { 45 memset(g,0,sizeof(g)); 46 memset(x,0,sizeof(x)); 47 for (i=1;i<=n;i++) 48 for (j=1;j<=m;j++) scanf("%lf",&a[i][j]); 49 for (i=1;i<=n;i++) 50 for (j=1;j<=m;j++) 51 { 52 id=cal(i,j); 53 ans=0; 54 for (ti=1;ti<=n;ti++) 55 for (tj=1;tj<=m;tj++) 56 if (abs(i-ti)+abs(j-tj)<=d) ans++; 57 for (ti=1;ti<=n;ti++) 58 for (tj=1;tj<=m;tj++) 59 if (abs(i-ti)+abs(j-tj)<=d){ 60 tmp_id=cal(ti,tj); 61 g[id][tmp_id]+=1.0/ans; 62 } 63 x[id]+=a[i][j]; 64 } 65 row=col=n*m; 66 guass(); 67 if (judge) printf("\n"); judge=1; 68 for (i=1;i<=n;i++) 69 for (j=1;j<=m;j++) 70 { 71 id=cal(i,j); 72 printf("%8.2lf",x[id]); 73 if (j==m) printf("\n"); 74 } 75 } 76 return 0; 77 }