|
| 1 | +<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </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 <string> <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 <string> <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>    </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 <string></td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getNumber(vector <string> 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>    </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>    </td><td><table><tr><td><table><tr><td><pre>{"SSSSS", |
| 91 | + "SSHHS", |
| 92 | + "SSSSS"}</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>    </td><td><table><tr><td><table><tr><td><pre>{"SSSSS", |
| 93 | + "SSHSH", |
| 94 | + "HSSSS"}</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>    </td><td><table><tr><td><table><tr><td><pre>{"SSS", |
| 95 | + "SSS", |
| 96 | + "SSS"}</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>    </td><td><table><tr><td><table><tr><td><pre>{"HSHSHSH", "SSSHSSS", "SSHSHSS", "SHSHSHS"}</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>    </td><td><table><tr><td><table><tr><td><pre>{"HHSH", "HHHS", "HSSS", "SHSH", "HHHS", "HSHH", "SSSH"}</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