Stamp Grid
InterviewTime limit2sMemory limit1024 MB
Decide whether a K x K stamp, rotated as needed, can be placed on an empty N x N canvas to match a target pattern without inking any blank cell.
- Level
Medium4 of 10
- Topics
- Brute force, Matrix, Simulation
- Solved
- No attempts yet
Problem
A stamp painting is a black and white painting on an canvas, where some cells are inked and the rest are blank. It is described by an array of characters (). The th entry of the th column of the array is * if the canvas has ink at that square, and . otherwise.
Bessie wants to create a certain stamp painting. Farmer John lends her a single () stamp and an empty canvas. Bessie can rotate the stamp clockwise by between stampings and stamp anywhere on the grid, as long as the stamp lies entirely inside the grid. Formally, she picks integers with and . For each with , canvas cell is painted black if the stamp has ink at . Once a canvas cell is painted black, it stays black.
Input
The first line contains , the number of test cases.
Each test case starts with an integer , followed by lines of * and . characters that describe Bessie's desired painting. The next line contains , followed by lines of * and . characters that describe Farmer John's stamp.
Test cases are separated by blank lines.
Output
For each test case, print "YES" or "NO" on its own line.