#include #include #include using namespace std; struct Point { int row; int col; }; ostream& operator << (ostream& os, const Point& p) { os << "[" << p.row << "," << p.col << "]"; return os; } /** Smallest and fastest 2d array implementation. No checks. Nothing ;) */ template class Array2 { public: Array2(unsigned rows,unsigned cols) : rows_(rows), cols_(cols), data_(new T[rows*cols]) { } T& at(unsigned row, unsigned col) { return data_[row*cols_ + col]; } template T& at(const P& p) { return at(p.row, p.col); } const T& at(unsigned row, unsigned col) const { return data_[row*cols_ + col]; } void fill(const T& value) { for( unsigned i=0; i ostream& operator <<(ostream& os,const Array2& arr) { for( unsigned i=0; i& board() const { return board_; } private: bool step(const Point& p) { board_.at(p) = ++depth_; if( depth_ >= goal_ ) return true; for( Point m: moves(p) ) { if( step(m) ) return true; } board_.at(p) = 0; depth_--; return false; } vector moves(const Point& pos) { vector mm; for( unsigned i=0; i= 0 && r < rank_ ) { int c = pos.col + m.col; if( c >= 0 && c < rank_ && board_.at(r,c) == 0 ) { mm.push_back({r,c}); } } } return mm; } unsigned rank_, depth_, goal_; Array2 board_; }; template void timing(const S& name,int repetitions, const std::function& block) { clock_t start = clock(); while( repetitions-- > 0) { block(); } cout << name << ": " << ((clock() - start)/((double)CLOCKS_PER_SEC)) << endl; } int main(int argc, char** argv) { cout << "starting\n"; timing( "C++", 5, [] { KM km(7); km.solve(); // cout << km.board() << endl; }); }