kharus/phallenges

861 scaffolding

1 parent 884a070 commit 1101fee78aff1bb4cc8f65bc48713d06a4bc3ee6 committed May 20, 2012
Showing with 362 additions and 0 deletions.
1. +113 −0 861/861.cc
2. +79 −0 861/861_unittest.cc
3. +77 −0 861/Makefile
4. +47 −0 861/permutations.cc
5. +46 −0 861/subsets.cc
 @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef complex square; + +struct classcomp { + bool operator() (const square& lhs, const square& rhs) const{ + if (lhs.real() == rhs.real()) + return lhs.imag() square_set; + +bool is_under_attack(vector board_setup, const int position){ + return true; +} + +bool is_square_in_board(square point, int board_size){ + return + point.real() > 0 && + point.imag() > 0 && + point.real() <= board_size && + point.imag() <= board_size; +} + +int initial_d(square point, int a, int board_size){ + int b = point.imag()-a*point.real(); + int x0 = (a==1) ? 1-b : b-board_size; + + return point.real() - max(1,x0); +} + +square_set attacked_diagonal(square point, int direction, int board_size){ + square_set result; + + int initial = initial_d(point, direction, board_size); + square step(1, direction); + square attacked_square = point - step*initial; + for (int i=0; is_square_in_board(attacked_square + step*i, board_size); i++){ + result.insert(attacked_square + step*i); + } + return result; +} + +square_set attacked_squares(square point, int board_size){ + square_set result; + + square_set asc_diag = attacked_diagonal(point, 1, board_size); + result.insert(asc_diag.begin(), asc_diag.end()); + + square_set desc_diag = attacked_diagonal(point, -1, board_size); + result.insert(desc_diag.begin(), desc_diag.end()); + + return result; +} + +square_set clear_board(int shift, int board_size){ + square_set result; + for (int i = 1; i<= board_size; i++){ + for (int j = 1 + (shift ^ (i+1)%2); j<= board_size; j+=2){ + result.insert(square(i, j)); + } + } + return result; +} + +square_set unattacked_board(square_set bishops, int shift, int board_size){ + square_set result = clear_board(shift, board_size); + square_set::iterator bishop; + square_set::iterator attacked_square; + for(bishop = bishops.begin(); bishop != bishops.end(); bishop++){ + square_set attacked_squaresb = attacked_squares(*bishop, board_size); + for(attacked_square = attacked_squaresb.begin(); attacked_square != attacked_squaresb.end(); attacked_square++){ + result.erase(*attacked_square); + } + } + return result; +} + +square_set construct_candidates(square_set a, int input){ +} + +#ifdef ONLINE_JUDGE +int main(){ + unsigned long input; + vector results; + + cin>>input; + while ( input != 0 ){ + results.push_back( + check_bulb(input)); + cin>>input; + } + + for (int i=0; i
 @@ -0,0 +1,79 @@ +#include +#include "861.cc" +#include "gtest/gtest.h" + +TEST(Board, CheckSquares) { + EXPECT_TRUE(is_square_in_board(square(1,1),1)); + EXPECT_TRUE(is_square_in_board(square(3,4),8)); + EXPECT_FALSE(is_square_in_board(square(0,1),8)); + EXPECT_FALSE(is_square_in_board(square(1,0),8)); + EXPECT_FALSE(is_square_in_board(square(9,1),8)); + EXPECT_FALSE(is_square_in_board(square(1,9),8)); +} + +TEST(Board, Attack) { + square_set fixture = {square(1,1), square(2,2)}; + EXPECT_EQ(fixture, attacked_squares(square(2,2), 2)); +} + +TEST(Board, Attack3) { + square_set fixture = {square(1,2), square(3,2), square(2,1)}; + EXPECT_EQ(fixture, attacked_squares(square(2,1), 3)); +} + +TEST(Board, AttackAscFull) { + square_set fixture = {square(1,1), square(2,2), square(3,3)}; + EXPECT_EQ(fixture, attacked_diagonal(square(1,1),1, 3)); + EXPECT_EQ(fixture, attacked_diagonal(square(2,2),1, 3)); + EXPECT_EQ(fixture, attacked_diagonal(square(3,3),1, 3)); +} + +TEST(Board, AttackDescFull) { + square_set fixture = {square(1,3), square(2,2), square(3,1)}; + EXPECT_EQ(fixture, attacked_diagonal(square(1,3), -1, 3)); + EXPECT_EQ(fixture, attacked_diagonal(square(2,2), -1, 3)); + EXPECT_EQ(fixture, attacked_diagonal(square(3,1), -1, 3)); +} + +TEST(Board, AttackAscOne) { + EXPECT_EQ(square_set({square(1,3)}), attacked_diagonal(square(1,3),1, 3)); + EXPECT_EQ(square_set({square(3,1)}), attacked_diagonal(square(3,1),1, 3)); +} + +TEST(Board, AttackDescOne) { + EXPECT_EQ(square_set({square(1,1)}), attacked_diagonal(square(1,1), -1, 3)); + EXPECT_EQ(square_set({square(3,3)}), attacked_diagonal(square(3,3), -1, 3)); +} + +TEST(Board, InitialZero) { + EXPECT_EQ(0, initial_d(square(1,1),1, 3)); + EXPECT_EQ(0, initial_d(square(1,1),-1, 3)); + EXPECT_EQ(0, initial_d(square(3,1),1, 3)); + EXPECT_EQ(0, initial_d(square(1,3),-1, 3)); +} + +TEST(Board, Initial2) { + EXPECT_EQ(2, initial_d(square(3,3), 1, 3)); + EXPECT_EQ(2, initial_d(square(3,1), -1, 3)); +} + +TEST(Board, Initial1) { + EXPECT_EQ(1, initial_d(square(3,2), 1, 3)); + EXPECT_EQ(1, initial_d(square(3,2), -1, 3)); +} + +TEST(Board, ClearBoard) { + EXPECT_EQ(square_set({square(1,1), square(2,2)}), clear_board(0,2)); + EXPECT_EQ(square_set({square(1,2), square(2,1)}), clear_board(1,2)); + EXPECT_EQ(square_set({square(1,1), square(1,3), square(2,2), square(3,1), square(3,3)}), clear_board(0,3)); + EXPECT_EQ(square_set({square(1,2), square(2,1), square(2,3), square(3,2)}), clear_board(1,3)); +} + +TEST(Board, UnattackedBoard) { + EXPECT_EQ(square_set({square(1,1), square(2,2)}), unattacked_board(square_set(), 0, 2)); + EXPECT_EQ(square_set({square(1,3), square(3,1)}), + unattacked_board(square_set({square(1,1)}), 0, 3)); + EXPECT_EQ(square_set(), + unattacked_board(square_set({square(2,2)}), 0, 3)); + EXPECT_EQ(square_set({square(2,4)}), unattacked_board(square_set({square(2,2), square(3,1)}), 0, 4)); +}