#C10957. Matrix Subarray
Matrix Subarray
Matrix Subarray
You are given two matrices, A and B. Your task is to determine whether matrix B appears as a contiguous subarray within matrix A. In other words, check if there exists a pair of indices (i, j) in matrix A such that for all 0 ≤ x < P and 0 ≤ y < Q, the element A[i+x][j+y] equals B[x][y].
Input Format
The input begins with an integer T, representing the number of test cases. Each test case has the following format:
- The first line contains two integers N and M, the number of rows and columns of matrix A respectively.
- The next N lines each contain M integers, representing the matrix A.
- The following line contains two integers P and Q, the number of rows and columns of matrix B respectively.
- The next P lines each contain Q integers, representing the matrix B.
You need to output "YES" if B is a subarray of A, otherwise output "NO" for each test case.
Mathematical Formulation
Given two matrices \( A_{N\times M} \) and \( B_{P\times Q} \), find if there exists an index \( (i, j) \) such that \[ A[i+x][j+y] = B[x][y] \quad \text{for all} \ 0 \le x < P, \ 0 \le y < Q. \]
inputFormat
The input is read from standard input (stdin) and has the following structure:
- An integer T, the number of test cases.
- For each test case:
- Two integers N and M, the dimensions of matrix A.
- N lines each with M space-separated integers representing matrix A.
- Two integers P and Q, the dimensions of matrix B.
- P lines each with Q space-separated integers representing matrix B.
outputFormat
For each test case, output a single line to standard output (stdout) with either "YES" if matrix B is a subarray of A or "NO" if it is not.
## sample3
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
2 3
8 9 10
13 14 15
3 3
1 2 3
4 5 6
7 8 9
1 2
4 5
2 2
1 2
3 4
1 1
5
YES
YES
NO
</p>