zoukankan      html  css  js  c++  java
  • L3-018 森森美图 (30 分)

    森森最近想让自己的朋友圈熠熠生辉,所以他决定自己写个美化照片的软件,并起名为森森美图。众所周知,在合照中美化自己的面部而不美化合照者的面部是让自己占据朋友圈高点的绝好方法,因此森森美图里当然得有这个功能。 这个功能的第一步是将自己的面部选中。森森首先计算出了一个图像中所有像素点与周围点的相似程度的分数,分数越低表示某个像素点越“像”一个轮廓边缘上的点。 森森认为,任意连续像素点的得分之和越低,表示它们组成的曲线和轮廓边缘的重合程度越高。为了选择出一个完整的面部,森森决定让用户选择面部上的两个像素点A和B,则连接这两个点的直线就将图像分为两部分,然后在这两部分中分别寻找一条从A到B且与轮廓重合程度最高的曲线,就可以拼出用户的面部了。 然而森森计算出来得分矩阵后,突然发现自己不知道怎么找到这两条曲线了,你能帮森森当上朋友圈的小王子吗?

    为了解题方便,我们做出以下补充说明:

    • 图像的左上角是坐标原点(0,0),我们假设所有像素按矩阵格式排列,其坐标均为非负整数(即横轴向右为正,纵轴向下为正)。
    • 忽略正好位于连接A和B的直线(注意不是线段)上的像素点,即不认为这部分像素点在任何一个划分部分上,因此曲线也不能经过这部分像素点。
    • 曲线是八连通的(即任一像素点可与其周围的8个像素连通),但为了计算准确,某像素连接对角相邻的斜向像素时,得分额外增加两个像素分数和的2​​倍减一。例如样例中,经过坐标为(3,1)和(4,2)的两个像素点的曲线,其得分应该是这两个像素点的分数和(2+2),再加上额外的(2+2)乘以(2​​1),即约为5.66。

    输入格式:

    输入在第一行给出两个正整数N和M(5),表示像素得分矩阵的行数和列数。

    接下来N行,每行M个不大于1000的非负整数,即为像素点的分值。

    最后一行给出用户选择的起始和结束像素点的坐标(和(。4个整数用空格分隔。

    输出格式:

    在一行中输出划分图片后找到的轮廓曲线的得分和,保留小数点后两位。注意起点和终点的得分不要重复计算。

    输入样例:

    6 6
    9 0 1 9 9 9
    9 9 1 2 2 9
    9 9 2 0 2 9
    9 9 1 1 2 9
    9 9 3 3 1 1
    9 9 9 9 9 9
    2 1 5 4
    

    输出样例:

    27.04

    用深搜果然超时了,所以得用广搜了,但并不是每个点都入队列一遍就可以,需要维护一个数组,用来记录从起点(sx,sy)到这个点的最小值,不断更新,如果更新了就入队,否则不入,直到队列为空,
    返回终点(ex,ey)的最小值,另外是开头输入起点和终点,给出的是坐标,根数组下标其实是相反的,这个是后来才发现的,判断一个点是否和起点和终点连线共线,可以用向量叉乘积判断,根据右手定则吧应该是。
    可以先打表存好,用一个flag与之对应,要bfs两次,找连线的两层吗,通过改变flag的值,判断哪些点是属于正确的一侧。

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef pair<int,int> pa;
    typedef pair<pair<int,int>,double> paa;
    const double r = sqrt(2) - 1;
    int n,m;
    int sx,sy,ex,ey;
    double mp[101][101];
    int flag;
    int dir[8][2] = {0,1,1,0,0,-1,-1,0,-1,-1,1,-1,-1,1,1,1};
    int cc[101][101];
    double rr[8] = {0,0,0,0,r,r,r,r};
    int check(int x,int y) {
        int d = (sx - x) * (ey - y) - (ex - x) * (sy - y);
        if(d) return d / abs(d);
        return 0;
    }
    double bfs() {
        bool vis[101][101] = {false};
        double msum[101][101];
        for(int i = 0;i < n;i ++) {
            for(int j = 0;j < m;j ++) {
                msum[i][j] = inf;
            }
        }
        msum[sx][sy] = mp[sx][sy];
        queue<paa> q;
        q.push(paa(pa(sx,sy),mp[sx][sy]));
        while(!q.empty()) {
            int x = q.front().first.first;
            int y = q.front().first.second;
            double sum = q.front().second;
            q.pop();
            for(int i = 0;i < 8;i ++) {
                int tx = x + dir[i][0];
                int ty = y + dir[i][1];
                if(tx < 0 || ty < 0 || tx >= n || ty >= m || cc[tx][ty] != flag) continue;
                double tsum = sum + mp[tx][ty] + rr[i] * (mp[x][y] + mp[tx][ty]);
                if(tsum < msum[tx][ty]) {
                    q.push(paa(pa(tx,ty),msum[tx][ty] = tsum));
                }
            }
        }
        return msum[ex][ey];
    }
    int main() {
        scanf("%d%d",&n,&m);
        for(int i = 0;i < n;i ++) {
            for(int j = 0;j < m;j ++) {
                scanf("%lf",&mp[i][j]);
            }
        }
        scanf("%d%d%d%d",&sy,&sx,&ey,&ex);
        for(int i = 0;i < n;i ++) {
            for(int j = 0;j < m;j ++) {
                cc[i][j] = check(i,j);
            }
        }
        double ans = 0 - mp[sx][sy] - mp[ex][ey];
        cc[sx][sy] = cc[ex][ey] = flag = 1;
        ans += bfs();
        cc[sx][sy] = cc[ex][ey] = flag = -1;
        ans += bfs();
        printf("%.2f",ans);
    }

     重温:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <cmath>
    #define inf 0x3f3f3f3f
    using namespace std;
    const double r = sqrt(2) - 1;
    int flag;
    int n,m,sx,sy,ex,ey;
    int dir[8][2] = {0,1,1,0,0,-1,-1,0,1,1,-1,-1,-1,1,1,-1};
    double rr[8] = {0,0,0,0,r,r,r,r};
    double mp[100][100];
    double mpm[100][100];
    struct po {
        int x,y;
        double sum;
        po(){}
        po(int x,int y,double sum) {
            this -> x = x;
            this -> y = y;
            this -> sum = sum;
        }
    }temp;
    int check(int x,int y) {
        if(x == ex && y == ey) return flag;
        int d = (sx - x) * (ey - y) - (sy - y) * (ex - x);
        if(d) return d / abs(d);
        return 0;
    }
    double bfs() {
        queue<po> q;
        q.push(po(sx,sy,mp[sx][sy]));
        mpm[sx][sy] = mp[sx][sy];
        mpm[ex][ey] = inf;
        while(!q.empty()) {
            temp = q.front();
            q.pop();
            for(int i = 0;i < 8;i ++) {
                int tx = temp.x + dir[i][0];
                int ty = temp.y + dir[i][1];
                if(tx < 0 || ty < 0 || tx >= n || ty >= m || check(tx,ty) != flag) continue;
                double sum = temp.sum + mp[tx][ty] + rr[i] * (mp[tx][ty] + mp[temp.x][temp.y]);
                if(sum < mpm[tx][ty]) {
                    q.push(po(tx,ty,sum));
                    mpm[tx][ty] = sum;
                }
            }
        }
        return mpm[ex][ey];
    }
    int main() {
        cin>>n>>m;
        for(int i = 0;i < n;i ++) {
            for(int j = 0;j < m;j ++) {
                cin>>mp[i][j];
                mpm[i][j] = inf;
            }
        }
        cin>>sy>>sx>>ey>>ex;
        flag = -1;
        double ans = -mp[sx][sy] - mp[ex][ey] + bfs();
        flag = 1;
        ans += bfs();
        printf("%.2f",ans);
    }
  • 相关阅读:
    cors解决跨域
    神经网络和keras
    tensorflow笔记
    5.聚类算法-kmeans
    4.回归类算法-目标值连续型
    3.分类算法-目标值离散型
    Phaser.js开发小游戏之洋葱头摘星星
    VS Code 插件之 vscode-debug-visualizer
    Phaser.js开发小游戏之Phaser.js介绍
    微信小程序中写threejs系列之 threejs-miniprogram
  • 原文地址:https://www.cnblogs.com/8023spz/p/10483962.html
Copyright © 2011-2022 走看看