【模拟】poj 1009 Edge Detection

/*
poj 1009 Edge Detection
题意: 给出一副图要求刻画出另一幅图,刻画的图上每个像素值是取原图周围8个像素值与其像素值的最大差绝对值。
题解:模拟题
对于每一个变化点,一定是处于每个分界点所影响的位置,对于图来讲,我们就可以对分界点相邻及自身的9个格进行计算即可。
这题给出图的宽度,对于每对数据给出的pos,我们可以模拟出它相应的二维位置解决图的二维表示。

坑点: 光想着把有意义的pair作为分界点,最后结束也应该作为分界点,所以要把(0,0)这对也作为分界点。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
pair<int,int> a[N];
struct asd  //改变的点
{
    int pos;
    int val;
}ans[10*N];
int n;
int pos_sum;
int get_value(int pos)
{
    int i = 0;
    int now = 0;
    while(now <= pos)
        now += a[i++].second;
    return a[i-1].first;
}
int cal(int pos)
{
    int h = get_value(pos);
    int x = pos/n;
    int y = pos%n;
    int mx = 0;
    for(int j = x-1; j <= x+1; j++)
    {
        for(int k = y-1; k <= y+1; k++)
        {
            int pos_now = j*n+k;
            if(!(j==x && k==y) && k < n && k >= 0 && j >= 0 && pos_now < pos_sum)
            {
                int f = get_value(pos_now);
                mx = max(mx,abs(h-f));
            }
        }
    }
    return mx;
}
bool cmp(asd x,asd y)
{
    return x.pos < y.pos;
}
int main()
{
    while(cin >> n)
    {
        if(n == 0)
        {
            puts("0");
            break;
        }
        int pair_num;
        pos_sum = 0;
        for(int i = 0; ; i++)
        {
            int v,f;
            cin >> v >> f;
            if(v==0 && f==0)
            {
                a[i] = make_pair(v,f);
                pair_num = i;
                break;
            }
            a[i] = make_pair(v,f);
            pos_sum += a[i].second;
        }
        int pos = 0;  //分界点的pos
        int m = 0;
        for(int i = 0; i <= pair_num; i++)
        {
            int x = pos/n;
            int y = pos%n;
                        //cout << x << "  " << y << endl;

            for(int j = x-1; j <= x+1; j++)
            {
                for(int k = y-1; k <= y+1; k++)
                {
                    int pos_now = j*n+k;
                    if(k < n && k >= 0 && j >= 0 && pos_now < pos_sum)
                    {
                        ans[m].pos = pos_now;
                        ans[m++].val = cal(pos_now);
                    }
                }
            }
            pos += a[i].second;
        }
        sort(ans,ans+m,cmp);
        cout << n << endl;
        pos = 0;
        for(int i = 1; i < m; i++)
        {
            if(ans[i].val == ans[i-1].val)
                continue;
            else
            {
                int x = pos/n;
                int y = pos%n;
                printf("%d %d\n",ans[i-1].val,ans[i].pos-pos);
                pos = ans[i].pos;
            }
        }
        int x = pos_sum - pos;
        printf("%d %d\n",ans[m-1].val,x);
        printf("0 0\n");
    }
    return 0;
}
/*
30
10 41
20 41
15 41
30 41
25 41
0 5
0 0

30
0 10
10 62
5 20
15 62
5 20
25 6
5 15
0 9
25 6
0 0
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值