-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossword-puzzle.html
More file actions
103 lines (95 loc) · 3.6 KB
/
Copy pathcrossword-puzzle.html
File metadata and controls
103 lines (95 loc) · 3.6 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<head><title>crossword puzzle</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<link rel="stylesheet" href="/third-party/highlight/solarized-dark.css">
<script src="/third-party/highlight/highlight.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<h2>Yanzhan's solution for "crossword puzzle"</h2>
<ul>
<li>Hi, I'm Yanzhan. For more algothmic problems, visit my Youtube Channel <a href="https://www.youtube.com/channel/UCDkz-__gl3frqLexukpG0DA?view_as=subscriber" target=_blank>[Yanzhan Yang's Youtube Channel]</a> or my Twitter Account <a href="https://twitter.com/YangYanzhan" target=_blank>[Yanzhan Yang's Twitter]</a> or my GitHub HomePage <a href="https://github.com/yangyanzhan/code-camp" target=_blank>[Yanzhan Yang's GitHub Project]</a> .</li>
<li>It's fascinating to solve algothmic problems, follow Yanzhan to learn more!</li>
</ul>
<div>
</div>
<div>
</div>
<div>
</div>
<h3>
solution:
</h3>
<pre class="yanzhan-hidden"><code class="c++">bool crosswordPuzzle(vector<string> &crossword, vector<string> words) {
if (words.empty()) {
return true;
}
string word = words[0];
vector<string> remains(words.begin() + 1, words.end());
int m = crossword.size(), n = crossword[0].size();
for (int i = 0; i < m; i++) {
string &line = crossword[i];
for (int j = 0; j + word.size() <= n; j++) {
string old = line.substr(j, word.size());
bool valid = true;
for (int k = 0; k < word.size(); k++) {
if (!(old[k] == '-' || old[k] == word[k])) {
valid = false;
break;
}
}
if (valid) {
line.replace(j, word.size(), word);
if (crosswordPuzzle(crossword, remains)) {
return true;
} else {
line.replace(j, word.size(), old);
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j + word.size() <= m; j++) {
bool valid = true;
vector<char> old;
for (int k = 0; k < word.size(); k++) {
if (!(crossword[j + k][i] == '-' ||
crossword[j + k][i] == word[k])) {
valid = false;
break;
}
old.push_back(crossword[j + k][i]);
}
if (valid) {
for (int k = 0; k < word.size(); k++) {
crossword[j + k][i] = word[k];
}
if (crosswordPuzzle(crossword, remains)) {
return true;
} else {
for (int k = 0; k < word.size(); k++) {
crossword[j + k][i] = old[k];
}
}
}
}
}
return false;
}
vector<string> crosswordPuzzle(vector<string> &crossword, string words) {
vector<string> words_vec;
int head = 0, tail = 0;
while ((tail = words.find(";", head)) != string::npos) {
words_vec.push_back(words.substr(head, tail - head));
head = tail + 1;
}
words_vec.push_back(words.substr(head));
crosswordPuzzle(crossword, words_vec);
return crossword;
}
</code></pre>
<ul class="yanzhan-hidden">
<li><a href="/index.html" target=_blank>[Go Back To HomePage]</a></li>
<li><a href="/hackerrank" target=_blank>[Go Back To Solution List Page]</a></li>
</ul>
<script src="/main.js"></script>
</body>