0% found this document useful (0 votes)
16 views1 page

CCC Practice

Uploaded by

derekhaozhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

CCC Practice

Uploaded by

derekhaozhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <vector>
#include <string>

using namespace std;

string word;
int R, C;
vector<vector<string>> area;

int search(int r, int c, bool turned, int i, int dr, int dc) {
if (!(0 <= r && r < R && 0 <= c && c < C)) // out of bounds
return 0;
if (area[r][c] != string(1, word[i])) // didn't match word
return 0;
if (i == word.length() - 1) // found word
return 1;

int res = search(r + dr, c + dc, turned, i + 1, dr, dc);


if (!turned) {
res += search(r + dc, c - dr, true, i + 1, dc, -dr);
res += search(r - dc, c + dr, true, i + 1, -dc, dr);
}
return res;
}

int main() {
cin >> word;
cin >> R >> C;
area.resize(R, vector<string>(C));

for (int i = 0; i < R; i++) {


for (int j = 0; j < C; j++) {
cin >> area[i][j];
}
}

vector<pair<int, int>> dir_check = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1},
{-1, -1},
{1, -1}, {-1, 1}};
int total = 0;

for (int i = 0; i < R; i++) {


for (int j = 0; j < C; j++) {
if (area[i][j] == string(1, word[0])) {
for (auto [dr, dc] : dir_check) {
// start on the second letter to prevent turning at start
total += search(i + dr, j + dc, false, 1, dr, dc);
}
}
}
}
cout << total << endl;
return 0;

You might also like