-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFloorBoards.cpp
More file actions
186 lines (158 loc) · 4.21 KB
/
FloorBoards.cpp
File metadata and controls
186 lines (158 loc) · 4.21 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
using namespace std;
/* http://topcoder.bgcoder.com/print.php?id=1999 */
#define SIZEOFARRAY(s) sizeof(s)/sizeof(s[0])
#define REP(i,n) for(int i=0;i<(n);++i)
#define LOOP(i,v,n) for(i=v;i<(n);++i)
#define LL long long
#define memSet(m, v) memset(m, v, sizeof(m))
int memo[1<<10][10][10];
class FloorBoards
{
size_t mWidth;
size_t mHeight;
int solve(int i, int j, int mask, const vector<string> &mat)
{
if (i == mHeight) return 0;
int &memv = memo[mask][i][j];
if (memv != -1) return memv;
if (mat[i][j] == '#')
return solve(i + (j + 1) / mWidth, (j + 1) % mWidth, mask & ~(1 << j), mat);
int vertical = solve(i + (j + 1) / mWidth, (j + 1) % mWidth, mask | (1 << j), mat);
if ((mask & (1 << j)) == 0)
++vertical;
int horizontal = solve(i + (j + 1) / mWidth, (j + 1) % mWidth, mask & ~(1 << j), mat);
if (j == 0 || mat[i][j - 1] == '#' || (mask & (1 << (j - 1))) != 0)
++horizontal;
memv = std::min(horizontal, vertical);
return memv;
}
public:
int howMany(vector<string> mat)
{
mWidth = mat[0].length();
mHeight = mat.size();
memSet(memo, -1);
return solve(0, 0, 0, mat);
}
};
void TEST(vector<string> mat, int expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
FloorBoards floorBoards;
int result = floorBoards.howMany(mat);
assert(result == expected);
end = clock();
cpu_time_used = ((double)(end - start));
cout << "Time taken : " << cpu_time_used << endl;
}
template <class T>
vector<T> convert(T *list, int n)
{
vector<T> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
void Test1()
{
string test[] = { "....."
, "....."
, "....."
, "....."
, "....." };
TEST(convert(test, SIZEOFARRAY(test)), 5);
}
void Test2()
{
string test[] = { "......."
, ".#..##."
, ".#....."
, ".#####."
, ".##..#."
, "....###" };
TEST(convert(test, SIZEOFARRAY(test)), 7);
}
void Test3()
{
string test[] = { "####"
, "####"
, "####"
, "####" };
TEST(convert(test, SIZEOFARRAY(test)), 0);
}
void Test4()
{
string test[] = { "...#.."
, "##...."
, "#.#..."
, ".#...."
, "..#..."
, "..#..#" }
;
TEST(convert(test, SIZEOFARRAY(test)), 9);
}
void Test5()
{
string test[] = { ".#...."
, "..#..."
, ".....#"
, "..##.."
, "......"
, ".#..#." };
TEST(convert(test, SIZEOFARRAY(test)), 9);
}
void Test6()
{
string test[] = {"..........", ".....#..#.", "...#...#.#", ".#....##.#", "#.........", "..##..#...", "...###..#.", "#..#.....#", "#..##..#..", "..##......"};
TEST(convert(test, SIZEOFARRAY(test)), 22);
}
void Test7()
{
string test[] = {"......#..#", "..###.....", "......#.#.", "#..#.#...#", "..##..##..", "#.........", ".#.#.###..", "..##.#.#..", ".........#", ".....#..##"};
TEST(convert(test, SIZEOFARRAY(test)), 23);
}
void Test8()
{
string test[] = {"###...#..#", "##.....#.#", "#.....#.#.", "....#..#..", ".#.#......", ".##....##.", "...#......", ".#..#.....", ".#......#.", "##....#..#"};
TEST(convert(test, SIZEOFARRAY(test)), 21);
}
void Test9()
{
string test[] = {"..#.......", ".#.....#..", "...#..#...", "#.##..#...", "...##..#..", ".##...#..#", "#......#..", ".#...#.#..", "##...#....", ".###.#..#."};
TEST(convert(test, SIZEOFARRAY(test)), 24);
}
void Test10()
{
string test[] = {".#..#..#..", "###.#.####", "##..#.....", "###.#.###.", "#...#..##.", "###.#.###.", "##..#...#.", "###.#.###.", "#......##.", "#########."};
TEST(convert(test, SIZEOFARRAY(test)), 14);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
Test10();
cout << "success";
return 0;
}