Skip to content

Commit 181fe5d

Browse files
committed
srm 150 div 1 1000
1 parent 4dab694 commit 181fe5d

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

RoboCourier.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
#include <stack>
12+
using namespace std;
13+
typedef pair<int,int> pi;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
typedef vector<vs> vvs;
18+
#define inf 1000000;
19+
struct node
20+
{
21+
int x;
22+
int y;
23+
int d[6];
24+
node(int xx = 0, int yy = 0) : x(xx), y(yy) {
25+
for(int i = 0; i < 6; i++) d[i] = -1;
26+
}
27+
};
28+
29+
int mx[6] = {0, 1, 1, 0, -1, -1};
30+
int my[6] = {1, 0, -1, -1, 0, 1};
31+
32+
class RoboCourier{
33+
public:
34+
int timeToDeliver(vector <string> path){
35+
node R[505];
36+
map<pi, int> M;
37+
string total = "";
38+
for(string s : path) total += s;
39+
M[make_pair(0, 0)] = 0;
40+
R[0] = node(0, 0);
41+
int i = 0, j = 0, n = 0, k = 0, dest = 0;
42+
int dir = 0;
43+
44+
while(k < total.length()){
45+
node s = R[i];
46+
char c = total[k++];
47+
if(c == 'L') dir = (dir+5)%6;
48+
if(c == 'R') dir = (dir+1)%6;
49+
if(c == 'F'){
50+
int xx = s.x+mx[dir], yy = s.y+my[dir];
51+
if(M.count(make_pair(xx, yy)) == 0){
52+
M[make_pair(xx, yy)] = ++n;
53+
R[n] = node(xx, yy);
54+
}
55+
j = M[make_pair(xx, yy)];
56+
node p = R[j];
57+
// cout<<i<<") "<<s.x<<" "<<s.y<<" "<<dir<<" "<<p.x<<" "<<p.y<<endl;
58+
s.d[dir] = j;
59+
p.d[(dir+3)%6] = i;
60+
R[i] = s;
61+
R[j] = p;
62+
i = j;
63+
}
64+
}
65+
dest = i;
66+
// cout<<"dest = "<<i<<endl;
67+
int D[4000], visited[4000];
68+
for(int i = 0; i < 4000; i++){
69+
D[i] = inf;
70+
visited[i] = 0;
71+
}
72+
set<pi> Q;
73+
Q.insert(make_pair(0, 0));
74+
D[0] = 0;
75+
76+
while(1){
77+
int m = 3999;
78+
for(int i = 0; i <= 3050; i++) if(!visited[i] && D[i] < D[m]) m = i;
79+
if(m > 3050) exit(1);
80+
visited[m] = 1;
81+
int c = D[m];
82+
int pos = m/6, dir = m%6;
83+
if(pos == dest) return c;
84+
node p = R[pos];
85+
if(D[pos*6 + (dir+1)%6] > c+3) D[pos*6 + (dir+1)%6] = c+3;
86+
if(D[pos*6 + (dir+5)%6] > c+3) D[pos*6 + (dir+5)%6] = c+3;
87+
int steps = 0;
88+
while(p.d[dir] >= 0){
89+
steps++;
90+
int q = p.d[dir], cost = 0;
91+
// cout<<"q = "<<q<<" steps = "<<steps<<endl;
92+
if(steps > 1) cost = (steps-2)*2 + 8;
93+
else cost = steps*4;
94+
if(D[q*6 + dir] > c+cost){
95+
D[q*6 + dir] = c+cost;
96+
// cout<<"D["<<q*6 + dir<<"] = "<<c+cost<<endl;
97+
}
98+
p = R[q];
99+
}
100+
}
101+
}
102+
103+
// BEGIN CUT HERE
104+
public:
105+
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(); }
106+
private:
107+
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(); }
108+
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; } }
109+
void test_case_0() { string Arr0[] = { "FRRFLLFLLFRRFLF" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 15; verify_case(0, Arg1, timeToDeliver(Arg0)); }
110+
void test_case_1() { string Arr0[] = { "RFLLF" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 17; verify_case(1, Arg1, timeToDeliver(Arg0)); }
111+
void test_case_2() { string Arr0[] = { "FLFRRFRFRRFLLFRRF" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(2, Arg1, timeToDeliver(Arg0)); }
112+
void test_case_3() { string Arr0[] = { "FFFFFFFFFRRFFFFFFRRFFFFF",
113+
"FLLFFFFFFLLFFFFFFRRFFFF" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 44; verify_case(3, Arg1, timeToDeliver(Arg0)); }
114+
void test_case_4() { string Arr0[] = { "RFLLFLFLFRFRRFFFRFFRFFRRFLFFRLRRFFLFFLFLLFRFLFLRFF",
115+
"RFFLFLFFRFFLLFLLFRFRFLRLFLRRFLRFLFFLFFFLFLFFRLFRLF",
116+
"LLFLFLRLRRFLFLFRLFRF" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 24; verify_case(4, Arg1, timeToDeliver(Arg0)); }
117+
void test_case_5() { string Arr0[] = { "LLFLFRLRRLRFFLRRRRFFFLRFFRRRLLFLFLLRLRFFLFRRFFFLFL",
118+
"RLFFRRLRLRRFFFLLLRFRLLRFFLFRLFRRFRRRFRLRLRLFFLLFLF",
119+
"FRFLRFRRLLLRFFRRRLRFLFRRFLFFRLFLFLFRLLLLFRLLRFLLLF",
120+
"FFLFRFRRFLLFFLLLFFRLLFLRRFRLFFFRRFFFLLRFFLRFRRRLLR",
121+
"FFFRRLLFLLRLFRRLRLLFFFLFLRFFRLRLLFLRLFFLLFFLLFFFRR",
122+
"LRFRRFLRRLRRLRFFFLLLLRRLRFFLFRFFRLLRFLFRRFLFLFFLFR",
123+
"RFRRLRRFLFFFLLRFLFRRFRFLRLRLLLLFLFFFLFRLLRFRLFRLFR",
124+
"LLFLFRLFFFFFFFRRLRLRLLRFLRLRRRRRRRRLFLFLFLRFLFRLFF",
125+
"RLFRRLLRRRRFFFRRRLLLLRRLFFLLLLLRFFFFRFRRLRRRFFFLLF",
126+
"FFFFLRRLRFLLRRLRLRFRRRRLFLLRFLRRFFFRFRLFFRLLFFRRLL" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 169; verify_case(5, Arg1, timeToDeliver(Arg0)); }
127+
128+
// END CUT HERE
129+
130+
};
131+
132+
// BEGIN CUT HERE
133+
int main(){
134+
135+
RoboCourier ___test;
136+
___test.run_test(-1);
137+
}
138+
// END CUT HERE

RoboCourier.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
A robotic courier needs to deliver a package through a minefield, following a safe path discovered by
3+
a robotic scout. The scout's path is not necessarily as efficient as it could be. For example, it might
4+
loop back on itself. The courier need not follow the path exactly, but can take shortcuts when it can do
5+
so safely.
6+
</p>
7+
8+
<p>
9+
Each robot can make only three kinds of moves, each represented by a single letter: it can go forward 1 meter ('F'),
10+
pivot right 60 degrees ('R'), or pivot left 60 degrees ('L'). Notice that the locations on which a robot can begin or
11+
end a move form a hexagonal grid.
12+
The scout and courier begin in the same location, facing in the same direction. The courier's goal is to reach the last
13+
location visited by the scout as quickly as possible. To travel safely, the courier must always stay in the wheel tracks
14+
of the scout. That is, any forward movement made by the courier must be along a path segment traveled by the scout. Pivoting
15+
left or right is always safe. Note that the courier is permitted to follow a path segment in either the same or the opposite
16+
direction as the scout. Similarly, the courier may be facing in any direction when it reaches its final destination--it need not
17+
end up facing in same direction as the scout.
18+
</p>
19+
20+
<p>
21+
The courier requires 3 seconds to pivot left or right 60 degrees, and 4 seconds to move forward one meter. However, because
22+
of acceleration effects, the courier can move faster when it makes several consecutive forward moves. The first and last moves
23+
in any such sequence take 4 seconds each, but intermediate moves in the sequence take 2 seconds each. For example, it would
24+
take the courier 20 seconds to travel 8 meters in a straight line: 4 seconds for the first meter, 4 seconds for the last
25+
meter, and 12 seconds for the six meters in between.
26+
</p>
27+
28+
<p>
29+
For example, suppose the scout follows the path &quot;FRRFLLFLLFRRFLF&quot; (all quotes for clarity only). Altogether, it visits six locations, which can be drawn as
30+
</p>
31+
<pre>
32+
_
33+
/6\_
34+
\_/5\_
35+
\_/4\
36+
/2\_/
37+
\_/3\
38+
/1\_/
39+
\_/
40+
</pre>
41+
It begins in hexagon 1, facing upward, and travels in order to hexagons 2, 3, and 4. It then returns to hexagon 2 before continuing on to hexagons 5 and 6. The courier could deliver the package in a minimum of 15 seconds, using the path &quot;FFLF&quot; which visits hexagons 1, 2, 5, and 6.
42+
43+
<p>
44+
The scout's path will be given as a vector &lt;string&gt; <b>path</b>, rather than as a single string. For example,
45+
the scout's path above might have been given as {&quot;FRRFLLFLL&quot;, &quot;FRRFLF&quot;}.
46+
Note that there is no significance to where the breaks fall between strings in <b>path</b>; it is best to think
47+
of all the strings being concatenated together. Given the <b>path</b>, you must calculate the minimum time needed for
48+
the courier to deliver its package.
49+
</p>
50+
51+
</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>RoboCourier</td></tr><tr><td>Method:</td><td>timeToDeliver</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 timeToDeliver(vector &lt;string&gt; path)</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>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>path</b> contains between 1 and 10 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>path</b> contains between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>path</b> contains only the characters 'F', 'L', and '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;FRRFLLFLLFRRFLF&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 15</pre></td></tr><tr><td><table><tr><td colspan="2">The example above.</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;RFLLF&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 17</pre></td></tr><tr><td><table><tr><td colspan="2">Even though the ending location is one meter in front of the starting location, the courier
52+
cannot simply go forward, because that would not be safe. It must follow in the tracks of
53+
the scout.</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;FLFRRFRFRRFLLFRRF&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">Scout ends up at starting location.</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;FFFFFFFFFRRFFFFFFRRFFFFF&quot;,
54+
&quot;FLLFFFFFFLLFFFFFFRRFFFF&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 44</pre></td></tr><tr><td><table><tr><td colspan="2">The shortest path is &quot;FFFRFFFFFFRFFFF&quot;.</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;RFLLFLFLFRFRRFFFRFFRFFRRFLFFRLRRFFLFFLFLLFRFLFLRFF&quot;,
55+
&quot;RFFLFLFFRFFLLFLLFRFRFLRLFLRRFLRFLFFLFFFLFLFFRLFRLF&quot;,
56+
&quot;LLFLFLRLRRFLFLFRLFRF&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 24</pre></td></tr><tr><td></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;LLFLFRLRRLRFFLRRRRFFFLRFFRRRLLFLFLLRLRFFLFRRFFFLFL&quot;,
57+
&quot;RLFFRRLRLRRFFFLLLRFRLLRFFLFRLFRRFRRRFRLRLRLFFLLFLF&quot;,
58+
&quot;FRFLRFRRLLLRFFRRRLRFLFRRFLFFRLFLFLFRLLLLFRLLRFLLLF&quot;,
59+
&quot;FFLFRFRRFLLFFLLLFFRLLFLRRFRLFFFRRFFFLLRFFLRFRRRLLR&quot;,
60+
&quot;FFFRRLLFLLRLFRRLRLLFFFLFLRFFRLRLLFLRLFFLLFFLLFFFRR&quot;,
61+
&quot;LRFRRFLRRLRRLRFFFLLLLRRLRFFLFRFFRLLRFLFRRFLFLFFLFR&quot;,
62+
&quot;RFRRLRRFLFFFLLRFLFRRFRFLRLRLLLLFLFFFLFRLLRFRLFRLFR&quot;,
63+
&quot;LLFLFRLFFFFFFFRRLRLRLLRFLRLRRRRRRRRLFLFLFLRFLFRLFF&quot;,
64+
&quot;RLFRRLLRRRRFFFRRRLLLLRRLFFLLLLLRFFFFRFRRLRRRFFFLLF&quot;,
65+
&quot;FFFFLRRLRFLLRRLRLRFRRRRLFLLRFLRRFFFRFRLFFRLLFFRRLL&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 169</pre></td></tr><tr><td></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)