forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKiloManX.cpp
More file actions
205 lines (169 loc) · 3.95 KB
/
KiloManX.cpp
File metadata and controls
205 lines (169 loc) · 3.95 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
#include <queue>
using namespace std;
#define SIZEOFARRAY(s) sizeof(s)/sizeof(s[0])
/* https://www.topcoder.com/stat?c=problem_statement&pm=2288&rd=4725 */
struct node
{
size_t weapons;
size_t shots;
node (size_t w, size_t s) : weapons(w), shots(s) {}
bool operator<(const node &rhs) const
{
return this->shots > rhs.shots;
}
};
class KiloManX
{
bool visited[32768];
priority_queue<node> pq;
public:
int leastShots(vector<string> damageChart, vector<int> bossHealth);
};
int KiloManX::leastShots(vector<string> damageChart, vector<int> bossHealth)
{
memset(visited, 0, sizeof(visited));
pq.push(node(0, 0));
size_t maxWeapons = (1 << damageChart[0].length()) - 1;
while (!pq.empty())
{
node top = pq.top();
pq.pop();
if ( visited[top.weapons] ) continue;
if ( top.weapons == maxWeapons )
return top.shots;
visited[top.weapons] = true;
for (size_t i = 0, size = damageChart.size(); i < size; ++i)
{
if ((top.weapons >> i) & 1) continue;
int best = bossHealth[i];
for (size_t j = 0; j < size; ++j)
{
if (i == j) continue;
size_t damage = damageChart[j][i] - '0';
if ( ((top.weapons >> j) & 1) && damage)
{
int hits = bossHealth[i] / damage;
if ( bossHealth[i] % damage ) ++hits;
best = std::min(best, hits);
}
}
pq.push(node(top.weapons | (1 << i), top.shots + best));
}
}
return -1;
}
void TEST(vector<string> damageChart, vector<int> bossHealth, int expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
KiloManX kiloManX;
int result = kiloManX.leastShots(damageChart, bossHealth);
assert( result == expected );
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
}
vector< vector<int> > convert(string *list, int n)
{
vector< vector<int> > ret(n, vector<int> (n));
for (int i = 0; i< n; ++i)
{
string str = list[i];
for (int j = 0, len = str.length(); j < len; ++j) {
istringstream iss(str.substr(j,1));
iss >> ret[i][j];
}
}
return ret;
}
template <class T>
vector<T> convertEx(T *list, int n)
{
vector<T> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
void Test1()
{
string damageChart[]= {"070","500","140"};
int bossHealth[]= {150,150,150};
TEST(convertEx(damageChart, SIZEOFARRAY(damageChart)),
convertEx(bossHealth, SIZEOFARRAY(bossHealth)), 218);
}
void Test2()
{
string damageChart[]= {"1542", "7935", "1139", "8882"};
int bossHealth[]= {150,150,150,150};
TEST(convertEx(damageChart, SIZEOFARRAY(damageChart)),
convertEx(bossHealth, SIZEOFARRAY(bossHealth)), 205);
}
void Test3()
{
string damageChart[]= {"07", "40"};
int bossHealth[]= {150,10};
TEST(convertEx(damageChart, SIZEOFARRAY(damageChart)),
convertEx(bossHealth, SIZEOFARRAY(bossHealth)), 48);
}
void Test4()
{
string damageChart[]= {"198573618294842",
"159819849819205",
"698849290010992",
"000000000000000",
"139581938009384",
"158919111891911",
"182731827381787",
"135788359198718",
"187587819218927",
"185783759199192",
"857819038188122",
"897387187472737",
"159938981818247",
"128974182773177",
"135885818282838"};
int bossHealth[]= {157, 1984, 577, 3001, 2003, 2984, 5988, 190003,
9000, 102930, 5938, 1000000, 1000000, 5892, 38};
TEST(convertEx(damageChart, SIZEOFARRAY(damageChart)),
convertEx(bossHealth, SIZEOFARRAY(bossHealth)), 260445);
}
void Test5()
{
string damageChart[]= {"02111111", "10711111", "11071111", "11104111",
"41110111", "11111031", "11111107", "11111210"};
int bossHealth[]= {28,28,28,28,28,28,28,28};
TEST(convertEx(damageChart, SIZEOFARRAY(damageChart)),
convertEx(bossHealth, SIZEOFARRAY(bossHealth)), 92);
}
void Test6()
{
}
void Test7()
{
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
cout<<"success";
getchar();
return 0;
}