04: The most matched matrix. 04: the matching matrix.
04: The most matched Matrix
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65536kB
-
Description
-
Given A m * n matrix A and r * s matrix B, where 0 <r ≤ m, 0 <s ≤ n, all element values of A and B are positive integers smaller than 100. Evaluate A child matrix C with A size of r * s to minimize the sum of the absolute values of the corresponding element difference between B and C. C is called the most matched matrix. If multiple child matrices meet the conditions at the same time, select the smaller element row number in the upper-left corner of the Child matrix. If the row number is the same, select the smaller column number.
-
Input
-
The first line is m and n, separated by a space.
Each row of m rows has n integers, indicating the rows in matrix A. Separate the numbers with A space.
Act r and s in m + 2, separated by a space.
Then, each row in the r row has s integers, indicating the rows in the B matrix. The numbers are separated by a space.
(1 ≤ m ≤ 100, 1 ≤ n ≤)
-
Output
-
The output matrix C contains a total of r rows. Each row has s integers separated by a space.
-
Sample Input
-
3 33 4 55 3 48 2 42 27 34 9
-
Sample output
-
4 5 3 4
-
Source
-
Department of Medicine 2010 final exam Lin Hongwu
-
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 # include <cmath> 5 # include <queue> 6 # include <vector> 7 # include <algorithm> 8 using namespace std; 9 int n, m; // The long width of the large matrix is 10 int r, s; // The long width of the small matrix is 11 int a [1001] [1001]; // large 12 int B [1001] [1001]; // small 13 int minn = 1000000; // store the smallest absolute value 14 int minnow; 15 int wzh; // store 16 int wzl; 17 void find () 18 {19 for (int I = 1; I <= n-r + 1; I ++) 20 {21 for (int j = 1; j <= m-s + 1; j ++) 22 {23 minnow = 0; 24 for (int k = I; k <= I + R-1; k ++) 25 {26 for (int l = j; l <= s + J-1; l ++) 27 {28 minnow = minnow + abs (a [k] [l]-B [k-I + 1] [l-j + 1]); 29} 30} 31 if (minnow <minn) 32 {33 wzh = I; 34 wzl = j; 35 minn = minnow; 36 37} 38} 39} 40} 41 int main () 42 {43 cin> n> m; 44 for (int I = 1; I <= n; I ++) 45 {46 for (int j = 1; j <= m; j ++) 47 {48 cin> a [I] [j]; 49} 50} 51 cin> r> s; 52 for (int I = 1; I <= r; I ++) 53 {54 for (int j = 1; j <= s; j ++) 55 {56 cin> B [I] [j]; 57} 58} 59 find (); 60 for (int I = wzh; I <= wzh + R-1; I ++) 61 {62 for (int j = wzl; j <= wzl + s-1; j ++) 63 {64 cout <a [I] [j] <"; 65} 66 cout <endl; 67} 68 return 0; 69}