Skip to content

Commit 174c57d

Browse files
committedFeb 12, 2012
Chessboard problem
1 parent 8058739 commit 174c57d

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
 

‎chessboard/chessboard

9.38 KB
Binary file not shown.

‎chessboard/chessboard.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <cstdio>
2+
#include <cmath>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int w ( int* C, int mu ) {
8+
int i, profit = 0;
9+
for ( i = 0; i < 4; ++i ) {
10+
if ( (int)pow( (double)2, i ) & mu ) {
11+
profit += C[ 3 - i ];
12+
}
13+
}
14+
return profit;
15+
}
16+
17+
FILE *in = fopen( "chessboard.in", "r" );
18+
int W[ 2 ][ 8 ], C[ 4 ], parity = 1, method, i, omega, j, mu, profit, solution, N, M[] = { 0, 1, 2, 4, 5, 8, 9, 10 }; //Encoding column methods as binary numbers to be able to do bitwise operators.
19+
20+
int main() {
21+
fscanf( in, "%i", &N );
22+
for ( i = 0; i < N; ++i ) {
23+
for ( j = 0; j < 4; ++j ) {
24+
fscanf( in, "%i", &C[ j ] );
25+
}
26+
for ( omega = 0; omega < 8; ++omega ) {
27+
profit = 0;
28+
for ( mu = 0; mu < 8; ++mu ) {
29+
if ( !( M[ omega ] & M[ mu ] ) ) {
30+
profit = max( profit, W[ !parity ][ mu ] );
31+
}
32+
}
33+
profit += w( C, M[ omega ] );
34+
W[ parity ][ omega ] = profit;
35+
}
36+
parity = !parity;
37+
}
38+
for ( i = 0; i < 8; ++i ) {
39+
solution = max( solution, W[ !parity ][ i ] );
40+
}
41+
printf( "Solution: %i\n", solution );
42+
return 0;
43+
}

‎chessboard/chessboard.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
1000 1 1000 1
3+
1 1 1 1
4+
1 1000 1 1000

0 commit comments

Comments
 (0)
Please sign in to comment.