HDU 3359 (浮点数高斯消元)

Kind of a Blur

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


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:
HDU 3359 (浮点数高斯消元)_第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.
HDU 3359 (浮点数高斯消元)_第2张图片 HDU 3359 (浮点数高斯消元)_第3张图片 HDU 3359 (浮点数高斯消元)_第4张图片
 

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.
 


题意是给出一个按照题目所给的公式求出的矩阵,求原来的矩阵。保证有唯一解。

直接上高斯消元。

#include <bits/stdc++.h>
using namespace std;
#define maxn 111
#define eps 1e-9

double a[maxn][maxn], x[maxn], cnt[maxn][maxn];
int w, h, d;
double mp[11][11];
int equ, var;

int Gauss () {
    int i, j, k, col, max_r;
    for (k = 0, col = 0; k < equ && col < var; k++, col++) {
        max_r = k;
        for (int i = k+1; i < equ; i++) { //找到最大的数所在的行
            if (fabs (a[i][col]) > fabs (a[max_r][col]))
                max_r = i;
        }
        if (k != max_r) { //交换行
            for (int j = col; j <= var; j++) {
                swap (a[k][j], a[max_r][j]);
            }
        }
        for (int i = k+1; i < equ; i++) { //消去
            if (a[i][col]) {
                double tmp = -a[i][col]/a[k][col];
                for (int j = col; j <= var; j++) {
                    a[i][j] += tmp*a[k][j];
                }
            }
        }
    }
    for (int i = equ-1; i >= 0; i--) { //回代
        double tmp = a[i][var];
        for (int j = i+1; j < var; j++) {
            tmp -= x[j]*a[i][j];
        }
        x[i] = tmp/a[i][i];
    }
    return 1;
}

int main () {
    //freopen ("in", "r", stdin);
    int kase = 0;
    while (cin >> w >> h >> d && w+h) {
        if (kase++) cout << endl;
        memset (cnt, 0, sizeof cnt);
        memset (a, 0, sizeof a);
        memset (x, 0, sizeof x);
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                cin >> mp[i][j];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                for (int x = 0; x < h; x++) {
                    for (int y = 0; y < w; y++) {
                        if (abs(i-x)+abs(j-y) <= d) {
                            cnt[i][j] += 1;
                            a[i*w+j][x*w+y] = 1.0;
                        }
                    }
                }
            }
        }
        equ = h*w, var = h*w;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                a[i*w+j][var] = mp[i][j]*cnt[i][j];
            }
        }
        Gauss ();
        int cur = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                printf ("%8.2f", x[cur++]);
            }
            printf ("\n");
        }
    }
    return 0;
}


你可能感兴趣的:(HDU 3359 (浮点数高斯消元))