2015-2016 ACM-ICPC Blur

题目地址:http://codeforces.com/gym/100819/attachments

题目:

You have a black and white image that is w pixels wide and h pixels high. You decide to representthis image with one number per pixel: black is 0, and white is 1. Your friend asks you to blur theimage, resulting in various shades of gray. The way you decide to blur the image is as follows: Youcreate a new image that is the same size as the old one, and each pixel in the new image has a valueequal 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 therightmost column of the same row, and the top neighbor of an uppermost pixel is on the bottom inthe 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 theexact same process.

Given an input image and a fixed number of times to blur it, how many distinct shades of graydoes 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, witheach 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 finalimage.


思路:

emmm..pdf真难复制。

这题的坑在于,如果到了边界的话,旁边没有数字时要绕道另一个边界去读数。比赛时我就是这么wa的……

然后这题不能用浮点数,不然精度不行。反正都是九个格子啦,就用整数储存就行了。

每一个格子去读取以他为中心的九宫格,然后相加,储存到另一个数组里,一遍模糊后再传回给原来的数组。

最后数一数有几个不同的数字,用set就行了。

代码:

#include<iostream>
#include<set>
using namespace std;
int dx[9]={-1,-1,-1,0,0,0,1,1,1},dy[9]={-1,0,1,-1,0,1,-1,0,1};
long long int a[101][101];
long long int wa[101][101];
void blur(int x,int y)
{
    int sum=0;
    for(int i=0;i<9;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        sum=sum+a[nx][ny];
    }
    wa[x][y]=sum;
}
int main()
{
    int w,h,b;
    cin>>h>>w>>b;
    for(int i=1;i<=w;i++)
    {
        for(int j=1;j<=h;j++)
        {
            cin>>a[i][j];
        }
    }
    a[0][0]=a[w][h];a[0][h+1]=a[w][1];a[w+1][0]=a[1][h];a[w+1][h+1]=a[1][1];
    for(int j=1;j<=h;j++)
    {
        a[0][j]=a[w][j];
    }
    for(int j=1;j<=h;j++)
    {
        a[w+1][j]=a[1][j];
    }
    for(int i=0;i<=w;i++)
    {
        a[i][0]=a[i][h];
    }
    for(int i=0;i<=w;i++)
    {
        a[i][h+1]=a[i][1];
    }
    while(b--){
    for(int i=1;i<=w;i++)
    {
        for(int j=1;j<=h;j++)
        {
            blur(i,j);
        }
    }
    for(int i=1;i<=w;i++)
    {
        for(int j=1;j<=h;j++)
        {
            a[i][j]=wa[i][j];
        }
    }
    a[0][0]=a[w][h];a[0][h+1]=a[w][1];a[w+1][0]=a[1][h];a[w+1][h+1]=a[1][1];
    for(int j=1;j<=h;j++)
    {
        a[0][j]=a[w][j];
    }
    for(int j=1;j<=h;j++)
    {
        a[w+1][j]=a[1][j];
    }
    for(int i=0;i<=w;i++)
    {
        a[i][0]=a[i][h];
    }
    for(int i=0;i<=w;i++)
    {
        a[i][h+1]=a[i][1];
    }
    }
    set<long long int>s;
    for(int i=1;i<=w;i++)
    {
        for(int j=1;j<=h;j++)
        {
            s.insert(a[i][j]);
        }
    }
    cout<<s.size()<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zero_979/article/details/80738721