Skip to content

Commit ebca65a

Browse files
committed
srm 655 div 2 250
1 parent 0858fb3 commit ebca65a

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

Diff for: BichromeBoard.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 make_pair
20+
#define pb push_back
21+
#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b)
22+
#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b]
23+
#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b)
24+
#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b]
25+
#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b)
26+
#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b]
27+
#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b)
28+
#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b]
29+
#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b)
30+
#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b]
31+
#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b)
32+
#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b]
33+
#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b]
34+
35+
class BichromeBoard{
36+
37+
public:
38+
string ableToDraw(vector <string> board){
39+
int n = board.size(), m = board[0].size();
40+
vs a = board, b = board;
41+
fi(n) fj(m) {
42+
if(i%2){
43+
if(j%2){
44+
a[i][j] = 'W';
45+
b[i][j] = 'B';
46+
}
47+
else{
48+
a[i][j] = 'B';
49+
b[i][j] = 'W';
50+
}
51+
}
52+
else{
53+
if(j%2){
54+
a[i][j] = 'B';
55+
b[i][j] = 'W';
56+
}
57+
else{
58+
a[i][j] = 'W';
59+
b[i][j] = 'B';
60+
}
61+
}
62+
}
63+
bool ret1 = true, ret2 = true;
64+
fi(n) fj(m){
65+
if(board[i][j] != '?' && board[i][j] != a[i][j]) ret1 = false;
66+
if(board[i][j] != '?' && board[i][j] != b[i][j]) ret2 = false;
67+
}
68+
if(ret1 || ret2) return "Possible";
69+
return "Impossible";
70+
}
71+
72+
// BEGIN CUT HERE
73+
public:
74+
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(); }
75+
private:
76+
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(); }
77+
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; } }
78+
void test_case_0() { string Arr0[] = {"W?W",
79+
"??B",
80+
"???"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(0, Arg1, ableToDraw(Arg0)); }
81+
void test_case_1() { string Arr0[] = {"W??W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Impossible"; verify_case(1, Arg1, ableToDraw(Arg0)); }
82+
void test_case_2() { string Arr0[] = {"??"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(2, Arg1, ableToDraw(Arg0)); }
83+
void test_case_3() { string Arr0[] = {"W???",
84+
"??B?",
85+
"W???",
86+
"???W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(3, Arg1, ableToDraw(Arg0)); }
87+
void test_case_4() { string Arr0[] = {"W???",
88+
"??B?",
89+
"W???",
90+
"?B?W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Impossible"; verify_case(4, Arg1, ableToDraw(Arg0)); }
91+
void test_case_5() { string Arr0[] = {"B"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(5, Arg1, ableToDraw(Arg0)); }
92+
93+
// END CUT HERE
94+
95+
};
96+
97+
// BEGIN CUT HERE
98+
int main()
99+
{
100+
BichromeBoard ___test;
101+
___test.run_test(-1);
102+
}
103+
// END CUT HERE

Diff for: BichromeBoard.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>We have a rectangular board divided into a grid of unit squares.
2+
We are going to color each square either white or black.
3+
<br></br>
4+
<br></br>
5+
You are given the vector &lt;string&gt; <b>board</b>.
6+
Each character in <b>board</b> represents one unit square.
7+
If <b>board</b>[i][j] is 'B', the corresponding square must be black.
8+
If <b>board</b>[i][j] is 'W', the corresponding square must be white.
9+
Finally, if <b>board</b>[i][j] is '?', you get to choose the color for this square: either white or black.
10+
<br></br>
11+
<br></br>
12+
Two squares are adjacent if they share a common side.
13+
We want to color the board in such a way that no two adjacent squares share the same color.
14+
Return "Possible" (quotes for clarity) if it can be done, or "Impossible" otherwise.</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>BichromeBoard</td></tr><tr><td>Method:</td><td>ableToDraw</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string ableToDraw(vector &lt;string&gt; board)</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>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 in <b>board</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element in <b>board</b> will contain the same number of characters.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in <b>board</b> will be one of 'W', 'B', '?'.</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;W?W&quot;,
15+
&quot;??B&quot;,
16+
&quot;???&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">The solution is:
17+
<pre>
18+
WBW
19+
BWB
20+
WBW
21+
</pre>
22+
</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;W??W&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Impossible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">The four possible colorings of this board are WWWW, WWBW, WBWW, and WBBW.<br></br>
23+
In each of them there is at least one pair of adjacent squares that share the same color.<br></br>
24+
Thus, there is no way to get a pattern with the desired property.<br></br></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;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">There are 2 ways:
25+
WB and BW</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;W???&quot;,
26+
&quot;??B?&quot;,
27+
&quot;W???&quot;,
28+
&quot;???W&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&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">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;W???&quot;,
29+
&quot;??B?&quot;,
30+
&quot;W???&quot;,
31+
&quot;?B?W&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Impossible&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">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;B&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&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)