1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
//======================================================================
struct PT
{
int i, j;
PT operator + ( PT b ){ return { i + b.i, j + b.j }; }
PT operator - ( PT b ){ return { i - b.i, j - b.j }; }
bool operator == ( PT b ){ return i == b.i && j == b.j; }
bool operator != ( PT b ){ return !( *this == b ); }
};
ostream & operator << ( ostream &out, const PT p ){ return out << p.i << ' ' << p.j; }
//======================================================================
int main()
{
istringstream in( "5 \n"
"10101\n"
"10010\n"
"11001\n"
"01000\n"
"01111\n" );
int N;
in >> N;
vector<string> grid(N);
for ( int i = 0; i < N; i++ ) in >> grid[i];
PT topLeft = { 0, 0 }, bottomRight = { N - 1, N - 1 };
PT p = topLeft, prev = p;
int length = 1, corners = 0;
for ( const string &s : grid ) cout << s << '\n';
cout << "\nRoute:\n";
cout << p << '\n';
while ( p != bottomRight )
{
for ( PT step : { PT{ -1, 0 }, PT{ 1, 0 }, PT{ 0, -1 }, PT{ 0, 1 } } )
{
PT q = p + step;
if ( q == prev ) continue;
int i = q.i, j = q.j;
if (i >= 0 && i < N && j >= 0 && j < N && grid[i][j] == '1' )
{
if ( length > 1 && step != p - prev ) corners++;
prev = p;
p = q;
break;
}
}
cout << p << '\n';
length++;
}
cout << "\nLength = " << length << ", corners = " << corners << '\n';
cout << "Score = " << length - corners << '\n';
}
|