Skip to content

Commit a413f5f

Browse files
committed
srm 576 div 1 250
1 parent cf25202 commit a413f5f

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

Diff for: ArcadeManao.cpp

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
#define sz size()
19+
#define mp(x, y) make_pair(x, y)
20+
#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b)
21+
#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b]
22+
#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b)
23+
#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b]
24+
#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b)
25+
#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b]
26+
#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b)
27+
#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b]
28+
#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b)
29+
#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b]
30+
#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b)
31+
#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b]
32+
#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b]
33+
vs M;
34+
int visited[55][55];
35+
int n, m;
36+
#define valid(x, y) ( (x) >= 0 && (x) < n && (y) >= 0 && (y) < m && !visited[(x)][(y)] && M[(x)][(y)] == 'X')
37+
38+
39+
class ArcadeManao{
40+
public:
41+
int shortestLadder(vector <string> level, int coinRow, int coinColumn){
42+
M = level;
43+
n = M.size();
44+
m = M[0].size();
45+
vi move(2);
46+
move[0] = -1; move[1] = 1;
47+
48+
for(int l = 0; l < n; l++){
49+
// cout<<"level = "<<l<<endl;
50+
fi(n) fj(m) visited[i][j] = 0;
51+
queue<pi> Q;
52+
Q.push(mp(n-1, m-1));
53+
visited[n-1][m-1] = 1;
54+
while(!Q.empty()){
55+
pi p = Q.front();
56+
Q.pop();
57+
int x = p.first, y = p.second;
58+
// cout<<x<<" "<<y<<endl;
59+
if(x+1 == coinRow && y+1 == coinColumn) return l;
60+
//Move horizontally
61+
for(int k : move){
62+
if(valid(x, y+k)){
63+
Q.push(mp(x, y+k));
64+
visited[x][y+k] = 1;
65+
}
66+
}
67+
//Move vertically
68+
for(int i = 1; i <= l; i++){
69+
for(int k : move){
70+
if(valid(x+k*i, y)){
71+
visited[x+k*i][y] = 1;
72+
Q.push(mp(x+k*i, y));
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
80+
81+
// BEGIN CUT HERE
82+
public:
83+
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(); }
84+
private:
85+
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(); }
86+
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; } }
87+
void test_case_0() { string Arr0[] = {"XXXX....",
88+
"...X.XXX",
89+
"XXX..X..",
90+
"......X.",
91+
"XXXXXXXX"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 4; int Arg3 = 2; verify_case(0, Arg3, shortestLadder(Arg0, Arg1, Arg2)); }
92+
void test_case_1() { string Arr0[] = {"XXXX",
93+
"...X",
94+
"XXXX"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 1; int Arg3 = 1; verify_case(1, Arg3, shortestLadder(Arg0, Arg1, Arg2)); }
95+
void test_case_2() { string Arr0[] = {"..X..",
96+
".X.X.",
97+
"X...X",
98+
".X.X.",
99+
"..X..",
100+
"XXXXX"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 3; int Arg3 = 4; verify_case(2, Arg3, shortestLadder(Arg0, Arg1, Arg2)); }
101+
void test_case_3() { string Arr0[] = {"X"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 1; int Arg3 = 0; verify_case(3, Arg3, shortestLadder(Arg0, Arg1, Arg2)); }
102+
void test_case_4() { string Arr0[] = {"XXXXXXXXXX",
103+
"...X......",
104+
"XXX.......",
105+
"X.....XXXX",
106+
"..XXXXX..X",
107+
".........X",
108+
".........X",
109+
"XXXXXXXXXX"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 1; int Arg3 = 2; verify_case(4, Arg3, shortestLadder(Arg0, Arg1, Arg2)); }
110+
111+
// END CUT HERE
112+
113+
};
114+
115+
// BEGIN CUT HERE
116+
int main(){
117+
118+
ArcadeManao ___test;
119+
___test.run_test(-1);
120+
}
121+
// END CUT HERE

Diff for: ArcadeManao.html

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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>You might remember the old computer arcade games. Here is one about Manao.
2+
<br></br><br></br>
3+
The game level is an NxM grid of equal cells. The bottom of some cells has a platform at which Manao can stand. All the cells in the bottommost row contain a platform, thus covering the whole ground of the level. The rows of the grid are numbered from 1 to N starting from the top and the columns are numbered from 1 to M starting from the left. Exactly one cell contains a coin and Manao needs to obtain it.
4+
<br></br><br></br>
5+
Initially, Manao is standing on the ground, i.e., in the bottommost row. He can move between two horizontally adjacent cells if both contain a platform. Also, Manao has a ladder which he can use to climb. He can use the ladder to climb both up and down. If the ladder is L units long, Manao can climb between two cells (i1, j) and (i2, j) if both contain a platform and |i1-i2| &lt;= L. Note that Manao carries the ladder along, so he can use it multiple times. You need to determine the minimum ladder length L which is sufficient to acquire the coin.
6+
<br></br><br></br>
7+
Take a look at the following picture. On this level, Manao will manage to get the coin with a ladder of length 2.
8+
<br></br>
9+
<img src="http://www.topcoder.com/contest/problem/ArcadeManao/pic.png"></img>
10+
<br></br><br></br>
11+
You are given a vector &lt;int&gt; <b>level</b> containing N elements. The j-th character in the i-th row of <b>level</b> is 'X' if cell (i+1, j+1) contains a platform and '.' otherwise. You are also given ints <b>coinRow</b> and <b>coinColumn</b>. The coin which Manao seeks is located in cell (<b>coinRow</b>, <b>coinColumn</b>) and it is guaranteed that this cell contains a platform.
12+
<br></br><br></br>
13+
Return the minimum L such that ladder of length L is enough to get the coin. If Manao can perform the task without using the ladder, return 0.</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>ArcadeManao</td></tr><tr><td>Method:</td><td>shortestLadder</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;, int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int shortestLadder(vector &lt;string&gt; level, int coinRow, int coinColumn)</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>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>Manao is not allowed to fall. The only way in which he may change his vertical coordinate is by using the ladder.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>level</b> will contain N elements, where N is between 1 and 50, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>level</b> will be M characters long, where M is between 1 and 50, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>level</b> will consist of '.' and 'X' characters only.</td></tr><tr><td align="center" valign="top">-</td><td>The last element of <b>level</b> will be entirely filled with 'X'.</td></tr><tr><td align="center" valign="top">-</td><td><b>coinRow</b> will be between 1 and N, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>coinColumn</b> will be between 1 and M, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>level</b>[<b>coinRow</b> - 1][<b>coinColumn</b> - 1] will be 'X'.</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;XXXX....&quot;,
14+
&quot;...X.XXX&quot;,
15+
&quot;XXX..X..&quot;,
16+
&quot;......X.&quot;,
17+
&quot;XXXXXXXX&quot;}</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>4</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">The example from 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;XXXX&quot;,
18+
&quot;...X&quot;,
19+
&quot;XXXX&quot;}</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">Manao can use the ladder to climb from the ground to cell (2, 4), then to cell (1, 4) and then he can walk right to the coin.</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;..X..&quot;,
20+
&quot;.X.X.&quot;,
21+
&quot;X...X&quot;,
22+
&quot;.X.X.&quot;,
23+
&quot;..X..&quot;,
24+
&quot;XXXXX&quot;}</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">With a ladder of length 4, Manao can first climb to cell (5, 3) and then right to (1, 3).</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;X&quot;}</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">Manao begins in the same cell as the coin.</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;XXXXXXXXXX&quot;,
25+
&quot;...X......&quot;,
26+
&quot;XXX.......&quot;,
27+
&quot;X.....XXXX&quot;,
28+
&quot;..XXXXX..X&quot;,
29+
&quot;.........X&quot;,
30+
&quot;.........X&quot;,
31+
&quot;XXXXXXXXXX&quot;}</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</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)