Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

861 scaffolding

  • Loading branch information...
commit 1101fee78aff1bb4cc8f65bc48713d06a4bc3ee6 1 parent 884a070
kharus authored
113 861/861.cc
View
@@ -0,0 +1,113 @@
+#include<cstdlib>
+#include<cmath>
+#include<set>
+#include<string>
+#include<iostream>
+#include<sstream>
+#include<algorithm>
+#include<complex>
+
+using namespace std;
+
+typedef complex<int> square;
+
+struct classcomp {
+ bool operator() (const square& lhs, const square& rhs) const{
+ if (lhs.real() == rhs.real())
+ return lhs.imag()<rhs.imag();
+ else
+ return lhs.real()<rhs.real();
+ }
+};
+
+typedef set<square, classcomp> square_set;
+
+bool is_under_attack(vector<int> 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<bool> results;
+
+ cin>>input;
+ while ( input != 0 ){
+ results.push_back(
+ check_bulb(input));
+ cin>>input;
+ }
+
+ for (int i=0; i<results.size(); i++){
+ if (results[i])
+ cout<<"yes"<<endl;
+ else
+ cout<<"no"<<endl;
+ }
+}
+#endif
79 861/861_unittest.cc
View
@@ -0,0 +1,79 @@
+#include <limits.h>
+#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));
+}
77 861/Makefile
View
@@ -0,0 +1,77 @@
+# A sample Makefile for building Google Test and using it in user
+# tests. Please tweak it to suit your environment and project. You
+# may want to move it to your project's root directory.
+#
+# SYNOPSIS:
+#
+# make [all] - makes everything.
+# make TARGET - makes the given target.
+# make clean - removes all files generated by make.
+
+# Please tweak the following variable definitions as needed by your
+# project, except GTEST_HEADERS, which you can use in your own targets
+# but shouldn't modify.
+
+# Points to the root of Google Test, relative to where this file is.
+# Remember to tweak this if you move this file.
+GTEST_DIR = /home/ruslan/lib/gtest-1.6.0
+
+# Where to find user code.
+USER_DIR = .
+
+# Flags passed to the preprocessor.
+CPPFLAGS += -I$(GTEST_DIR)/include
+
+# Flags passed to the C++ compiler.
+CXXFLAGS += -g -Wall -Wextra -std=c++0x
+
+# All tests produced by this Makefile. Remember to add new tests you
+# created to the list.
+TESTS = 861_unittest
+
+# All Google Test headers. Usually you shouldn't change this
+# definition.
+GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
+ $(GTEST_DIR)/include/gtest/internal/*.h
+
+# House-keeping build targets.
+
+all : $(TESTS)
+
+clean :
+ rm -f $(TESTS) gtest.a gtest_main.a *.o
+
+# Builds gtest.a and gtest_main.a.
+
+# Usually you shouldn't tweak such internal variables, indicated by a
+# trailing _.
+GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
+
+# For simplicity and to avoid depending on Google Test's
+# implementation details, the dependencies specified below are
+# conservative and not optimized. This is fine as Google Test
+# compiles fast and for ordinary users its source rarely changes.
+gtest-all.o : $(GTEST_SRCS_)
+ $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
+ $(GTEST_DIR)/src/gtest-all.cc
+
+gtest_main.o : $(GTEST_SRCS_)
+ $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
+ $(GTEST_DIR)/src/gtest_main.cc
+
+gtest.a : gtest-all.o
+ $(AR) $(ARFLAGS) $@ $^
+
+gtest_main.a : gtest-all.o gtest_main.o
+ $(AR) $(ARFLAGS) $@ $^
+
+# Builds a sample test. A test should link with either gtest.a or
+# gtest_main.a, depending on whether it defines its own main()
+# function.
+
+861_unittest.o : $(USER_DIR)/861_unittest.cc \
+ $(USER_DIR)/861.cc $(GTEST_HEADERS)
+ $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/861_unittest.cc
+
+861_unittest : 861_unittest.o gtest_main.a
+ $(CXX) $(CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $@
47 861/permutations.cc
View
@@ -0,0 +1,47 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+using namespace std;
+
+bool finished = false;
+
+bool is_a_solution(vector<int> a, int input){
+ return (a.size()==input);
+}
+
+vector<int> construct_candidates(vector<int> a, int input){
+ vector<int> result;
+ for (int i=1; i<= input; i++)
+ if ( find(a.begin(), a.end(), i) == a.end())
+ result.push_back(i);
+ return result;
+}
+
+void process_solution(vector<int> a){
+ cout<<"{";
+ for (int i = 0; i <a.size(); i++)
+ cout<<" "<<a[i];
+ cout<<" "<<"}"<<endl;
+}
+
+void backtrack(vector<int> a, int input){
+ if (is_a_solution(a,input))
+ process_solution(a);
+ else {
+ vector<int> candidates = construct_candidates(a,input);
+ for (int i=0; i<candidates.size(); i++) {
+ a.push_back(candidates[i]);
+ backtrack(a,input);
+ a.pop_back();
+ if (finished) return; /* terminate early */
+ }
+ }
+}
+
+int main(){
+ vector<int> a;
+
+ backtrack(a,3);
+}
+
46 861/subsets.cc
View
@@ -0,0 +1,46 @@
+#include<iostream>
+#include<vector>
+
+using namespace std;
+
+bool finished = false;
+
+bool is_a_solution(vector<int> a, int input){
+ return (a.size()==input);
+}
+
+vector<int> construct_candidates(vector<int> a, int input){
+ vector<int> result;
+ result.push_back(true);
+ result.push_back(false);
+ return result;
+}
+
+void process_solution(vector<int> a){
+ cout<<"{";
+ for (int i = 0; i <a.size(); i++)
+ if (a[i])
+ cout<<" "<<i+1;
+ cout<<" "<<"}"<<endl;
+}
+
+void backtrack(vector<int> a, int input){
+ if (is_a_solution(a,input))
+ process_solution(a);
+ else {
+ vector<int> candidates = construct_candidates(a,input);
+ for (int i=0; i<candidates.size(); i++) {
+ a.push_back(candidates[i]);
+ backtrack(a,input);
+ a.pop_back();
+ if (finished) return; /* terminate early */
+ }
+ }
+}
+
+int main(){
+ vector<int> a;
+
+ backtrack(a,3);
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.