题:https://leetcode.com/problems/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 <= A.length = A[0].length = B.length = B[0].length <= 30
- 0 <= A[i][j], B[i][j] <= 1
题目大意
有两个等长度的正方形矩阵,矩阵的元素是0或1,对一个矩阵进行行或列 移动,即把所有行 上下移动 或 所有列左右移动 缺省的地方用0填充。
最后将两个 矩阵 叠加在一起,位置均为1 的 1,求通过 行或列移动 ,能获得最多 1 的 叠加矩阵。
思路
1.暴力解法
构建一个大矩阵extendA,中间是 A ,其余的部位为0,然后将B,逐个扫描,最大值便是 答案。
import javafx.util.Pair;
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int N = A.length;
int[][] extendA = new int[3*N][3*N];
int startIndex = N;
for(int i = 0 ; i < A.length ; i++)
for(int j = 0 ; j < A.length ;j++){
extendA[startIndex+i][startIndex+j] = A[i][j];
}
int maxRes = 0 ;
for(int i = 0 ; i < 2*N ; i++)
for(int j = 0 ; j < 2*N ;j++){
int tCnt = 0;
for(int k = 0 ; k < N ; k++){
for(int p = 0; p <N; p++){
if(extendA[i+k][j+p] ==1 && B[k][p] == 1)
tCnt++;
}
}
maxRes = Math.max(tCnt,maxRes);
}
return maxRes;
}
}
这里边界部分会重复计算。
对边界可以进行优化
但 对 边界为1 的矩阵 需要特殊处理。
import javafx.util.Pair;
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int N = A.length;
if(N == 1)
return A[0][0]&B[0][0];
int[][] extendA = new int[3*N-2][3*N-2];
int startIndex = N-1;
for(int i = 0 ; i < A.length ; i++)
for(int j = 0 ; j < A.length ;j++){
extendA[startIndex+i][startIndex+j] = A[i][j];
}
int maxRes = 0 ;
for(int i = 0 ; i < 2*N-2 ; i++)
for(int j = 0 ; j < 2*N-2 ;j++){
int tCnt = 0;
for(int k = 0 ; k < N ; k++){
for(int p = 0; p <N; p++){
if(extendA[i+k][j+p] ==1 && B[k][p] == 1)
tCnt++;
}
}
maxRes = Math.max(tCnt,maxRes);
}
return maxRes;
}
}
2. 统计位移向量
对于矩阵A的某个元素 A[i][j] ,要和 B[k][p] 重叠上,A需要的位移 为 (i-k,j-p)。由于A[i][j] B[i][j] 均为1 叠加矩阵才能产生1。所以只需要 统计 A、B中为1的元素。
然后将 AList 与 BList 中的每对 point,计算相应的位移,答案便是 统计出现最多次位移的次数。
import javafx.util.Pair;
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
List<Pair<Integer,Integer>> aList = new ArrayList<>();
List<Pair<Integer,Integer>> bList = new ArrayList<>();
for(int i =0 ; i < A.length; i++)
for(int j = 0 ; j <A.length ;j ++){
if(A[i][j] == 1)
aList.add(new Pair<Integer, Integer>(i,j));
if(B[i][j] == 1)
bList.add(new Pair<Integer, Integer>(i,j));
}
Map<Pair<Integer,Integer>,Integer> diffMap = new HashMap();
for(Pair<Integer,Integer> aPair:aList)
for(Pair<Integer,Integer> bPair :bList){
Pair<Integer,Integer> diffPair = new Pair<>(aPair.getKey() - bPair.getKey(),aPair.getValue() - bPair.getValue());
diffMap.put(diffPair,diffMap.getOrDefault(diffPair,0)+1);
}
int maxCnt = 0 ;
for(Integer cnt : diffMap.values()){
maxCnt = Math.max(maxCnt,cnt);
}
return maxCnt;
}
}