Skip to content

Commit 343c88b

Browse files
committed
tccc 04 round 4 500
1 parent 572150d commit 343c88b

File tree

2 files changed

+260
-0
lines changed

2 files changed

+260
-0
lines changed

BombMan.cpp

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
{
19+
int x, y, t, b;
20+
state(int x, int y, int t, int b) : x(x), y(y), t(t), b(b) {}
21+
};
22+
bool operator<(state a, state b){
23+
if(a.t != b.t) return a.t > b.t;
24+
return a.b < b.b;
25+
}
26+
27+
class BombMan{
28+
public:
29+
int shortestPath(vector <string> maze, int bombs) {
30+
int n = maze.size(), m = maze[0].size();
31+
int dx[4] = {1, 0, -1, 0};
32+
int dy[4] = {0, 1, 0, -1};
33+
priority_queue<state> Q;
34+
int bx = 0, by = 0, ex = 0, ey = 0;
35+
for(int i = 0; i < n; i++)
36+
for(int j = 0; j < m; j++)
37+
if(maze[i][j] == 'B'){
38+
bx = i;
39+
by = j;
40+
}
41+
else if(maze[i][j] == 'E'){
42+
ex = i;
43+
ey = j;
44+
}
45+
cout<<"bx = "<<bx<<" by = "<<by<<endl;
46+
cout<<"ex = "<<ex<<" ey = "<<ey<<endl;
47+
48+
vvi visited(n, vi(m, 0));
49+
Q.push(state(bx, by, 0, bombs));
50+
while(!Q.empty()){
51+
state p = Q.top();
52+
Q.pop();
53+
int x = p.x, y = p.y, t = p.t, b = p.b;
54+
if(visited[x][y]) continue;
55+
// cout<<x<<" "<<y<<" "<<t<<" "<<b<<endl;
56+
if(x == ex && y == ey) return t;
57+
visited[x][y] = 1;
58+
for(int k = 0; k < 4; k++){
59+
int xx = x+dx[k], yy = y+dy[k];
60+
if(0 <= xx && xx < n && 0 <= yy && yy < m){
61+
if(maze[xx][yy] == '#'){
62+
if(b > 0) Q.push(state(xx, yy, t+3, b-1));
63+
}
64+
else Q.push(state(xx, yy, t+1, b));
65+
}
66+
}
67+
}
68+
return -1;
69+
}
70+
71+
// BEGIN CUT HERE
72+
public:
73+
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(); }
74+
private:
75+
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(); }
76+
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; } }
77+
void test_case_0() { string Arr0[] = {".....B.",
78+
".#####.",
79+
".#...#.",
80+
".#E#.#.",
81+
".###.#.",
82+
"......."}
83+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 8; verify_case(0, Arg2, shortestPath(Arg0, Arg1)); }
84+
void test_case_1() { string Arr0[] = {"B.#.#.#...E"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = -1; verify_case(1, Arg2, shortestPath(Arg0, Arg1)); }
85+
void test_case_2() { string Arr0[] = {"..#####",
86+
"B.#####",
87+
"..#####",
88+
"#######",
89+
"####...",
90+
"####.E."}
91+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 17; verify_case(2, Arg2, shortestPath(Arg0, Arg1)); }
92+
void test_case_3() { string Arr0[] = {".#.#.#.#B#...#.#...#.#...#.#...#.#...#.#.#.......",
93+
".#.#.#.#.#.###.###.#.###.#.#.###.###.#.#.#.###.##",
94+
".#.#.#...#.#.#.#.#.#...#.....#.#.#...#...#.#.#...",
95+
".#.#.###.#.#.#.#.#.###.#.#####.#.###.###.#.#.###.",
96+
".............#.#...#...#.....#.#.#...#.#.#.....#.",
97+
"##.#######.###.#.#####.#.#####.#.###.#.#.#.#.####",
98+
".#.#.....#...#...#.#...#...#.#.#...#...#...#.....",
99+
".#######.#.#####.#.#.#.#.###.#.###.#.#####.#.####",
100+
".#.#.#.#...#.#.#.#.#.#.......#...#.#...#.#.#.....",
101+
".#.#.#.###.#.#.#.#.#####.#####.###.###.#.#.######",
102+
".....#...#.#...#...#...#...#...#...#.#.#.........",
103+
"####.###.#.###.###.#.###.#.#.###.###.#.#.########",
104+
".......#.........#.#.#.#.#.#.#.#.........#...#...",
105+
".#.###.#########.#.#.#.#.###.#.#####.#.#.#.###.##",
106+
".#.#.........#.#.#.#.#.....#.#.#.....#.#.........",
107+
"############.#.#.#.#.#.#####.#.#.################",
108+
".#...........#...#.#.#.#...#.#.#...#.#.#.....#...",
109+
".#####.#####.###.#.#.#.#.###.#.#.###.#.#.#####.##",
110+
".......#...#.#.#.....#...#...#.#.#.#.#...........",
111+
"##########.#.#.#####.#.###.###.#.#.#.#.##########",
112+
".....#...#.....#.#...#.......#.#...#.......#.....",
113+
"##.#.###.#.###.#.#.#.#.#####.#.#.###.#######.####",
114+
"...#...#...#.......#.....#.#...#...#.......#.....",
115+
"####.#.#.#########.#.###.#.#####.###.#.#######.##",
116+
".#...#...#.........#.#.....#.........#.#.#.#.....",
117+
".#####.#.#.###.#######.#.###.#.#########.#.#.####",
118+
".......#.#.#...#.......#.....#.#.#.......#.#.#.#.",
119+
"########.#.#.#.#####.#.###.#.###.#.#######.#.#.#.",
120+
".........#.#.#.#.....#...#.#.........#.#.........",
121+
"################.#.#.#.#.#.#.#.#######.#.########",
122+
".................#.#.#.#.#.#.#...........#.......",
123+
"########.#####.#.###.#.#.#####.###.#.#####.###.##",
124+
".........#...#.#...#.#.#...#.....#.#.........#...",
125+
".#####.#####.#.###.#.###.#.#.#.#.#.#####.#.###.#.",
126+
".#.....#.........#.#.#...#.#.#.#.#.#.....#...#.#.",
127+
"####.#####.###.#.#.#.#.#.#.###.###.#.#.#.#.#####.",
128+
".....#.....#.#.#.#.#.#.#.#.#...#...#.#.#.#...#...",
129+
"####.#.#.###.#.#.###.#.###.#.#.#####.#.#.#.######",
130+
".....#.#.#.#...#...#.#...#.#.#...#...#.#.#.......",
131+
"##########.#.#.#.#####.###.#.#.###.#.###.#####.##",
132+
"...#.#...#...#.#.....#.#...#.#...#.#.#.......#...",
133+
".#.#.#.#.#.#.#.#.#.#.###.#.#########.###.#.#.#.#.",
134+
".#.#...#...#.#.#.#.#...#.#...#.......#...#.#.#.#.",
135+
"##.###.#.#.###.#.#.#.#.#####.#.#.#.###.#.########",
136+
".......#.#...#.#.#.#.#.#.....#.#.#...#.#.........",
137+
"####.#######.#.#####.#.###.#.#.###.#.#.#.########",
138+
"E..#.......#.#.....#.#.#.#.#.#.#...#.#.#.........",
139+
"##.#.#.#.###.###.###.###.#.#.###.#.#.#.#.#######.",
140+
".....#.#...#.#.....#.#.....#...#.#.#.#.#.....#..."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 76; verify_case(3, Arg2, shortestPath(Arg0, Arg1)); }
141+
142+
// END CUT HERE
143+
144+
};
145+
146+
// BEGIN CUT HERE
147+
int main(){
148+
149+
BombMan ___test;
150+
___test.run_test(-1);
151+
}
152+
// END CUT HERE

BombMan.html

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
Bomb Man is trapped inside a maze shaped like a grid.
3+
You must help him find the exit as fast as possible.
4+
The maze will be given as a vector &lt;string&gt; where each element
5+
corresponds to a row in the grid and each character in an element
6+
corresponds to a cell in that row. '#' marks a wall,
7+
'.' an empty cell, 'B' the start position of Bomb Man
8+
and 'E' the exit. Below is an example of a maze in this format,
9+
given as a vector &lt;string&gt;:
10+
</p>
11+
12+
<pre>
13+
{&quot;.....B.&quot;,
14+
&quot;.#####.&quot;,
15+
&quot;.#...#.&quot;,
16+
&quot;.#E#.#.&quot;,
17+
&quot;.###.#.&quot;,
18+
&quot;.......&quot;}
19+
</pre>
20+
21+
<p>
22+
In each time unit, Bomb Man can move one cell up, down, left or right.
23+
Thus, in the maze above, he can reach the exit in 14 time units by just walking.
24+
</p>
25+
26+
<p>
27+
To be able to reach the exit quicker, Bomb Man is in possession
28+
of a number of bombs, each of which can blow a hole in a wall and thus open
29+
up new passages. Instead of moving in any of the four cardinal directions,
30+
Bomb Man can (if he has bombs left) blow a hole in a wall located in any of
31+
the four neighboring squares. The bomb will go off in the time unit after he has placed the bomb, creating
32+
an empty cell that Bomb Man can enter in the time unit after that. That is,
33+
if he places a bomb in time unit <i>t</i>, he can enter the cell earliest in time unit <i>t+2</i>.
34+
In the maze above, Bomb Man can then reach the exit in 8 time units by first walking
35+
left, placing a bomb, waiting for the bomb to explode, and then walking down, down,
36+
left, left and down.
37+
</p>
38+
39+
<p>
40+
Create a class BombMan containing the method shortestPath which
41+
takes a vector &lt;string&gt; <b>maze</b>, containing the maze in the
42+
format described above, and an int <b>bombs</b>, the number
43+
of bombs in Bomb Man's possession. The method should return an int, the
44+
least number of time units required for Bomb Man to reach the exit.
45+
If it's not possible for Bomb Man to reach the exit, return -1 (see example 1).
46+
</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>BombMan</td></tr><tr><td>Method:</td><td>shortestPath</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int shortestPath(vector &lt;string&gt; maze, int bombs)</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>maze</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element in <b>maze</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element in <b>maze</b> will contain the same number of characters.</td></tr><tr><td align="center" valign="top">-</td><td><b>maze</b> will only contain the characters '#', '.', 'B' and 'E'.</td></tr><tr><td align="center" valign="top">-</td><td>Exactly one character in <b>maze</b> will be a 'B'.</td></tr><tr><td align="center" valign="top">-</td><td>Exactly one character in <b>maze</b> will be a 'E'.</td></tr><tr><td align="center" valign="top">-</td><td><b>bombs</b> will be between 0 and 100, 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;.....B.&quot;,
47+
&quot;.#####.&quot;,
48+
&quot;.#...#.&quot;,
49+
&quot;.#E#.#.&quot;,
50+
&quot;.###.#.&quot;,
51+
&quot;.......&quot;}
52+
</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 8</pre></td></tr><tr><td><table><tr><td colspan="2">This is the example mentioned in the problem statement.</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;B.#.#.#...E&quot;}</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2">Bomb Man can only blow through the first two walls, but not the third. Hence the method returns -1.</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;,
53+
&quot;B.#####&quot;,
54+
&quot;..#####&quot;,
55+
&quot;#######&quot;,
56+
&quot;####...&quot;,
57+
&quot;####.E.&quot;}
58+
</pre></td></tr><tr><td><pre>4</pre></td></tr></table></td></tr><tr><td><pre>Returns: 17</pre></td></tr><tr><td><table><tr><td colspan="2">Here Bomb Man has just enough bombs to reach the exit. One way to do this is to walk
59+
down, right, down (bomb), down (bomb), right (bomb), right (bomb), right, right, down.
60+
This takes 1+1+3+3+3+3+1+1+1 = 17 time units.</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;.#.#.#.#B#...#.#...#.#...#.#...#.#...#.#.#.......&quot;,
61+
&quot;.#.#.#.#.#.###.###.#.###.#.#.###.###.#.#.#.###.##&quot;,
62+
&quot;.#.#.#...#.#.#.#.#.#...#.....#.#.#...#...#.#.#...&quot;,
63+
&quot;.#.#.###.#.#.#.#.#.###.#.#####.#.###.###.#.#.###.&quot;,
64+
&quot;.............#.#...#...#.....#.#.#...#.#.#.....#.&quot;,
65+
&quot;##.#######.###.#.#####.#.#####.#.###.#.#.#.#.####&quot;,
66+
&quot;.#.#.....#...#...#.#...#...#.#.#...#...#...#.....&quot;,
67+
&quot;.#######.#.#####.#.#.#.#.###.#.###.#.#####.#.####&quot;,
68+
&quot;.#.#.#.#...#.#.#.#.#.#.......#...#.#...#.#.#.....&quot;,
69+
&quot;.#.#.#.###.#.#.#.#.#####.#####.###.###.#.#.######&quot;,
70+
&quot;.....#...#.#...#...#...#...#...#...#.#.#.........&quot;,
71+
&quot;####.###.#.###.###.#.###.#.#.###.###.#.#.########&quot;,
72+
&quot;.......#.........#.#.#.#.#.#.#.#.........#...#...&quot;,
73+
&quot;.#.###.#########.#.#.#.#.###.#.#####.#.#.#.###.##&quot;,
74+
&quot;.#.#.........#.#.#.#.#.....#.#.#.....#.#.........&quot;,
75+
&quot;############.#.#.#.#.#.#####.#.#.################&quot;,
76+
&quot;.#...........#...#.#.#.#...#.#.#...#.#.#.....#...&quot;,
77+
&quot;.#####.#####.###.#.#.#.#.###.#.#.###.#.#.#####.##&quot;,
78+
&quot;.......#...#.#.#.....#...#...#.#.#.#.#...........&quot;,
79+
&quot;##########.#.#.#####.#.###.###.#.#.#.#.##########&quot;,
80+
&quot;.....#...#.....#.#...#.......#.#...#.......#.....&quot;,
81+
&quot;##.#.###.#.###.#.#.#.#.#####.#.#.###.#######.####&quot;,
82+
&quot;...#...#...#.......#.....#.#...#...#.......#.....&quot;,
83+
&quot;####.#.#.#########.#.###.#.#####.###.#.#######.##&quot;,
84+
&quot;.#...#...#.........#.#.....#.........#.#.#.#.....&quot;,
85+
&quot;.#####.#.#.###.#######.#.###.#.#########.#.#.####&quot;,
86+
&quot;.......#.#.#...#.......#.....#.#.#.......#.#.#.#.&quot;,
87+
&quot;########.#.#.#.#####.#.###.#.###.#.#######.#.#.#.&quot;,
88+
&quot;.........#.#.#.#.....#...#.#.........#.#.........&quot;,
89+
&quot;################.#.#.#.#.#.#.#.#######.#.########&quot;,
90+
&quot;.................#.#.#.#.#.#.#...........#.......&quot;,
91+
&quot;########.#####.#.###.#.#.#####.###.#.#####.###.##&quot;,
92+
&quot;.........#...#.#...#.#.#...#.....#.#.........#...&quot;,
93+
&quot;.#####.#####.#.###.#.###.#.#.#.#.#.#####.#.###.#.&quot;,
94+
&quot;.#.....#.........#.#.#...#.#.#.#.#.#.....#...#.#.&quot;,
95+
&quot;####.#####.###.#.#.#.#.#.#.###.###.#.#.#.#.#####.&quot;,
96+
&quot;.....#.....#.#.#.#.#.#.#.#.#...#...#.#.#.#...#...&quot;,
97+
&quot;####.#.#.###.#.#.###.#.###.#.#.#####.#.#.#.######&quot;,
98+
&quot;.....#.#.#.#...#...#.#...#.#.#...#...#.#.#.......&quot;,
99+
&quot;##########.#.#.#.#####.###.#.#.###.#.###.#####.##&quot;,
100+
&quot;...#.#...#...#.#.....#.#...#.#...#.#.#.......#...&quot;,
101+
&quot;.#.#.#.#.#.#.#.#.#.#.###.#.#########.###.#.#.#.#.&quot;,
102+
&quot;.#.#...#...#.#.#.#.#...#.#...#.......#...#.#.#.#.&quot;,
103+
&quot;##.###.#.#.###.#.#.#.#.#####.#.#.#.###.#.########&quot;,
104+
&quot;.......#.#...#.#.#.#.#.#.....#.#.#...#.#.........&quot;,
105+
&quot;####.#######.#.#####.#.###.#.#.###.#.#.#.########&quot;,
106+
&quot;E..#.......#.#.....#.#.#.#.#.#.#...#.#.#.........&quot;,
107+
&quot;##.#.#.#.###.###.###.###.#.#.###.#.#.#.#.#######.&quot;,
108+
&quot;.....#.#...#.#.....#.#.....#...#.#.#.#.#.....#...&quot;}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 76</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)