#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class KnightsTour { public:
int visitedPositions(vector <string> board)
{
// sentinels
for(int yy=0; yy<board.size(); ++yy)
board[yy] = "**"+board[yy]+"**";
board.push_back( string(board[0].size(),'*') );
board.push_back( string(board[0].size(),'*') );
board.insert( board.begin(), string(board[0].size(),'*') );
board.insert( board.begin(), string(board[0].size(),'*') );
// starting point
int y, x;
for(int yy=0; yy<board.size(); ++yy)
for(int xx=0; xx<board[yy].size(); ++xx)
if(board[yy][xx]=='K')
y=yy, x=xx;
int n = 1;
while( step(board,y,x) )
++n;
return n;
}
bool step(vector<string>& B, int& Y, int& X)
{
int dy[]={-2,-2,-1,-1,+1,+1,+2,+2};
int dx[]={-1,+1,-2,+2,-2,+2,-1,+1};
int minAn = 9999;
for(int i=0; i<8; ++i)
if( B[Y+dy[i]][X+dx[i]] == '.' )
minAn = min(minAn, an(B,Y+dy[i],X+dx[i]));
if( minAn == 9999 )
return false;
for(int i=0; i<8; ++i)
if( B[Y+dy[i]][X+dx[i]] == '.' )
if( minAn == an(B,Y+dy[i],X+dx[i]) ) {
Y+=dy[i];
X+=dx[i];
B[Y][X] = 'K';
break;
}
return true;
}
int an(vector<string>& B, int Y, int X)
{
int dy[]={-2,-2,-1,-1,+1,+1,+2,+2};
int dx[]={-1,+1,-2,+2,-2,+2,-1,+1};
int cnt = 0;
for(int i=0; i<8; ++i)
cnt += (B[Y+dy[i]][X+dx[i]] == '.');
return cnt;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, KnightsTour().visitedPositions(board));}
int main(){
CASE(0)
string board_[] = {
"........"
,".*.*...."
,".*......"
,"..K...*."
,"*...*..."
,"...*...."
,"...*.*.."
,"........"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 39;
END
CASE(1)
string board_[] = {
"K......."
,"........"
,"........"
,"........"
,"........"
,"........"
,"........"
,"........"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 64;
END
CASE(2)
string board_[] = {
"********"
,"*******."
,"********"
,"**.***.*"
,"********"
,"***.*.**"
,"********"
,"****K***"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 3;
END
CASE(3)
string board_[] = {
"*.*....*"
,".......*"
,"**...*.."
,"..***..."
,".**.*..."
,"..*.*..K"
,"..***.*."
,"**...*.."};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 17;
END
CASE(4)
string board_[] = {
"..*...*."
,"**.....*"
,"*..*...."
,"*..*...."
,".....*.."
,"....*..K"
,"**.*...*"
,"..**...."};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 27;
END
CASE(5)
string board_[] = {
"**.**"
,"K****"
,"**.**"
,"**.*."
};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 3;
END
}
// END CUT HERE