码迷,mamicode.com
首页 > 其他好文 > 详细

835. Image Overlap —— weekly contest 84

时间:2018-05-14 00:58:48      阅读:899      评论:0      收藏:0      [点我收藏+]

标签:mes   模拟   color   rom   val   pre   style   over   tor   

 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.

Notes: 

  1. 1 <= A.length = A[0].length = B.length = B[0].length <= 30
  2. 0 <= A[i][j], B[i][j] <= 1
 1 class Solution {
 2 public:
 3     int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
 4         int n = A.size();
 5         int res = 0;
 6         for(int i = -n+1; i < n; i++){
 7             for(int j =-n+1; j < n; j++){
 8                 int count = 0;
 9                 for(int k = max(i,0); k < min(n+i,n); k++){
10                     for(int l = max(j,0); l < min(n+j,n);l++){
11                         if(A[k-i][l-j]==1&&B[k][l]==1){
12                             count++;
13                         }
14                     }
15                 }
16                 res = max(res,count);
17             }
18         }
19         return res;
20     }
21 };

暴力模拟,O(n4),模拟过程的边界以及max的使用也没有想象中的那么容易。

835. Image Overlap —— weekly contest 84

标签:mes   模拟   color   rom   val   pre   style   over   tor   

原文地址:https://www.cnblogs.com/jinjin-2018/p/9034105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!