Image Overlap

Two images A and B are given, represented as binary, square matrices of the same size.  (A binary matrix has only 0s and 1s as values.)

We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image.  After, the overlap of this translation is the number of positions that have a 1 in both images.

(Note also that a translation does not include any kind of rotation.)

What is the largest possible overlap?

Example 1:

Input: A = [[1,1,0],
            [0,1,0],
            [0,1,0]]
       B = [[0,0,0],
            [0,1,1],
            [0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.
/* 计算两张图片经过平移后重合的1最多是多少
 * 注意移动后重合的位置发生变化 因此不能用dp
 * length<30 上暴力O(n^4) 900*900=810000 控制在百万应该OK
 * */
class Solution {
public:
    int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
        return max(myIOU(A, B), myIOU(B, A));
    }
    int myIOU(vector<vector<int>>& A, vector<vector<int>>& B){//
        int len=A.size(), ret=0;
        // 固定A 移动B row,col是移动量
        for(int row=0;row<len;row++){
            for(int col=0;col<len;col++){
                int cnt=0;
                for(int i=row;i<len;i++){
                    for(int j=col;j<len;j++){
                        if(A[i][j]==1 && B[i-row][j-col]==1)    cnt++;
                    }
                }
                cout<<row<<" "<<col<<" "<<cnt<<endl;
                ret = max(ret, cnt);
            }
        }
        return ret;
    }
};

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值