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

2009 ANARC

高斯消元,居然是先输入宽,再输入高,被这个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 }
时间: 2024-11-04 14:55:14

HDU3359(SummerTrainingDay05-I 高斯消元)的相关文章

学习高斯消元

点击打开链接     习题  行列式 高斯消元问题类型: 用LCM 保持整型 1. 基本的高斯消元,裸模板 HDU3359 2. 开关问题,用^操作代替 -, 求x[i]时候一样用*  poj 1222 1830 1753 3. 枚举自由变元, return -1 是因为出现[0,0,0,0,a]这种情况,return 0 是唯一解,否则是有自由变元 4. 取模方程 (a1*x1+a2*x2...)%P=b在初等行变换后,每次进行消元的时候将所有值模P,最后求解回带的时候利用扩展欧几里得来对每一

poj_1222_高斯消元

第一次学习使用高斯消元,将灯板化为线性方程组,进行求解. /*######################################################################### # File Name: poj_1222.cpp # Author: CaoLei # Created Time: 2015/7/20 15:48:04 ###################################################################

HDU 4870 Rating(高斯消元)

HDU 4870 Rating 题目链接 题意:一个人注册两个账号,初始rating都是0,他每次拿低分的那个号去打比赛,赢了加50分,输了扣100分,胜率为p,他会打到直到一个号有1000分为止,问比赛场次的期望 思路:f(i, j)表示i >= j,第一个号i分,第二个号j分时候,达到目标的期望,那么可以列出转移为f(i, j) = p f(i', j') + (1 - p) f(i'' + j'') + 1 f(i', j')对应的是赢了加分的状态,f(i'', j'')对应输的扣分的状态

【BZOJ 4171】 4171: Rhl的游戏 (高斯消元)

4171: Rhl的游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 74  Solved: 33[Submit][Status][Discuss] Description RHL最近迷上一个小游戏:Flip it.游戏的规则很简单,在一个N*M的格子上,有一些格子是黑色,有一些是白色 .每选择一个格子按一次,格子以及周围边相邻的格子都会翻转颜色(边相邻指至少与该格子有一条公共边的格子 ),黑变白,白变黑.RHL希望把所有格子都变成白色的.不幸

POJ 1830 开关问题 高斯消元,自由变量个数

http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被操作一次,记得a[i][i] = 1是必须的,因为开关i操作一次,本身肯定会变化一次. 所以有n个开关,就有n条方程, 每个开关的操作次数总和是:a[i][1] + a[i][2] + ... + a[i][n] 那么sum % 2就代表它的状态,需要和(en[i] - be[i] + 2) % 2

BZOJ 3105: [cqoi2013]新Nim游戏 [高斯消元XOR 线性基]

以后我也要用传送门! 题意:一些数,选择一个权值最大的异或和不为0的集合 终于有点明白线性基是什么了...等会再整理 求一个权值最大的线性无关子集 线性无关子集满足拟阵的性质,贪心选择权值最大的,用高斯消元判断是否和已选择的线性相关 每一位记录pivot[i]为i用到的行 枚举要加入的数字的每一个二进制为1的位,如果有pivot[i]那么就异或一下(消元),否则pivot[i]=这个数并退出 如果最后异或成0了就说明线性相关... #include <iostream> #include &l

[bzoj1013][JSOI2008]球形空间产生器sphere-题解[高斯消元]

Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁这个球形空间产生器. Input 第一行是一个整数n(1<=N=10).接下来的n+1行,每行有n个实数,表示球面上一点的n维坐标.每一个实数精确到小数点后6位,且其绝对值都不超过20000. Output 有且只有一行,依次给出球心的n维坐标(n个实数),两个实数之间用一个空格隔开.每个实数精确到

[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be c

UVA 10828 Back to Kernighan-Ritchie(高斯消元)

高斯消元求概率 对于非起点,期望x[i] = ∑x[j] / deg[j] #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #includ