#P9445. [ICPC2021 WF] Mosaic Browsing

[ICPC2021 WF] Mosaic Browsing

题目描述

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\textit{mosaic} is a rectangular grid where each grid square contains a colored tile. A motif\textit{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 rq×cqr_q \times c_q mosaic are numbered 11 to rqr_q from top to bottom, and the columns are numbered 11 to cqc_q 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 rp×cpr_p \times c_p motif appears in an rq×cqr_q \times c_q mosaic at position (rr, cc) if for all 1irp1 \leq i \leq r_p, 1jcp1 \leq j \leq c_p, the tile (r+i1r+i-1, c+j1c+j-1) exists in the mosaic and either the square (ii, jj) in the motif is empty or the tile at (ii, jj) in the motif has the same color as the tile at (r+i1r+i-1, c+j1c+j-1) in the mosaic.

Given the full motif and mosaic, find all occurrences of the motif in the mosaic.

Figure G.1: Motif (left) and mosaic (right) of Sample Input 1.

输入格式

The first line of input contains two integers rpr_p and cpc_p, where rpr_p and cpc_p (1rp,cp10001 \leq r_p, c_p \leq 1000) are the number of rows and columns in the motif. Then rpr_p lines follow, each with cpc_p integers in the range [0,100][0, 100], denoting the color of the motif at that position. A value of 00 denotes an empty square.

The next line of input contains two integers rqr_q and cqc_q where rqr_q and cqc_q (1rq,cq10001 \leq r_q, c_q \leq 1000) are the number of rows and columns in the mosaic. Then rqr_q lines follow, each with cqc_q integers in the range [1,100][1, 100], denoting the color of the mosaic at that position.

输出格式

On the first line, output kk, the total number of matches. Then output kk lines, each of the form rcr c where rr is the row and cc is the column of the top left tile of the match. Sort matches by increasing rr, breaking ties by increasing cc.

题目大意

简要题意

给出两个矩阵, 其中第二个矩阵所有元素非 00.

定义第一个矩阵在第二个矩阵中的坐标 (l,r)(l, r) 处「出现」, 当且仅当存在一种方式任意修改第一个矩阵所有为 00 的元素后, 第一个矩阵的左上角在第二个矩阵的对应位置坐标为 (l,r)(l, r) 时可以与第二个矩阵的一部分完全重合.

求第一个矩阵在第二个矩阵中所有「出现」的位置和总「出现」次数。

输入格式

第一行, 两个整数 rp,cpr_p, c_p, 表示第一个矩阵的行数和列数.

接下来 rpr_p 行, 每行 cpc_p 个整数, 描述第一个矩阵.

接下来一行, 两个整数 rq,cqr_q, c_q, 表示第二个矩阵的行数和列数.

接下来 rqr_q 行,每行 cqc_q 个整数, 描述第二个矩阵.

输出格式

第一行, 一个整数 kk, 表示「出现」的总次数.

接下来 kk 行, 每行两个整数 x,yx, y, 表示每一次「出现」的坐标. 如果有多个, 请以 xx 为第一关键字, yy 为第二关键字升序排序后输出。

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