loj#P6858. 「ICPC World Finals 2021」马赛克搜索
「ICPC World Finals 2021」马赛克搜索
Description
The International Center for the Preservation of Ceramics (ICPC) is searching for motifs in some ancient mosaics. According to the ICPC's definition, a mosaic is a rectangular grid where each grid square contains a colored tile. A motif is similar to a mosaic but some of the grid squares can be empty. Figure G.1 shows an example motif and mosaic.
The rows of an mosaic are numbered to from top to bottom, and the columns are numbered to from left to right.
A contiguous rectangular subgrid of the mosaic matches the motif if every tile of the motif matches the color of the corresponding tile of the subgrid. Formally, an motif appears in an mosaic at position if for all , , the tile exists in the mosaic and either the square in the motif is empty or the tile at in the motif has the same color as the tile at in the mosaic.
Given the full motif and mosaic, find all occurrences of the motif in the mosaic.
Input
The first line of input contains two integers and , where and () are the number of rows and columns in the motif. Then lines follow, each with integers in the range , denoting the color of the motif at that position. A value of denotes an empty square.
The next line of input contains two integers and where and () are the number of rows and columns in the mosaic. Then lines follow, each with integers in the range , denoting the color of the mosaic at that position.
Output
On the first line, output , the total number of matches. Then output lines, each of the form where is the row and is the column of the top left tile of the match. Sort matches by increasing , breaking ties by increasing .
2 2
1 0
0 1
3 4
1 2 1 2
2 1 1 1
2 2 1 3
3
1 1
1 3
2 2