您的位置:首页 > 其它

hdu 3359(高斯消元)

2016-01-24 17:55 387 查看

Kind of a Blur

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


[align=left]Problem Description[/align]
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.







 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

    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.

题意: H * W (W,H <= 10) 的矩阵A的某个元素A[i][j],从它出发到其他点的曼哈顿距离小于等于D的所有值的和S[i][j]除上可达点的数目,构成了矩阵B。给定矩阵B,求矩阵A。
一直在WA,而且是在93ms,在网上copy别人的AC代码,93ms,不知道错在哪里。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 110;
const double eps = 1e-8;
double A[maxn][maxn],B[maxn][maxn];
int w,h,d;

void build_matrix()
{
for(int row = 0; row < h; row++)
for(int col = 0; col < w; col++)
{
int cnt = 0;
for(int i = 0; i < h; i++)	//查找manhatthan distance<d的点
for(int j = 0; j < w; j++)
{
if(abs(i-row)+abs(j-col) <= d)
{
A[row*h+col][i*h+j] = 1;
cnt++;
}
}
A[row*h+col][w*h] = cnt*B[row][col];
}
}

void Gauss(int n)
{
int col = 0,row = 0,j,k,r;
while(col < n && row < n) //处理第row个方程,第col个变量
{
r = row;
for(k = row+1; k < n; k++)
{
if(A[k][col] > A[r][col])
r = k;
}
if(abs(A[r][col]) > eps)
{
if(r != row)
{
for(k = col; k <= n; k++)
swap(A[r][k],A[row][k]);
}
for(k = row+1; k < n; k++)
{
double f = A[k][col]/A[row][col];
for(j = col; j <= n; j++)
A[k][j] -= f*A[row][j];
}
row++;
}
col++;
}
//回代求解过程
for(k = n-1; k >= 0; k--)
{
for(j = k+1; j < n; j++)
A[k]
-= A[j]
*A[k][j];
A[k]
/= A[k][k];
}
}

int main()
{
while(scanf("%d%d%d",&w,&h,&d)!=EOF && h)
{

for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
scanf("%lf",&B[i][j]);
memset(A,0,sizeof(A));
build_matrix();
Gauss(w*h);
//输出解
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
printf("%8.2f",A[i*h+j][w*h]);
}
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学