Skip to main content
edited tags
Link
Pimgd
  • 22.6k
  • 5
  • 68
  • 144
deleted 124 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

The Grid Search

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 and C, indicating the number of rows and columns in the grid G, respectively.
This is followed by R lines, each with a string of C digits, which represent the grid G.
The following line contains two space-separated integers, r and c, indicating the number of rows and columns in the pattern grid P.
This is followed by r lines, each with a string of c digits, which represent the pattern P.


Output Format

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 and C, indicating the number of rows and columns in the grid G, respectively.

This is followed by R lines, each with a string of C digits, which represent the grid G.

The following line contains two space-separated integers, r and c, indicating the number of rows and columns in the pattern grid P.

This is followed by r lines, each with a string of c digits, which represent the pattern P.

Output Format

Display YES or NO, depending on whether (or not) you find that the larger grid G contains the rectangular pattern P. The evaluation will be case sensitive.

Display YES or NO, depending on whether (or not) you find that the larger grid G contains the rectangular pattern P. The evaluation will be case sensitive.


My Code

Please provide any tips from efficiency to readability.

My Question

As a newbie programmer, especially in C++, any tips regarding my code would be much appreciated. This could be anything from code efficiency to readability.

The Grid Search

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 and C, indicating the number of rows and columns in the grid G, respectively.
This is followed by R lines, each with a string of C digits, which represent the grid G.
The following line contains two space-separated integers, r and c, indicating the number of rows and columns in the pattern grid P.
This is followed by r lines, each with a string of c digits, which represent the pattern P.


Output Format

Display YES or NO, depending on whether (or not) you find that the larger grid G contains the rectangular pattern P. The evaluation will be case sensitive.


My Code

My Question

As a newbie programmer, especially in C++, any tips regarding my code would be much appreciated. This could be anything from code efficiency to readability.

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 and C, indicating the number of rows and columns in the grid G, respectively.

This is followed by R lines, each with a string of C digits, which represent the grid G.

The following line contains two space-separated integers, r and c, indicating the number of rows and columns in the pattern grid P.

This is followed by r lines, each with a string of c digits, which represent the pattern P.

Output Format

Display YES or NO, depending on whether (or not) you find that the larger grid G contains the rectangular pattern P. The evaluation will be case sensitive.

Please provide any tips from efficiency to readability.

Source Link
Ziyad Edher
  • 281
  • 3
  • 10

The Grid Search

The Grid Search

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 and C, indicating the number of rows and columns in the grid G, respectively.
This is followed by R lines, each with a string of C digits, which represent the grid G.
The following line contains two space-separated integers, r and c, indicating the number of rows and columns in the pattern grid P.
This is followed by r lines, each with a string of c digits, which represent the pattern P.


Output Format

Display YES or NO, depending on whether (or not) you find that the larger grid G contains the rectangular pattern P. The evaluation will be case sensitive.


Taken from HackerRank challenge "The Grid Search"

My Code

#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;
}

My Question

As a newbie programmer, especially in C++, any tips regarding my code would be much appreciated. This could be anything from code efficiency to readability.