Skip to content

Commit 1ff790d

Browse files
committed
srm 651 250
1 parent 86be257 commit 1ff790d

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

RobotOnMoonEasy.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 set<int> si;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
typedef long long ll;
18+
map<char, int> mx, my;
19+
20+
21+
class RobotOnMoonEasy{
22+
public:
23+
string isSafeCommand(vector <string> board, string S){
24+
int n = board.size(), m = board[0].size();
25+
int sx, sy;
26+
for(int i = 0; i < n; i++)
27+
for(int j = 0; j < m; j++)
28+
if(board[i][j] == 'S'){
29+
sx = i;
30+
sy = j;
31+
}
32+
mx['U'] = -1; mx['D'] = 1; mx['L'] = 0; mx['R'] = 0;
33+
my['U'] = 0; my['D'] = 0; my['L'] = -1; my['R'] = 1;
34+
board[sx][sy] = '.';
35+
36+
for(char c : S){
37+
int x = sx+mx[c], y = sy+my[c];
38+
if(x < 0 || x >= n || y < 0 || y >= m) return "Dead";
39+
if(board[x][y] == '#') continue;
40+
sx = x;
41+
sy = y;
42+
}
43+
return "Alive";
44+
}
45+
46+
47+
// BEGIN CUT HERE
48+
public:
49+
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(); if ((Case == -1) || (Case == 5)) test_case_5(); }
50+
private:
51+
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(); }
52+
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
53+
void test_case_0() { string Arr0[] = {".....",
54+
".###.",
55+
"..S#.",
56+
"...#."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "URURURURUR"; string Arg2 = "Alive"; verify_case(0, Arg2, isSafeCommand(Arg0, Arg1)); }
57+
void test_case_1() { string Arr0[] = {".....",
58+
".###.",
59+
"..S..",
60+
"...#."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "URURURURUR"; string Arg2 = "Dead"; verify_case(1, Arg2, isSafeCommand(Arg0, Arg1)); }
61+
void test_case_2() { string Arr0[] = {".....",
62+
".###.",
63+
"..S..",
64+
"...#."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "URURU"; string Arg2 = "Alive"; verify_case(2, Arg2, isSafeCommand(Arg0, Arg1)); }
65+
void test_case_3() { string Arr0[] = {"#####",
66+
"#...#",
67+
"#.S.#",
68+
"#...#",
69+
"#####"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "DRULURLDRULRUDLRULDLRULDRLURLUUUURRRRDDLLDD"; string Arg2 = "Alive"; verify_case(3, Arg2, isSafeCommand(Arg0, Arg1)); }
70+
void test_case_4() { string Arr0[] = {"#####",
71+
"#...#",
72+
"#.S.#",
73+
"#...#",
74+
"#.###"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "DRULURLDRULRUDLRULDLRULDRLURLUUUURRRRDDLLDD"; string Arg2 = "Dead"; verify_case(4, Arg2, isSafeCommand(Arg0, Arg1)); }
75+
void test_case_5() { string Arr0[] = {"S"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "R"; string Arg2 = "Dead"; verify_case(5, Arg2, isSafeCommand(Arg0, Arg1)); }
76+
77+
// END CUT HERE
78+
79+
};
80+
81+
// BEGIN CUT HERE
82+
int main(){
83+
84+
RobotOnMoonEasy ___test;
85+
___test.run_test(-1);
86+
}
87+
// END CUT HERE

RobotOnMoonEasy.html

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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>There is a robot on the moon.
2+
You are given a vector &lt;string&gt; <b>board</b> containing the map of a rectangular area.
3+
The robot is currently located somewhere in that area.
4+
In the map, the character '.' (period) represents an empty square, 'S' represents an empty square that currently contains the robot, and '#' represents an obstacle.
5+
<br></br>
6+
<br></br>
7+
You are also given a string <b>S</b>.
8+
This string represents a sequence of commands we are going to send to the robot.
9+
Each character in <b>S</b> is one of 'U', 'D', 'L', and 'R', representing a step up, down, left, and right, respectively.
10+
<br></br>
11+
<br></br>
12+
Whenever the robot receives a command, one of three things will happen:
13+
<ul>
14+
<li>If the requested move leads to an empty square, the robot performs the move.</li>
15+
<li>If the requested move leads to a square with an obstacle, the robot ignores the command and remains in place.</li>
16+
<li>If the requested move leads out of the mapped area, the robot leaves the mapped area and dies immediately.</li>
17+
</ul>
18+
<br></br>
19+
<br></br>
20+
Given the map and the sequence of commands, compute whether the robot will survive.
21+
Return "Alive" (quotes for clarity) if the robot is still somewhere on the map after the last command.
22+
Otherwise, return "Dead".
23+
Note that the return value is case-sensitive.</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>RobotOnMoonEasy</td></tr><tr><td>Method:</td><td>isSafeCommand</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;, string</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string isSafeCommand(vector &lt;string&gt; board, string S)</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><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>The direction 'U' corresponds to moving from <b>board</b>[i][j] to <b>board</b>[i-1][j]. The direction 'L' corresponds to moving from <b>board</b>[i][j] to <b>board</b>[i][j-1].</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>board</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>board</b> will contain between 1 and 50 characters.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>board</b> will contain the same number of characters.</td></tr><tr><td align="center" valign="top">-</td><td>The characters in <b>board</b> will be '.', '#' or 'S'.</td></tr><tr><td align="center" valign="top">-</td><td>There will be exactly one 'S' in <b>board</b>.</td></tr><tr><td align="center" valign="top">-</td><td><b>S</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in <b>S</b> will be one of 'U', 'D', 'L', 'R'.</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;.....&quot;,
24+
&quot;.###.&quot;,
25+
&quot;..S#.&quot;,
26+
&quot;...#.&quot;}</pre></td></tr><tr><td><pre>&quot;URURURURUR&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Alive&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">The robot will never move from its starting location: commands 'U' and 'R' are sending it into obstacles, so the robot ignores them.</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;.....&quot;,
27+
&quot;.###.&quot;,
28+
&quot;..S..&quot;,
29+
&quot;...#.&quot;}</pre></td></tr><tr><td><pre>&quot;URURURURUR&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Dead&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">This time there is no obstacle on the robot's right side. Its execution of commands will look as follows:
30+
<ol>
31+
<li>'U' leads into an obstacle. The robot ignores it.</li>
32+
<li>'R' leads into an empty square. The robot makes a step to the right.</li>
33+
<li>'U' leads into an obstacle. The robot ignores it.</li>
34+
<li>'R' leads into an empty square. The robot makes a step to the right.</li>
35+
<li>The next 'U' now leads into an empty square. The robot makes a step up. After this step, the robot is in row 1, column 4. (Both indices are 0-based.)</li>
36+
<li>'R' leads out of the map. The robot steps out of the map and dies. The remaining four commands never get executed - the robot is already dead.</li>
37+
</ol></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;.....&quot;,
38+
&quot;.###.&quot;,
39+
&quot;..S..&quot;,
40+
&quot;...#.&quot;}</pre></td></tr><tr><td><pre>&quot;URURU&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Alive&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></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;#####&quot;,
41+
&quot;#...#&quot;,
42+
&quot;#.S.#&quot;,
43+
&quot;#...#&quot;,
44+
&quot;#####&quot;}</pre></td></tr><tr><td><pre>&quot;DRULURLDRULRUDLRULDLRULDRLURLUUUURRRRDDLLDD&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Alive&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">There are obstacles all around the map. Regardless of how the robot moves, it is safe - the obstacles will prevent it from leaving the map.</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;#####&quot;,
45+
&quot;#...#&quot;,
46+
&quot;#.S.#&quot;,
47+
&quot;#...#&quot;,
48+
&quot;#.###&quot;}</pre></td></tr><tr><td><pre>&quot;DRULURLDRULRUDLRULDLRULDRLURLUUUURRRRDDLLDD&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Dead&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">After some steps the robot will leave the map from the only empty square in the bottom row of the map.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;S&quot;}</pre></td></tr><tr><td><pre>&quot;R&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Dead&quot;</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)