Given a 2D array of digits, try to find the location of a given 2D pattern of digits.
Input Format
The first line contains an integer,
T
, which is the number of test cases.
T
test cases follow, each having a structure as described below:
The first line contains two space-separated integers,R
andC
, indicating the number of rows and columns in the gridG
, respectively.This is followed by
R
lines, each with a string ofC
digits, which represent the gridG
.The following line contains two space-separated integers,
r
andc
, indicating the number of rows and columns in the pattern gridP
.This is followed by
r
lines, each with a string ofc
digits, which represent the patternP
.Output Format
Display
YES
orNO
, depending on whether (or not) you find that the larger gridG
contains the rectangular patternP
. The evaluation will be case sensitive.
Taken from HackerRank challenge "The Grid Search"
Please provide any tips from efficiency to readability.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
int main () {
int testCases;
cin >> testCases;
// Iterate through each test case
for (int i = 0; i < testCases; i++) {
/* MAIN GRID */
// Iterate through each row of the main grid and store it in 'grid' a vector of strings
int rows, cols;
cin >> rows >> cols;
vector<string> grid(rows);
for (int j = 0; j < rows; j++) {
cin >> grid[j];
}
/* PATTERN GRID */
// Iterate through each row of the pattern grid and store it in 'pattern' a vector of strings
int patternRows, patternCols;
cin >> patternRows >> patternCols;
vector<string> pattern(patternRows);
for (int j = 0; j < patternRows; j++) {
cin >> pattern[j];
}
/* GRID INSPECTION */
bool found = false; // To know when to leave the loop and what answer to return
// Iterate through each row until the pattern would extend off the grid
for (int j = 0; j <= (rows - patternRows); j++) {
// Iterate through each column until the pattern would extend off the grid
for (int k = 0; k <= (cols - patternCols); k++) {
// Check if there is a number in the grid equal to the first number in the pattern
if (grid[j][k] == pattern[0][0]) {
bool wrong = false; // To break out if wrong is false at the end of checking the lines
// Begin cross checking each row with the pattern, until you hit the amount of pattern rows
for (int l = 0; l < patternRows; l++) {
// Set wrong to true and break out if the pattern row is not the same as the grid row --
// stripped at the correct spot to be the same length as the pattern row.
// E.g. if the pattern row was '9729' and the grid row was '209729142' get the substring --
// that is the same length as the pattern row (4) and starting at the correct place found --
// out by the checking of the first number in the pattern to this number in the row --
// then go downwards.
if (pattern[l] != grid[l + j].substr(k, patternCols)) {
wrong = true;
break;
}
}
// If the whole test passed without any inconsistencies set found to true and begin the break chain
if (!wrong) {
found = true;
break;
}
}
}
// If found is false continue breaking
if (found) {
break;
}
}
/* ANSWER RETURN */
if (found == true) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}