ZeroJudge F418: Word Search Puzzle

Word Search Puzzle is a common word puzzle typically composed of a rectangular grid where each cell contains a letter. Players can form specified words by tracing contiguous letters from left to right, top to bottom, or diagonally from top-left to bottom-right.

Wenwen has found a word. She provided the starting and ending positions. Please spell out the word she found.

ZeroJudge F418題目示意圖

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input consists of six integers: H (1 ≤ H ≤ 20) and W (1 ≤ W ≤ 50), representing the height and width of the word puzzle, respectively.
R1, C1, R2, and C2 denote the starting and ending positions, where R1 and R2 indicate the row numbers from top to bottom, and C1 and C2 indicate the column numbers from left to right. (1 ≤ R1 ≤ R2 ≤ H, 1 ≤ C1 ≤ C2 ≤ W)
The following H lines each contain W uppercase letters, representing the contents of the word puzzle.
Output the word formed by the contiguous characters from the starting position to the ending position.
10 14 4 3 10 9
VBREEFISHRACHP
ANACROCODILEEB
AOSTRICHTEGRDA
IADDHCHEETAHGD
BHRODRAVENENEG
EYWDLSAMOLELHE
ARTPVPRCBOLROR
RHTOAAHCROWAGH
CCANNORIAZEBRA
HANYTAEKNINAWA
(本測資請見上圖)
DOLPHIN
3 4 1 1 3 1
(Please see the chart below)
ROW

Sample Input (Formatted Table)

ROAR
OINK
WOOF

Thought Process

You can directly use a for loop to concatenate the characters. Just make sure to first determine if the answer string should be constructed horizontally from left to right, vertically from top to bottom, or diagonally from top-left to bottom-right.

"Left to right" means that the row (R) is fixed, so you just need to iterate through the columns (C) from C1 to C2 using a for loop.

"Top to bottom" means that the column (C) is fixed, so you just need to iterate through the rows (R) from R1 to R2 using a for loop.

"Diagonal" means you iterate through the rows from R1 to R2 using a for loop, but at the same time, you also increment the column (C1) by 1 each time you add a character.

Sample Code-ZeroJudge F418: Word Search Puzzle

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;
    int R1, C1, R2, C2;
    cin >> R1 >> C1 >> R2 >> C2;
    vector<string>v;
    for (int i = 0; i<N; i++)
    {
        string str;
        cin >> str;
        v.push_back(str);
    }
    string ans = "";
    if (R1 == R2)
    {
        for (int i = C1-1; i<C2; i++)
        {
            ans += v[R1-1][i];
        }
    }
    else if (C1 == C2)
    {
        for (int i = R1-1; i<R2; i++)
        {
            ans += v[i][C1-1];
        }
    }
    else
    {
        for (int i = R1-1; i<R2; i++)
        {
            ans += v[i][C1-1];
            C1++;
        }
    }
    cout << ans << "\n";
}

//ZeroJudge F418
//Dr. SeanXD

2 Comments

Comments