Benchmark AI / Public workspace

Humanity's Last Code Exam / 2021_G / Mosaic Browsing

Problem

Answer published by the source. Consult the official source to check your work against its answer.

platform

atcoder

question content

## Problem Statement 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. The rows of an rq×cqr_q \times c_q mosaic are numbered 1 to rqr_q from top to bottom, and the columns are numbered 1 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 (r,c)(r, c) if for all 1irp1 \leq i \leq r_p, 1jcp1 \leq j \leq c_p, the tile (r+i1,c+j1)(r + i - 1, c + j - 1) exists in the mosaic and either the square (i,j)(i, j) in the motif is empty or the tile at (i,j)(i, j) in the motif has the same color as the tile at (r+i1,c+j1)(r + i - 1, c + j - 1) 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 rpr_p and cpc_p, where rpr_p and cpc_p (1rp,cp1,000)(1 \leq r_p, c_p \leq 1,000) 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 0 denotes an empty square. The next line of input contains two integers rqr_q and cqc_q where rqr_q and cqc_q (1rq,cq1,000)(1 \leq r_q, c_q \leq 1,000) 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. ## Output 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. ## Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

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. The rows of an r_(q)×c_(q) mosaic are numbered 1 to r_(q) from top to bottom, and the columns are numbered 1 to c_(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 r_(p)×c_(p) motif appears in an r_(q)×c_(q) mosaic at position (r,c) if for all 1≤i≤r_(p), 1≤j≤c_(p), the tile (r+i−1,c+j−1) exists in the mosaic and either the square (i,j) in the motif is empty or the tile at (i,j) in the motif has the same color as the tile at (r+i−1,c+j−1) 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 r_(p) and c_(p), where r_(p) and c_(p) (1≤r_(p),c_(p)≤1,000) are the number of rows and columns in the motif. Then r_(p) lines follow, each with c_(p) integers in the range [0,100], denoting the color of the motif at that position. A value of 0 denotes an empty square.

The next line of input contains two integers r_(q) and c_(q) where r_(q) and c_(q) (1≤r_(q),c_(q)≤1,000) are the number of rows and columns in the mosaic. Then r_(q) lines follow, each with c_(q) integers in the range [1,100], denoting the color of the mosaic at that position.

## Output

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

## Sample Input 1

Original LaTeX notation
## Problem Statement

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. The rows of an \(r_q \times c_q\) mosaic are numbered 1 to \(r_q\) from top to bottom, and the columns are numbered 1 to \(c_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 \(r_p \times c_p\) motif appears in an \(r_q \times c_q\) mosaic at position \((r, c)\) if for all \(1 \leq i \leq r_p\), \(1 \leq j \leq c_p\), the tile \((r + i - 1, c + j - 1)\) exists in the mosaic and either the square \((i, j)\) in the motif is empty or the tile at \((i, j)\) in the motif has the same color as the tile at \((r + i - 1, c + j - 1)\) 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 \(r_p\) and \(c_p\), where \(r_p\) and \(c_p\) \((1 \leq r_p, c_p \leq 1,000)\) are the number of rows and columns in the motif. Then \(r_p\) lines follow, each with \(c_p\) integers in the range \([0, 100]\), denoting the color of the motif at that position. A value of 0 denotes an empty square.

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

## Output

On the first line, output \(k\), the total number of matches. Then output \(k\) lines, each of the form \(r \ c\) where \(r\) is the row and \(c\) is the column of the top left tile of the match. Sort matches by increasing \(r\), breaking ties by increasing \(c\).

## Sample Input 1

Code

2 2
1 0
0 1
3 4
1 2 1 2
2 1 1 1
2 2 1 3

## Sample Output 1

Code

3
1 1
1 3
2 2

question title

Mosaic Browsing

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import