Blur(水---题)

You have a black and white image that is w pixels wide and h pixels high. You decide to represent
this image with one number per pixel: black is 0, and white is 1. Your friend asks you to blur the
image, resulting in various shades of gray. The way you decide to blur the image is as follows: You
create a new image that is the same size as the old one, and each pixel in the new image has a value
equal to the average of the 9 pixels in the 3 × 3 square centered at the corresponding old pixel.
When doing this average, wrap around the edges, so the left neighbor of a leftmost pixel is in the
rightmost column of the same row, and the top neighbor of an uppermost pixel is on the bottom in
the same column. This way, the 3 × 3 square always gives you exactly 9 pixels to average together.
If you want to make the image blurrier, you can take the blurred image and blur it again using the
exact same process.
Given an input image and a fixed number of times to blur it, how many distinct shades of gray
does the final image have, if all the arithmetic is performed exactly?
Warning: Floating point numbers can be finicky; you might be surprised to learn, for example,
that 2/9 + 5/9 may not equal 3/9 + 4/9 if you represent the fractions with floating point numbers!
Can you figure out how to solve this problem without using floating point arithmetic?
Input
The first line of input contains three space-separated integers w, h, and b (3 ≤ w, h ≤ 100, 0 ≤
b ≤ 9), denoting the width and height of the image, and the number of times to blur the image,
respectively. The following h lines of w space-separated integers describe the original image, with
each integer being either 0 or 1, corresponding to the color of the pixel.
Output
Output, on a single line, a single integer equal to the number of distinct shades of gray in the final
image.
Sample Input Sample Output
5 4 1
0 0 1 1 0
0 0 1 1 0
0 0 1 1 0
0 0 1 1 0
3
Sample Input Sample Output
3 3 2
1 0 0
0 1 0
0 1 0
1

题目不好翻译,不过翻译过之后题就变得很水。

给个m*n的矩阵(注意先输入的是n,后输入的是m),然后进行b次操作,
操作的含义为:把当前位置的数更换为它周围3*3矩阵所有数和的平均值。如果数在第一行,那么它的上一行是最后一行,其它情况也是如此。
问最后矩阵中有多少个不同的数

开一个三维数组,第一维只需要开到b的数据量即可,每次新的矩阵的值都有上一个矩阵变换得来,即 a [ b ] [ i ] [ j ] =a [ b - 1 ] [ i ] [ j ];

最后不能用哈希数组去重,会RE在第三组,把值扔在set容器里就行。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a)  memset(a,0,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define eps 1.0e-8
//#define e  2.718281828459045
using namespace std;
typedef long long int LL;
const int MAXL(1e6);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
int dir1[10]= {-1,0,1,-1,0,1,-1,0,1};
int dir2[10]= {-1,-1,-1,0,0,0,1,1,1};
int a[20][110][110];
int haxi[MAXL+50];
int main()
{
    memset(a);
    memset(haxi);
    int n,m,b;
    scanf("%d%d%d",&n,&m,&b);
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
            scanf("%d",&a[0][i][j]);
    for(int t=1; t<=b; t++)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                for(int k=0; k<9; k++)
                {
                    a[t][i][j]+=(a[t-1][(i+dir1[k]+m)%m][(j+dir2[k]+n)%n]);
                }
            }
        }
    }
    set<int>st;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            st.insert(a[b][i][j]);
    cout<<st.size()<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值