Skip to content

Commit ca3f04a

Browse files
committed
srm 643 div 2 250
1 parent 0721712 commit ca3f04a

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

TheKingsArmyDiv2.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class TheKingsArmyDiv2{
19+
public:
20+
int getNumber(vector <string> s) {
21+
int n = s.size(), m = s[0].size();
22+
int ch = 0;
23+
for(int i = 0; i < n; i++)
24+
for(int j = 0; j < m; j++){
25+
if(s[i][j] == 'H'){
26+
ch++;
27+
if(i+1 < n && s[i+1][j] == 'H') return 0;
28+
if(j+1 < m && s[i][j+1] == 'H') return 0;
29+
}
30+
}
31+
if(ch == 0) return 2;
32+
return 1;
33+
}
34+
35+
// BEGIN CUT HERE
36+
public:
37+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
38+
private:
39+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
40+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
41+
void test_case_0() { string Arr0[] = {"SSSSS",
42+
"SSHHS",
43+
"SSSSS"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(0, Arg1, getNumber(Arg0)); }
44+
void test_case_1() { string Arr0[] = {"SSSSS",
45+
"SSHSH",
46+
"HSSSS"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, getNumber(Arg0)); }
47+
void test_case_2() { string Arr0[] = {"SSS",
48+
"SSS",
49+
"SSS"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(2, Arg1, getNumber(Arg0)); }
50+
void test_case_3() { string Arr0[] = {"HSHSHSH", "SSSHSSS", "SSHSHSS", "SHSHSHS"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(3, Arg1, getNumber(Arg0)); }
51+
void test_case_4() { string Arr0[] = {"HHSH", "HHHS", "HSSS", "SHSH", "HHHS", "HSHH", "SSSH"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(4, Arg1, getNumber(Arg0)); }
52+
53+
// END CUT HERE
54+
55+
};
56+
57+
// BEGIN CUT HERE
58+
int main(){
59+
60+
TheKingsArmyDiv2 ___test;
61+
___test.run_test(-1);
62+
}
63+
// END CUT HERE

TheKingsArmyDiv2.html

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>
2+
The King of Byteland has an army that consists of R*C soldiers.
3+
He has just arranged the soldiers into a grid with R rows and C columns.
4+
Two soldiers are neighbors if they stand next to each other in a row or in a column.
5+
</p>
6+
<p>
7+
</p>
8+
<p>
9+
Each of the soldiers is currently either happy or sad.
10+
You are given their current states in a vector &lt;string&gt; <b>state</b> with R elements, each containing C characters.
11+
The character <b>state</b>[i][j] is either 'H' (if the soldier in row i, column j is happy) or 'S' (if that soldier is sad).
12+
</p>
13+
<p>
14+
</p>
15+
<p>
16+
Happiness is contagious.
17+
Whenever two neighbors are both happy, they will tell each other jokes and after a minute that will make all of their neighbors happy as well.
18+
</p>
19+
<p>
20+
</p>
21+
<p>
22+
Here's an example.
23+
There are two happy neighbors among many sad soldiers:
24+
</p>
25+
<p>
26+
</p>
27+
<pre>
28+
{"SSSSS",
29+
"SSHHS",
30+
"SSSSS"}
31+
</pre>
32+
<p>
33+
</p>
34+
<p>
35+
This is the situation after one minute: all of their neighbors are happy now.
36+
</p>
37+
<p>
38+
</p>
39+
<pre>
40+
{"SSHHS",
41+
"SHHHH",
42+
"SSHHS"}
43+
</pre>
44+
<p>
45+
</p>
46+
<p>
47+
And this is the situation after another minute. Now all the neighbors of the soldiers that are currently happy became happy as well.
48+
</p>
49+
<p>
50+
</p>
51+
<pre>
52+
{"SHHHH",
53+
"HHHHH",
54+
"SHHHH"}
55+
</pre>
56+
<p>
57+
</p>
58+
<p>
59+
After another minute, all the soldiers in the King's army would be happy.
60+
</p>
61+
<p>
62+
</p>
63+
<p>
64+
The King wants all his soldiers to be happy.
65+
Sometimes it's easy, as in the above example: all he has to do is wait for a while and all soldiers will become happy.
66+
However, it is not always the case.
67+
For example, in the situation below the happiness would not spread anywhere, each soldier would remain in his original state forever.
68+
(Note that a single happy soldier does not make his neighbors happy.)
69+
</p>
70+
<p>
71+
</p>
72+
<pre>
73+
{"SSSSS",
74+
"SSHSH",
75+
"HSSSS"}
76+
</pre>
77+
<p>
78+
</p>
79+
<p>
80+
The King can make a soldier happy by giving him an award for excellent service.
81+
Obviously, the King could make all soldiers happy by giving awards to all of them.
82+
But the King is smart and knows that there is a better solution.
83+
He will only give the awards to a few carefully selected soldiers and then he will simply wait until the happiness spreads to the rest of the army.
84+
</p>
85+
<p>
86+
</p>
87+
<p>
88+
You are given the vector &lt;string&gt; <b>state</b>.
89+
Compute and return the smallest number of awards the king has to give to make all soldiers happy in the end.
90+
</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>TheKingsArmyDiv2</td></tr><tr><td>Method:</td><td>getNumber</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getNumber(vector &lt;string&gt; state)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>state</b> will contain between 3 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>state</b> will contain between 3 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>state</b> will contain the same number of characters.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in each element of <b>state</b> will be either 'H' or 'S'.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;SSSSS&quot;,
91+
&quot;SSHHS&quot;,
92+
&quot;SSSSS&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">This is the first example in the problem statement. No awards are necessary, all soldiers will become happy anyway.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;SSSSS&quot;,
93+
&quot;SSHSH&quot;,
94+
&quot;HSSSS&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">This is the second example in the problem statement. The King needs to give at least one award. One optimal solution is to give an award to the soldier in row 1, column 3. (Both indices are 0-based.)</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;SSS&quot;,
95+
&quot;SSS&quot;,
96+
&quot;SSS&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">Here the King must give awards to two soldiers.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;HSHSHSH&quot;, &quot;SSSHSSS&quot;, &quot;SSHSHSS&quot;, &quot;SHSHSHS&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;HHSH&quot;, &quot;HHHS&quot;, &quot;HSSS&quot;, &quot;SHSH&quot;, &quot;HHHS&quot;, &quot;HSHH&quot;, &quot;SSSH&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)