forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordFind.cpp
More file actions
151 lines (128 loc) · 4.04 KB
/
WordFind.cpp
File metadata and controls
151 lines (128 loc) · 4.04 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <set>
#include <queue>
#include <iterator>
#include <sstream>
#include <cassert>
#include <stack>
using namespace std;
/*WordFind : http://community.topcoder.com/stat?c=problem_statement&pm=3972&rd=6521 */
struct Point
{
int x;
int y;
Point() : x(-1), y(-1) {}
Point(int left, int top) : x(left), y(top) {}
};
struct SearchState
{
int compareIndex;
struct Point startPoint;
struct Point comparePoint;
SearchState(int c, Point p, Point cp) : compareIndex(c), startPoint(p), comparePoint(cp) {}
};
typedef queue<SearchState> BfsSearch;
Point findWord(const vector<vector<char>> &charGrid, const string &searchStr)
{
BfsSearch bfs;
int gridSize = charGrid.size();
int col = charGrid[0].size();
vector<vector<vector<int>>> visitState(searchStr.length(), vector<vector<int>>(gridSize, vector<int>(col)) );
bfs.push( SearchState(0, Point(-1,-1), Point(0,0)) );
while (bfs.size())
{
SearchState state = bfs.front();
bfs.pop();
if (state.comparePoint.x < gridSize && state.comparePoint.y < col
&& !visitState[state.compareIndex][state.comparePoint.x][state.comparePoint.y])
{
visitState[state.compareIndex][state.comparePoint.x][state.comparePoint.y] = 1;
if (charGrid[state.comparePoint.x][state.comparePoint.y] == searchStr[state.compareIndex])
{
if (state.compareIndex == searchStr.length() - 1)
{
return state.startPoint;
}
else if (state.compareIndex == 0)
{
state.startPoint.x = state.comparePoint.x;
state.startPoint.y = state.comparePoint.y;
}
int cmpIndex = state.compareIndex + 1;
bfs.push( SearchState(cmpIndex, Point(state.startPoint.x, state.startPoint.y),
Point(state.comparePoint.x + 1, state.comparePoint.y)) ) ;
bfs.push( SearchState(cmpIndex, Point(state.startPoint.x, state.startPoint.y),
Point(state.comparePoint.x, state.comparePoint.y + 1)) ) ;
bfs.push( SearchState(cmpIndex, Point(state.startPoint.x, state.startPoint.y),
Point(state.comparePoint.x + 1, state.comparePoint.y + 1)) ) ;
}
else
{
bfs.push( SearchState(0, Point(-1, -1),
Point(state.comparePoint.x + 1, state.comparePoint.y)) ) ;
bfs.push( SearchState(0, Point(-1, -1),
Point(state.comparePoint.x, state.comparePoint.y + 1)) ) ;
bfs.push( SearchState(0, Point(-1, -1),
Point(state.comparePoint.x + 1, state.comparePoint.y + 1)) ) ;
}
}
}
return Point();
}
vector<Point> findWords(const vector<string> &grid, const vector<string> &wordList)
{
int strLen = grid[0].length();
vector<vector<char>> charGrid(grid.size() , vector<char>(strLen));
vector<Point> points;
for (size_t idx = 0, gridSize = grid.size(); idx < gridSize; ++idx)
{
string str = grid[idx];
for (size_t sidx = 0, len = str.length(); sidx < len; ++sidx)
{
charGrid[idx][sidx] = str[sidx];
}
}
for (size_t idx = 0, wordListSize = wordList.size(); idx < wordListSize; ++idx)
{
points.push_back(findWord(charGrid, wordList[idx]));
}
return points;
}
int main(int argc, char *argv[])
{
vector<string> grid;
vector<string> wordList;
//grid.push_back("TEST");
//grid.push_back("GOAT");
//grid.push_back("BOAT");
//wordList.push_back("BOAT");
//wordList.push_back("GOAT");
//wordList.push_back("TEST");
//grid.push_back("SXXXXX");
//grid.push_back("XQXMXX");
//grid.push_back("XXXAXX");
//grid.push_back("XXXSWX");
//grid.push_back("XXXLQO");
//grid.push_back("XXXLXL");
//wordList.push_back("SQL");
//wordList.push_back("RAM");
grid.push_back("EASYTOFINDEAGSRVHOTCJYG");
grid.push_back("FLVENKDHCESOXXXXFAGJKEO");
grid.push_back("YHEDYNAIRQGIZECGXQLKDBI");
grid.push_back("DEIJFKABAQSIHSNDLOMYJIN");
grid.push_back("CKXINIMMNGRNSNRGIWQLWOG");
grid.push_back("VOFQDROQGCWDKOUYRAFUCDO");
grid.push_back("PFLXWTYKOITSURQJGEGSPGG");
wordList.push_back("EASYTOFIND");
wordList.push_back("DIAG");
wordList.push_back("GOING");
wordList.push_back("THISISTOOLONGTOFITINTHISPUZZLE");
/*vector<Point> vect = */findWords(grid, wordList);
getchar();
return 0;
}