-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path10651.cpp
More file actions
69 lines (59 loc) · 1.99 KB
/
10651.cpp
File metadata and controls
69 lines (59 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <cstring>
#define n 12
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define SET_BIT(var, pos) (var |= (1<<(pos)))
using namespace std;
bool can_move_left(int pos, int board) {
if (pos < 2) return false;
return CHECK_BIT(board, pos) and CHECK_BIT(board, pos-1)
and !CHECK_BIT(board, pos-2);
}
bool can_move_right(int pos, int board) {
if (pos > n-3) return false;
return CHECK_BIT(board, pos) and CHECK_BIT(board, pos+1)
and !CHECK_BIT(board, pos+2);
}
int move_right(int pos, int board) {
board &= ~(1 << pos); // Clear the pos marble
board &= ~(1 << (pos+1)); // Clear the removed marble
board |= 1 << (pos+2); // Set the new position of the marble
return board;
}
int move_left(int pos, int board) {
board &= ~(1 << pos); // Clear the pos marble
board &= ~(1 << (pos-1)); // Clear the removed marble
board |= 1 << (pos-2); // Set the new position of the marble
return board;
}
/**
* The idea behind this problem is to do a complete search on the
* solution space. Meaning, for every pebble in the input, we check if it can be moved.
* If so, we find the number of moves in the new board configuration (after the move
* was made). I added a memo array so we dont need to re-compute values, but that's
* not needed for a passing solution.
*/
int tc, board, peb, dp[1<<n];
int solve(int board) {
// for each board position, try to move right and try to move left
// return the min of all moves
if (dp[board] != -1) return dp[board];
int m = 0;
for (int i = 0; i < n; i++)
if (can_move_right(i, board)) m = max(m, 1 + solve(move_right(i, board)));
else if (can_move_left(i, board)) m = max(m, 1 + solve(move_left(i, board)));
return dp[board] = m;
}
int main() {
cin >> tc;
while (tc--) {
string in; cin >> in;
peb = board = 0;
memset(dp, -1, sizeof dp);
for (int i = 0; i < in.size(); i++)
if (in[i] == 'o') board |= 1 << i, peb++;
cout << peb - solve(board) << endl;
}
return 0;
}