|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目描述:
- 给出两个图像 A 和 B ,A 和 B 为大小相同的二维正方形矩阵。(并且为二进制矩阵,只包含0和1)。
- 我们转换其中一个图像,向左,右,上,或下滑动任何数量的单位,并把它放在另一个图像的上面。之后,该转换的重叠是指两个图像都具有 1 的位置的数目。
- (请注意,转换不包括向任何方向旋转。)
- 最大可能的重叠是什么?
- 示例 1:
- 输入:A = [[1,1,0],
- [0,1,0],
-   [0,1,0]]
-   B = [[0,0,0],
-   [0,1,1],
-   [0,0,1]]
- 输出:3
- 解释: 将 A 向右移动一个单位,然后向下移动一个单位。
- 注意: 
- 1 <= A.length = A[0].length = B.length = B[0].length <= 30
- 0 <= A[i][j], B[i][j] <= 1
- 来源:力扣(LeetCode)
- 链接:https://leetcode-cn.com/problems/image-overlap
- 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码
- class Solution {
- public:
- int flatten(int x, int y){
- return (x << 6) + y;//二维坐标通过这个函数一一映射,这个函数并不唯一
- }
- int largestOverlap(const vector<vector<int>>& A, const vector<vector<int>>& B) {
- int res = 0;
- vector<int>store1, store2;
- map<int, int>same_distance;
- for(int i = 0; i < A.size(); i++){
- for(int j = 0; j < A[0].size(); j++){
- if(A[i][j])store1.push_back(flatten(i, j));
- if(B[i][j])store2.push_back(flatten(i, j));
- }
- }
- for(auto cha1 : store1){
- for(auto cha2 : store2){
- same_distance[cha1 - cha2]++;
- }
- }
- for(auto cha : same_distance){
- res = max(res, cha.second);
- }
- return res;
- }
- };
复制代码
参考链接:https://leetcode-cn.com/problems ... chi-ji-shu-by-hare/
|
|