Skip to content

Commit ff2f380

Browse files
committed
srm 525 div 1 300
1 parent c43928f commit ff2f380

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

DropCoins.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
#include <queue>
9+
using namespace std;
10+
11+
class DropCoins {
12+
public:
13+
14+
int getMinimum(vector <string> b, int k)
15+
{
16+
int m = b.size();
17+
int n = b[0].size();
18+
vector<int> row, col, rr, cc;
19+
row.resize(m); rr.resize(m);
20+
col.resize(n); cc.resize(n);
21+
fill(row.begin(), row.end(), 0);
22+
fill(col.begin(), col.end(), 0);
23+
for(int i = 0; i < m; i++)
24+
for(int j = 0; j < n; j++){
25+
if(b[i][j] == 'o'){
26+
row[i]++;
27+
col[j]++;
28+
}
29+
}
30+
int ret = 1000000;
31+
for(int l = 0; l <= n; l++)
32+
for(int r = 0; l+r<=n; r++)
33+
for(int u = 0; u <= m; u++)
34+
for(int v = 0; u+v<=m; v++){
35+
int cnt = 0;
36+
vector<string> s = b;
37+
for(int i = 0; i < m; i++)
38+
for(int j = 0; j < n; j++)
39+
if((i < u || i >= m-v) || (j < l || j >= n-r)) s[i][j] = '.';
40+
41+
for(int i = 0; i < m; i++)
42+
for(int j = 0; j < n; j++)
43+
if(s[i][j] == 'o') cnt++;
44+
45+
if(cnt == k){
46+
ret = min(ret, l+r+min(l, r)+u+v+min(u, v));
47+
}
48+
}
49+
if(ret == 1000000) return -1;
50+
return ret;
51+
}
52+
53+
// BEGIN CUT HERE
54+
public:
55+
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(); }
56+
private:
57+
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(); }
58+
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; } }
59+
void test_case_0() { string Arr0[] = {".o.."
60+
,"oooo"
61+
,"..o."}
62+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 2; verify_case(0, Arg2, getMinimum(Arg0, Arg1)); }
63+
void test_case_1() { string Arr0[] = {".....o"
64+
,"......"
65+
,"oooooo"
66+
,"oooooo"
67+
,"......"
68+
,"o....."}
69+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; int Arg2 = 3; verify_case(1, Arg2, getMinimum(Arg0, Arg1)); }
70+
void test_case_2() { string Arr0[] = {"...."
71+
,".oo."
72+
,".oo."
73+
,"...."}
74+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = -1; verify_case(2, Arg2, getMinimum(Arg0, Arg1)); }
75+
void test_case_3() { string Arr0[] = {"......."
76+
,"..ooo.."
77+
,"ooooooo"
78+
,".oo.oo."
79+
,"oo...oo"}
80+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; int Arg2 = 4; verify_case(3, Arg2, getMinimum(Arg0, Arg1)); }
81+
void test_case_4() { string Arr0[] = {"................."
82+
,".ooooooo...oooo.."
83+
,".ooooooo..oooooo."
84+
,".oo.......oo..oo."
85+
,".oo.......oo..oo."
86+
,".ooooo.....oooo.."
87+
,".ooooooo...oooo.."
88+
,".....ooo..oo..oo."
89+
,"......oo..oo..oo."
90+
,".ooooooo..oooooo."
91+
,".oooooo....oooo.."
92+
,"................."}
93+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 58; int Arg2 = 6; verify_case(4, Arg2, getMinimum(Arg0, Arg1)); }
94+
95+
// END CUT HERE
96+
97+
};
98+
99+
// BEGIN CUT HERE
100+
int main()
101+
{
102+
DropCoins ___test;
103+
___test.run_test(-1);
104+
}
105+
// END CUT HERE

DropCoins.txt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
PROBLEM STATEMENT
2+
There is a rectangle divided into 1x1 cells. Each cell is either empty or it contains a single coin.
3+
4+
You can apply the following operation repeatedly.
5+
6+
First, choose one of the directions: up, down, left, or right.
7+
Then, move all coins in the chosen direction by exactly 1 cell. If this would cause a coin to move out of the rectangle, the coin drops out from the rectangle and disappears.
8+
9+
Your objective in this problem is to apply the operations so that the number of coins remaining on the rectangle becomes exactly K.
10+
11+
You are given the int K and a vector <string> board that describes the initial state of the rectangle. More precisely, character j of element i of board is 'o' if i-th row of j-th column of the rectangle contains a coin, and it is '.' otherwise.
12+
13+
Return the minimum number of operations you have to perform. If the objective is impossible, return -1.
14+
15+
DEFINITION
16+
Class:DropCoins
17+
Method:getMinimum
18+
Parameters:vector <string>, int
19+
Returns:int
20+
Method signature:int getMinimum(vector <string> board, int K)
21+
22+
23+
CONSTRAINTS
24+
-board will contain between 1 and 30 elements, inclusive.
25+
-Each element of board will contain between 1 and 30 characters, inclusive.
26+
-All elements of board will contain the same number of characters.
27+
-Each character in each element of board will be either '.' or 'o'.
28+
-K will be between 1 and 900, inclusive.
29+
30+
31+
EXAMPLES
32+
33+
0)
34+
{".o.."
35+
,"oooo"
36+
,"..o."}
37+
38+
3
39+
40+
Returns: 2
41+
42+
One of the optimal solutions is to move coins to the right twice.
43+
44+
1)
45+
{".....o"
46+
,"......"
47+
,"oooooo"
48+
,"oooooo"
49+
,"......"
50+
,"o....."}
51+
52+
12
53+
54+
Returns: 3
55+
56+
One of the optimal solutions:
57+
58+
move coins up (1 coin drops, 13 remain)
59+
move coins down
60+
move coins down again (1 coin drops, 12 remain)
61+
62+
63+
2)
64+
{"...."
65+
,".oo."
66+
,".oo."
67+
,"...."}
68+
69+
3
70+
71+
Returns: -1
72+
73+
It is impossible to make the number of remaining coins exactly 3.
74+
75+
3)
76+
{"......."
77+
,"..ooo.."
78+
,"ooooooo"
79+
,".oo.oo."
80+
,"oo...oo"}
81+
82+
12
83+
84+
Returns: 4
85+
86+
87+
88+
4)
89+
{"................."
90+
,".ooooooo...oooo.."
91+
,".ooooooo..oooooo."
92+
,".oo.......oo..oo."
93+
,".oo.......oo..oo."
94+
,".ooooo.....oooo.."
95+
,".ooooooo...oooo.."
96+
,".....ooo..oo..oo."
97+
,"......oo..oo..oo."
98+
,".ooooooo..oooooo."
99+
,".oooooo....oooo.."
100+
,"................."}
101+
102+
58
103+
104+
Returns: 6
105+
106+

0 commit comments

Comments
 (0)