Skip to content

Commit 572150d

Browse files
committed
srm 198 div 1 500
1 parent accf27b commit 572150d

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

DungeonEscape.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
struct state{
18+
int x;
19+
int y;
20+
int t;
21+
state(int x, int y, int t) : x(x) , y(y), t(t) {}
22+
};
23+
bool operator<(state a, state b){
24+
return a.t > b.t;
25+
}
26+
27+
class DungeonEscape{
28+
public:
29+
int exitTime(vector <string> up, vector <string> down, vector <string> east, vector <string> west, int startLevel, int startEasting) {
30+
int m = up.size(), n = up[0].size();
31+
priority_queue<state> Q;
32+
Q.push(state(startLevel, startEasting, 0));
33+
vvi visited(55, vi(55, 0));
34+
35+
while(!Q.empty()){
36+
state p = Q.top();
37+
Q.pop();
38+
int x = p.x, y = p.y, t = p.t;
39+
// cout<<x<<" "<<y<<" "<<t<<endl;
40+
if(x == -1) return t;
41+
if(visited[x][y]) continue;
42+
visited[x][y] = 1;
43+
//move west/left
44+
if(y > 0 && west[x][y] != 'x'){
45+
int tt = t+(west[x][y]-'0');
46+
if(tt < n*(m-x)) Q.push(state(x, y-1, tt));
47+
}
48+
//move east/right
49+
if(y < n-1 && east[x][y] != 'x'){
50+
int tt = t+(east[x][y]-'0');
51+
if(tt < n*(m-x)) Q.push(state(x, y+1, tt));
52+
}
53+
//move up
54+
if(up[x][y] != 'x'){
55+
int tt = t+(up[x][y]-'0');
56+
if(x == 0 || tt < n*(m-x+1)) Q.push(state(x-1, y, tt));
57+
}
58+
//move down
59+
if(x < m-1 && down[x][y] != 'x'){
60+
int tt = t+(down[x][y]-'0');
61+
if(tt < n*(m-x-1)) Q.push(state(x+1, y, tt));
62+
}
63+
}
64+
return -1;
65+
66+
}
67+
68+
// BEGIN CUT HERE
69+
public:
70+
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(); }
71+
private:
72+
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(); }
73+
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; } }
74+
void test_case_0() { string Arr0[] = {"0x4",
75+
"0x3",
76+
"0x3"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0x9",
77+
"009",
78+
"0x9"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0x9",
79+
"1x9",
80+
"009"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"0x9",
81+
"0x0",
82+
"009"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 2; int Arg5 = 2; int Arg6 = 10; verify_case(0, Arg6, exitTime(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); }
83+
void test_case_1() { string Arr0[] = {"xxxxxxxxx1",
84+
"1xxxxxxxxx",
85+
"xxxxxxxxx1"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"xxxxxxxxxx",
86+
"xxxxxxxxxx",
87+
"xxxxxxxxxx"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"1111111111",
88+
"xxxxxxxxxx",
89+
"1111111111"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"xxxxxxxxxx",
90+
"1111111111",
91+
"xxxxxxxxxx"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 2; int Arg5 = 0; int Arg6 = 30; verify_case(1, Arg6, exitTime(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); }
92+
void test_case_2() { string Arr0[] = {"xxxxxxxxx1",
93+
"xxxx999991",
94+
"x999999991"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"1111111111",
95+
"1111111111",
96+
"1111111111"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"1111122242",
97+
"2222222241",
98+
"2111111111"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"xxxxxxxxx1",
99+
"1111111111",
100+
"xxxxxxxxx1"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 2; int Arg5 = 0; int Arg6 = -1; verify_case(2, Arg6, exitTime(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); }
101+
void test_case_3() { string Arr0[] = {"1x2x3x4x5x6x7x8x9",
102+
"00000000000000000",
103+
"98765432223456789",
104+
"12345678987654321"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"00000000000000000",
105+
"00000000000000000",
106+
"00000000000000000",
107+
"00000000000000000"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"xxxxxxxxxxxxxxxxx",
108+
"xxxxxxxxxxxxxxxxx",
109+
"22222222222222222",
110+
"33333333333333333"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"xxxxxxxxxxxxxxxxx",
111+
"xxxxxxxxxxxxxxxxx",
112+
"22222222222222222",
113+
"33333333333333333"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 3; int Arg5 = 12; int Arg6 = 17; verify_case(3, Arg6, exitTime(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); }
114+
115+
// END CUT HERE
116+
117+
};
118+
119+
// BEGIN CUT HERE
120+
int main(){
121+
122+
DungeonEscape ___test;
123+
___test.run_test(-1);
124+
}
125+
// END CUT HERE

DungeonEscape.html

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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>You are the famous explorer Indiana Jones, or Lara Croft, take your pick. You are exploring the ruins of the dungeons beneath King Lockumup IV the Good's castle in Flatlandia. Of course, the dungeon layout is two-dimensional (like your character), East-West and Up-Down in this case, in a regular grid.
2+
</p>
3+
<pre>
4+
Surface
5+
| | | | | | | | |
6+
level 0 -R-R-R-R-R-R-R-R-R-
7+
| | | | | | | | |
8+
level 1 &lt;- West -R-R-R-R-R-R-R-R-R- East -&gt;
9+
| | | | | | | | |
10+
level 2 -R-R-R-R-R-R-R-R-R-
11+
| | | | | | | | |
12+
Depths of Despair
13+
</pre>
14+
<p> &quot;R&quot; indicates a room. &quot;-&quot; indicates an east-west passageway. &quot;|&quot; indicates an up-down passageway.
15+
</p>
16+
<p>
17+
Because it is rough going in the passageways between the rooms (there has been no dungeon maintenance for centuries), it is frequently easier to travel through a passageway in a particular direction than in the opposite direction. Each room has four passageways leaving in the directions East (right), West (left), Up, and Down which lead to adjacent rooms (except the Down in the bottom-most rooms, the East in the east-most rooms, and the West in the west-most rooms, which have dead-end passageways due to ancient budget cuts, and Up in the topmost rooms which lead to the surface). The time it takes to travel through a passageway from a given room to the adjacent rooms is given in four vector &lt;string&gt;s depending on your direction of travel. A digit between 0 and 9 indicates how many time units (in the local system of decimillifortnights, dmfs) are taken to leave the room in that direction and travel through the passageway to the adjacent room. An 'x' character indicates that the travel in that direction is too difficult and can not be done. The dead end passageways (at the edges of the dungeon) have time values, or 'x', specified (because they were in the original plans for the dungeon and we have an old map), but we can not actually travel through these passageways in this problem. The dungeon does *not* wrap in any direction (you are probably thinking of the castle of Queen Mobius the One Sided, the former stripper).
18+
</p>
19+
<p> In other words, if you are in room (i,j), where i is the up-down level and j is your easting (ie. how far east you are) coordinate,
20+
then </p>
21+
<ul>
22+
<li><b>up</b>[i][j] tells how many dmfs it takes to get to (i-1,j).</li>
23+
<li><b>down</b>[i][j] tells how many dmfs it takes to get to (i+1,j).</li>
24+
<li><b>east</b>[i][j] tells how many dmfs it takes to get to (i,j+1).</li>
25+
<li><b>west</b>[i][j] tells how many dmfs it takes to get to (i,j-1).</li>
26+
</ul>
27+
28+
<p>If it is obvious to you how these four directional time value arrays map to
29+
a directed graph of the dungeon, then skip this next section of the problem
30+
description, which goes into detail, and continue reading below for more
31+
of the important problem description information.</p>
32+
<ul><li>
33+
<p> For example if given the inputs <b>up</b> and <b>west</b> (shown below)</p>
34+
<pre>
35+
up = {&quot;123&quot;, west = {&quot;222&quot;,
36+
&quot;111&quot;, &quot;131&quot;,
37+
&quot;121&quot;} &quot;444&quot;}
38+
</pre>
39+
You would have the following time values for each passageway while going up or west.
40+
<pre>
41+
Surface
42+
1 2 3
43+
level 0 2R2R2R- Up and West going
44+
1 1 1 Passageway times
45+
level 1 West 1R3R1R- East
46+
1 2 1
47+
level 2 4R4R4R-
48+
| | |
49+
Depths of Despair
50+
</pre>
51+
<p>The dead-end passageways on the far west with times {2, 1, 4} are useless and can be ignored.</p>
52+
<br></br>
53+
<p>Similarly if you have the inputs <b>down</b> and <b>east</b> (shown below)</p>
54+
<pre>
55+
down = {&quot;987&quot;, east = {&quot;222&quot;,
56+
&quot;111&quot;, &quot;3x3&quot;,
57+
&quot;121&quot;} &quot;111&quot;}
58+
</pre>
59+
You would have the following time values for each passageway while going down or east.
60+
<pre>
61+
Surface
62+
| | |
63+
level 0 -R2R2R2 Down and East going
64+
9 8 7 Passageway times
65+
level 1 West -R3RxR3 East
66+
1 1 1
67+
level 2 -R1R1R1
68+
1 2 1
69+
Depths of Despair
70+
</pre>
71+
<p>The dead-end passageways on the far east with times {2, 3, 1} and the very bottom with times {1, 2, 1} are also
72+
useless to you and can be ignored.</p>
73+
</li></ul>
74+
<p>We are back from the boring details, here is some more important information.</p>
75+
<p>
76+
Unfortunately for you, Dr. Jones or Dr. Croft, you have just triggered an ancient trap, and the dungeon is beginning to to fill with water. First the lowest level fills with water. If the East-West width of the dungeon is n rooms, then each level of the dungeon takes n decimillifortnights to fill. Once full of water, the rooms on that level are no longer accessible. While partly full of water, they are still fully accessible. Time starts at time = 0, at time = n the lowest level becomes inaccessible, at time = 2n the second lowest level becomes inaccessible, etc. So if you are in, or pass through, a room on the lowest level at time &gt;= n, you are dead.
77+
</p>
78+
<p>For simplicity, we will only consider if the room is completely filled with water when you enter. So if you leave a nearly filled room, going up through a slow
79+
passageway, and arrive somewhat later in a now nearly filled room one level up, this is ok. We will ignore the physics which would lead us to think the passageway would fill with water before the room above it. Only check for drowning when you enter the room. Also the surface (above level 0) never fills with water (we run out of water before then), so you can not drown on the surface.
80+
</p>
81+
<p>
82+
Your goal is to get to the surface as fast as possible. You start at the location (<b>startLevel</b>, <b>startEasting</b>). &quot;Easting&quot; is how far east you are in the local coordinate system. Rooms have Easting coordinates between 0 and n-1 inclusive, where
83+
n is the number of rooms on each level. Return the number of time units (decimillifortnights) it takes to escape, or -1 if escape is impossible.
84+
</p>
85+
<p>For example:</p>
86+
<pre>
87+
<b>up</b> = {&quot;0x4&quot;, <b>down</b> = {&quot;0x9&quot;, <b>east</b> = {&quot;1x9&quot;, <b>west</b> = {&quot;0x9&quot;,
88+
&quot;0x3&quot;, &quot;009&quot;, &quot;0x9&quot;, &quot;0x0&quot;,
89+
&quot;0x3&quot;} &quot;0x9&quot;} &quot;009&quot;} &quot;009&quot;}
90+
<b>startLevel</b> = 2, <b>startEasting</b> = 2
91+
</pre>
92+
<p>
93+
We start in the lower right corner. If water were not an issue, we could reach each of the various rooms
94+
with various paths, and the earliest possible times in which we could reach each room are shown below. If we go straight up,
95+
the passageways take 3, 3 and 4 dmfs reaching the surface in 10 dmfs. We could also follow the path: up (3 dmfs), west (0 dmfs),
96+
down (0 dmfs), west (0 dmfs), up (0 dmfs), up (0 dmfs), and up (0 dmfs) reaching the surface in 3 dmfs. The diagram
97+
below shows the minimum time in which we could first get to each room (if water were not an issue), and the
98+
passageways used are shown with '|' for up-down, and '-' for east-west.
99+
</p>
100+
<pre>
101+
3 10 - surface
102+
| | -------------
103+
3-4 6 - level 0
104+
| |
105+
3 3-3 - level 1
106+
| | |
107+
3-3 0 - level 2
108+
</pre>
109+
<p> But since level 2 fills with water at time 3, we can not move from (1,1) down to (2,1) at time = 3 dmfs. The
110+
actual rooms we can reach, considering flooding, are shown below, where 'w' represents a room which is full of water
111+
when we first could move into it, and 'x' represents a room we can never reach:</p>
112+
<pre>
113+
10 - surface
114+
| ------------
115+
x w-6 - level 0
116+
|
117+
x 3-3 - level 1
118+
| |
119+
x w 0 - level 2
120+
</pre>
121+
<p>
122+
In this example we can reach the surface in 10 dmfs by going straight up, and this is the minimum,
123+
so return 10.
124+
</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>DungeonEscape</td></tr><tr><td>Method:</td><td>exitTime</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;, vector &lt;string&gt;, vector &lt;string&gt;, vector &lt;string&gt;, int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int exitTime(vector &lt;string&gt; up, vector &lt;string&gt; down, vector &lt;string&gt; east, vector &lt;string&gt; west, int startLevel, int startEasting)</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>up</b>, <b>down</b>, <b>east</b> and <b>west</b> have the same constraints, only <b>up</b> is described.</td></tr><tr><td align="center" valign="top">-</td><td><b>up</b> will contain between 1 and 50 elements inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>each element of <b>up</b> will contain between 1 and 50 characters inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>each character in each element of <b>up</b> will be a digit between '0' and '9' inclusive or 'x'.</td></tr><tr><td align="center" valign="top">-</td><td><b>up</b>, <b>down</b>, <b>east</b> and <b>west</b> will all have exactly the same number of elements in each.</td></tr><tr><td align="center" valign="top">-</td><td>All elements of <b>up</b>, <b>down</b>, <b>east</b> and <b>west</b> will contain the same number of characters.</td></tr><tr><td align="center" valign="top">-</td><td><b>startLevel</b> will be between 0 and (the number of elements in <b>up</b>) - 1 inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>startEasting</b> will be between 0 and (the number of characters in <b>up</b>[0]) - 1 inclusive.</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;0x4&quot;,
125+
&quot;0x3&quot;,
126+
&quot;0x3&quot;}</pre></td></tr><tr><td><pre>{&quot;0x9&quot;,
127+
&quot;009&quot;,
128+
&quot;0x9&quot;}</pre></td></tr><tr><td><pre>{&quot;0x9&quot;,
129+
&quot;1x9&quot;,
130+
&quot;009&quot;}</pre></td></tr><tr><td><pre>{&quot;0x9&quot;,
131+
&quot;0x0&quot;,
132+
&quot;009&quot;}</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 10</pre></td></tr><tr><td><table><tr><td colspan="2">The example from 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;xxxxxxxxx1&quot;,
133+
&quot;1xxxxxxxxx&quot;,
134+
&quot;xxxxxxxxx1&quot;}</pre></td></tr><tr><td><pre>{&quot;xxxxxxxxxx&quot;,
135+
&quot;xxxxxxxxxx&quot;,
136+
&quot;xxxxxxxxxx&quot;}</pre></td></tr><tr><td><pre>{&quot;1111111111&quot;,
137+
&quot;xxxxxxxxxx&quot;,
138+
&quot;1111111111&quot;}</pre></td></tr><tr><td><pre>{&quot;xxxxxxxxxx&quot;,
139+
&quot;1111111111&quot;,
140+
&quot;xxxxxxxxxx&quot;}</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: 30</pre></td></tr><tr><td><table><tr><td colspan="2">Only one serpentine path out, just avoiding the water.</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;xxxxxxxxx1&quot;,
141+
&quot;xxxx999991&quot;,
142+
&quot;x999999991&quot;}</pre></td></tr><tr><td><pre>{&quot;1111111111&quot;,
143+
&quot;1111111111&quot;,
144+
&quot;1111111111&quot;}</pre></td></tr><tr><td><pre>{&quot;1111122242&quot;,
145+
&quot;2222222241&quot;,
146+
&quot;2111111111&quot;}</pre></td></tr><tr><td><pre>{&quot;xxxxxxxxx1&quot;,
147+
&quot;1111111111&quot;,
148+
&quot;xxxxxxxxx1&quot;}</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2">No way out that is fast enough, glub, glub...</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;1x2x3x4x5x6x7x8x9&quot;,
149+
&quot;00000000000000000&quot;,
150+
&quot;98765432223456789&quot;,
151+
&quot;12345678987654321&quot;}</pre></td></tr><tr><td><pre>{&quot;00000000000000000&quot;,
152+
&quot;00000000000000000&quot;,
153+
&quot;00000000000000000&quot;,
154+
&quot;00000000000000000&quot;}</pre></td></tr><tr><td><pre>{&quot;xxxxxxxxxxxxxxxxx&quot;,
155+
&quot;xxxxxxxxxxxxxxxxx&quot;,
156+
&quot;22222222222222222&quot;,
157+
&quot;33333333333333333&quot;}</pre></td></tr><tr><td><pre>{&quot;xxxxxxxxxxxxxxxxx&quot;,
158+
&quot;xxxxxxxxxxxxxxxxx&quot;,
159+
&quot;22222222222222222&quot;,
160+
&quot;33333333333333333&quot;}</pre></td></tr><tr><td><pre>3</pre></td></tr><tr><td><pre>12</pre></td></tr></table></td></tr><tr><td><pre>Returns: 17</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)