题意:给出一个h*w的矩阵,每个值等于与它哈密顿距离不超过d的平均值,求原矩阵。
构造方程组,高斯消元,注意行/列,消元要取最大的。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #define MAXN 110 6 using namespace std; 7 double a[MAXN][MAXN], g[MAXN][MAXN], x[MAXN]; 8 int pos[MAXN][MAXN]; 9 int n, m, d, cnt; 10 int MHT(int x1, int y1, int x2, int y2) { 11 return abs(x1 - x2) + abs(y1 - y2); 12 } 13 void Gauss() { 14 int i, j, k; 15 double tmp, big; 16 for (i = 0; i < cnt; i++) { 17 for (big = 0, j = i; j < cnt; j++) { 18 if (abs(g[j][i]) > big) { 19 big = abs(g[j][i]); 20 k = j; 21 } 22 } 23 if (k != i) { 24 for (j = 0; j <= cnt; j++) 25 swap(g[i][j], g[k][j]); 26 } 27 for (j = i + 1; j < cnt; j++) { 28 if (g[j][i]) { 29 tmp = -g[j][i] / g[i][i]; 30 for (k = i; k <= cnt; k++) 31 g[j][k] += tmp * g[i][k]; 32 } 33 } 34 } 35 for (i = cnt - 1; i >= 0; i--) { 36 tmp = 0; 37 for (j = i + 1; j < cnt; j++) 38 tmp += g[i][j] * x[j]; 39 x[i] = (g[i][j] - tmp) / g[i][i]; 40 } 41 } 42 int main() { 43 int i, j, k, l; 44 bool flag = true; 45 while (scanf("%d%d%d", &m, &n, &d), n) { 46 if (flag) 47 flag = false; 48 else 49 putchar('\n'); 50 for (i = cnt = 0; i < n; i++) { 51 for (j = 0; j < m; j++) { 52 scanf("%lf", &a[i][j]); 53 pos[i][j] = cnt++; 54 } 55 } 56 memset(g, 0, sizeof(g)); 57 for (i = 0; i < n; i++) { 58 for (j = 0; j < m; j++) { 59 for (k = 0; k < n; k++) { 60 for (l = 0; l < m; l++) { 61 if (MHT(i, j, k, l) <= d) { 62 g[pos[i][j]][pos[k][l]] = 1; 63 g[pos[i][j]][cnt] += a[i][j]; 64 } 65 } 66 } 67 } 68 } 69 Gauss(); 70 for (i = 0; i < n; i++) { 71 for (j = 0; j < m; j++) 72 printf("%8.2lf", x[i * m + j]); 73 putchar('\n'); 74 } 75 } 76 return 0; 77 }