generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
77 lines (65 loc) · 2.06 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include <list>
#include <string>
#include <vector>
class Solution {
public:
bool placeWordInCrossword(
std::vector<std::vector<char>>& board, std::string word) {
auto reversedWord = word;
std::reverse(reversedWord.begin(), reversedWord.end());
for (int y = board.size() - 1; y >= 0; --y) {
int x = board.front().size() - 1;
while (x >= 0) {
while (x >= 0 && board[y][x] == '#') --x;
if (x < static_cast<int>(word.size() - 1)) break;
int i = word.size() - 1;
while (i >= 0) {
if (board[y][x - i] != ' ' && board[y][x - i] != word[i]) break;
--i;
}
if (i < 0) {
if (x - word.size() + 1 == 0) return true;
if (board[y][x - word.size()] == '#') return true;
}
i = word.size() - 1;
while (i >= 0) {
if (board[y][x - i] != ' ' && board[y][x - i] != reversedWord[i]) break;
--i;
}
if (i < 0) {
if (x - word.size() + 1 == 0) return true;
if (board[y][x - word.size()] == '#') return true;
}
while (x >= 0 && board[y][x] != '#') --x;
}
}
for (int x = board.front().size() - 1; x >= 0; --x) {
int y = board.size() - 1;
while (y >= 0) {
while (y >= 0 && board[y][x] == '#') --y;
if (y < static_cast<int>(word.size() - 1)) break;
int i = word.size() - 1;
while (i >= 0) {
if (board[y - i][x] != ' ' && board[y - i][x] != word[i]) break;
--i;
}
if (i < 0) {
if (y - word.size() + 1 == 0) return true;
if (board[y - word.size()][x] == '#') return true;
}
i = word.size() - 1;
while (i >= 0) {
if (board[y - i][x] != ' ' && board[y - i][x] != reversedWord[i]) break;
--i;
}
if (i < 0) {
if (y - word.size() + 1 == 0) return true;
if (board[y - word.size()][x] == '#') return true;
}
while (y >= 0 && board[y][x] != '#') --y;
}
}
return false;
}
};