HDU3359(SummerTrainingDay05-I 高斯消元)

Posted Penn000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3359(SummerTrainingDay05-I 高斯消元)相关的知识,希望对你有一定的参考价值。

Kind of a Blur

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2754    Accepted Submission(s): 751


Problem Description

Image blurring occurs when the object being captured is out of the camera‘s focus. The top two figures on the right are an example of an image and its blurred version. Restoring the original image given only the blurred version is one of the most interesting topics in image processing. This process is called deblurring, which will be your task for this problem.
In this problem, all images are in grey-scale (no colours). Images are represented as a 2 dimensional matrix of real numbers, where each cell corresponds to the brightness of the corresponding pixel. Although not mathematically accurate, one way to describe a blurred image is through averaging all the pixels that are within (less than or equal to) a certain Manhattan distance?from each pixel (including the pixel itself ). Here‘s an example of how to calculate the blurring of a 3x3 image with a blurring distance of 1:
技术分享

Given the blurred version of an image, we are interested in reconstructing the original version assuming that the image was blurred as explained above.
技术分享技术分享技术分享
 

 

Input

Input consists of several test cases. Each case is specified on H + 1 lines. The first line specifies three non negative integers specifying the width W, the height H of the blurred image and the blurring distance D respectively where (1<= W,H <= 10) and (D <= min(W/2,H/2)). The remaining H lines specify the gray-level of each pixel in the blurred image. Each line specifies W non-negative real numbers given up to the 2nd decimal place. The value of all the given real numbers will be less than 100.
Zero or more lines (made entirely of white spaces) may appear between cases. The last line of the input file consists of three zeros.
 

 

Output

For each test case, print a W * H matrix of real numbers specifying the deblurred version of the image. Each element in the matrix should be approximated to 2 decimal places and right justified in a field of width 8. Separate the output of each two consecutive test cases by an empty line. Do not print an empty line after the last test case. It is guaranteed that there is exactly one unique solution for every test case.
 

 

Sample Input

2 2 1 1 1 1 1 3 3 1 19 14 20 12 15 18 13 14 16 4 4 2 14 15 14 15 14 15 14 15 14 15 14 15 14 15 14 15 0 0 0
 

 

Sample Output

1.00 1.00 1.00 1.00 2.00 30.00 17.00 25.00 7.00 13.00 14.00 0.00 35.00 1.00 27.00 2.00 28.00 21.00 12.00 17.00 8.00 21.00 12.00 17.00 8.00 1.00 27.00 2.00 28.00

Hint

The Manhattan Distance (sometimes called the Taxicab distance) between two points is the sum of the (absolute) difference of their coordinates. The grid on the lower right illustrates the Manhattan distances from the grayed cell.
 

 

Source

 
高斯消元,居然是先输入宽,再输入高,被这个WA了好几发。。。
 1 //2017-08-05
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 
10 const int N = 15;
11 const double eps = 1e-9;
12 int n, m, d;
13 double G[N][N], A[N*N][N*N], x[N*N];
14 int equ, var;
15 
16 int Gauss(){
17     int i, j, k, col, max_r;
18     for(k = 0, col = 0; k < equ && col < var; k++, col++){
19         max_r = k;
20         for(i = k+1; i < equ; i++)
21               if(fabs(A[i][col]) > fabs(A[max_r][col]))
22                   max_r = i;
23         if(fabs(A[max_r][col]) < eps)return 0;
24         if(k != max_r){
25             for(j = col; j < var; j++)
26                   swap(A[k][j], A[max_r][j]);
27             swap(x[k], x[max_r]);
28         }
29         x[k] /= A[k][col];
30         for(j = col+1; j < var; j++)
31               A[k][j] /= A[k][col];
32         A[k][col] = 1;
33         for(i = 0; i < equ; i++)
34               if(i != k){
35                   x[i] -= x[k]*A[i][k];
36                   for(j = col+1; j < var; j++)
37                     A[i][j] -= A[k][j]*A[i][col];
38                 A[i][col] = 0;
39               }
40     }
41     return 1;
42 }
43 
44 int main()
45 {
46     bool fg = true;
47     while(scanf("%d%d%d", &m, &n, &d)!=EOF){
48         if(!n && !m)break;
49         if(!fg)printf("\n");
50         fg = false;
51         memset(A, 0, sizeof(A));
52         for(int i = 0; i < n; i++)
53               for(int j = 0; j < m; j++){
54                   scanf("%lf", &G[i][j]);
55                 x[i*m+j] = G[i][j];
56             }
57         for(int i = 0; i < n*m; i++){
58             int cnt = 0;
59             for(int j = 0; j < n*m; j++){
60                 int x = i/m;
61                 int y = i%m;
62                 int dx = j/m;
63                 int dy = j%m;
64                 if(abs(x-dx)+abs(y-dy) <= d){
65                     A[i][j] = 1.0;
66                     cnt++;
67                 }else A[i][j] = 0.0;
68             }
69             x[i] *= cnt;
70         }
71         equ = n*m;
72         var = n*m;
73         Gauss();
74         for(int i = 0; i < n*m; i++){
75             if(i % m == m-1)printf("%8.2lf\n", x[i]);
76             else printf("%8.2lf", x[i]);
77         }
78     }
79 
80     return 0;
81 }

 











以上是关于HDU3359(SummerTrainingDay05-I 高斯消元)的主要内容,如果未能解决你的问题,请参考以下文章

HDU4417(SummerTrainingDay08-N 主席树)

HDU2157(SummerTrainingDay05-F dp)

HDU4704(SummerTrainingDay04-A 欧拉降幂公式)

HDU3534(SummerTrainingDay13-C tree dp)

HDU2167(SummerTrainingDay02-D 状态压缩dp)

HDU4845(SummerTrainingDay02-C 状态压缩bfs)