Skip to content

Commit 33719e6

Browse files
committed
srm 234 div 1 500
1 parent 8e6f322 commit 33719e6

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

WeirdRooks.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <sstream>
4+
#include <cstring>
5+
#include <vector>
6+
#include <list>
7+
#include <algorithm>
8+
#include <set>
9+
#include <map>
10+
using namespace std;
11+
int sol[10][100];
12+
vector<int> m;
13+
int col[10];
14+
15+
class WeirdRooks {
16+
public:
17+
void fun(int r, int nr, int ns){
18+
19+
if(r < 0){
20+
sol[nr][ns] = 1;
21+
return;
22+
}
23+
24+
int ts = 0;
25+
for(int j = m[r]-1; j >= 0; j--){
26+
if(!col[j]){
27+
col[j] = 1;
28+
fun(r-1, nr+1, ns+ts);
29+
col[j] = 0;
30+
ts++;
31+
}
32+
}
33+
fun(r-1, nr, ns+ts);
34+
}
35+
36+
string describe(vector <int> cols)
37+
{
38+
memset(sol, 0, 1000*sizeof(int));
39+
m = cols;
40+
string ret = "";
41+
fun(cols.size()-1, 0, 0);
42+
for(int i = 0; i < 10; i++){
43+
for(int j = 0; j < 100; j++)
44+
if(sol[i][j]){
45+
char s[10];
46+
sprintf(s, "%d,%d", i, j);
47+
if(ret.size()) ret += ' ';
48+
ret += s;
49+
}
50+
}
51+
52+
return ret;
53+
}
54+
55+
// BEGIN CUT HERE
56+
public:
57+
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(); }
58+
private:
59+
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(); }
60+
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; } }
61+
void test_case_0() { int Arr0[] = {3,3,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "0,9 1,4 1,5 1,6 1,7 1,8 2,1 2,2 2,3 2,4 2,5 2,6 3,0 3,1 3,2 3,3"; verify_case(0, Arg1, describe(Arg0)); }
62+
void test_case_1() { int Arr0[] = {1,2,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "0,6 1,3 1,4 1,5 2,1 2,2 2,3 3,0"; verify_case(1, Arg1, describe(Arg0)); }
63+
void test_case_2() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "0,1 1,0"; verify_case(2, Arg1, describe(Arg0)); }
64+
void test_case_3() { int Arr0[] = {2,9}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "0,11 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 1,10 2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8"; verify_case(3, Arg1, describe(Arg0)); }
65+
66+
// END CUT HERE
67+
68+
};
69+
70+
// BEGIN CUT HERE
71+
int main()
72+
{
73+
WeirdRooks ___test;
74+
___test.run_test(-1);
75+
}
76+
// END CUT HERE

WeirdRooks.txt

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
PROBLEM STATEMENT
2+
On a weird chess board, each row can have a different number of columns. Element k of cols will give the number of columns in row k. Each row is flush left, so the right side can look quite ragged. In a valid assignment of n rooks to the weird chess board, no two rooks can share a row or column. In such an assignment, an unoccupied square is considered special if there is no rook to its right in the same row and no rook below in the same column (element 0 of cols describes the highest row). You are going to return a string containing a single-space delimited list of pairs. The pair (quotes for clarity) "r,f" should appear in the final string if and only if there is a valid assignment with r rooks such that f squares are special. The pairs should be sorted in nondecreasing order by r values. If a tie occurs, the lower f value should come first. The returned value should contain no repeated pairs. See the examples for further clarifications.
3+
4+
DEFINITION
5+
Class:WeirdRooks
6+
Method:describe
7+
Parameters:vector <int>
8+
Returns:string
9+
Method signature:string describe(vector <int> cols)
10+
11+
12+
CONSTRAINTS
13+
-cols will contain between 1 and 8 elements inclusive.
14+
-Each element of cols will be between between 1 and 10 inclusive.
15+
-cols will be sorted in non-descending order.
16+
17+
18+
EXAMPLES
19+
20+
0)
21+
{3,3,3}
22+
23+
Returns: "0,9 1,4 1,5 1,6 1,7 1,8 2,1 2,2 2,3 2,4 2,5 2,6 3,0 3,1 3,2 3,3"
24+
25+
If no rooks are placed on the board, all 9 squares are special. The following diagram depicts the scenario where 3 rooks are placed, and no squares are special.
26+
R..
27+
.R.
28+
..R
29+
30+
1)
31+
{1,2,3}
32+
33+
Returns: "0,6 1,3 1,4 1,5 2,1 2,2 2,3 3,0"
34+
35+
The case with 2 rooks and 3 special squares is depicted below.
36+
R
37+
.R
38+
...
39+
40+
2)
41+
{1}
42+
43+
Returns: "0,1 1,0"
44+
45+
3)
46+
{2,9}
47+
48+
Returns: "0,11 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 1,10 2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8"

0 commit comments

Comments
 (0)