-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordfinder.cpp
77 lines (67 loc) · 2.43 KB
/
wordfinder.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 "wordfinder.h"
#include <boost/unordered_map.hpp>
#include <vector>
typedef boost::unordered_map<wchar_t, std::vector<LetterPoint> > map_type;
namespace {
bool isNeighbours(const LetterPoint& center, const LetterPoint& neighbour)
{
return (center.getX() == neighbour.getX() && (center.getY() == neighbour.getY() + 1 || center.getY() == neighbour.getY() - 1))
|| (center.getY() == neighbour.getY() && (center.getX() == neighbour.getX() + 1 || center.getX() == neighbour.getX() - 1));
}
bool findInMap(map_type& map, const std::wstring& pattern, LetterPoint& currentPoint, std::stack<LetterPoint>& path)
{
currentPoint.setUsed(true);
path.push(currentPoint);
if (pattern.size() == 0)
{
return true;
}
if (map.count(pattern.at(0)) == 0)
{
return false;
}
std::vector<LetterPoint>& nextPointList = map.find(pattern.at(0))->second;
for (size_t i = 0 ; i < nextPointList.size() ; ++i)
{
if (!nextPointList.at(i).isUsed() && isNeighbours(currentPoint, nextPointList.at(i)) &&
findInMap(map, pattern.substr(1), nextPointList.at(i), path))
{
return true;
}
}
currentPoint.setUsed(false);
path.pop();
return false;
}
}
namespace WordFinder {
bool findPattern(
const std::vector<std::vector<wchar_t> >& matrix,
const std::wstring& pattern,
std::stack<LetterPoint>& path)
{
boost::unordered_map<wchar_t, std::vector<LetterPoint> > map;
for (size_t i = 0 ; i < matrix.size() ; ++i)
{
for (size_t j = 0 ; j < matrix.at(i).size() ; ++j)
{
map_type::value_type initList(matrix.at(i).at(j), std::vector<LetterPoint>());
map.insert(initList);
map_type::iterator iterator = map.find(matrix.at(i).at(j));
iterator->second.push_back(LetterPoint(i, j, matrix.at(i).at(j)));
}
}
if (map.count(pattern.at(0)) == 0)
{
return false;
}
for (size_t i = 0 ; i < map.find(pattern.at(0))->second.size() ; ++i)
{
if (findInMap(map, pattern.substr(1), map.find(pattern.at(0))->second.at(i), path))
{
return true;
}
}
return false;
}
}