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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>
#include <stack>
#include <set>
using namespace std;
using matrix = vector<vector<int>>;
using pt = pair<int,int>;
//=====================================================================
struct connect
{
bool idir = true, jdir = true;
};
//=====================================================================
bool operator < ( const pt &p, const pt &q ) // Define ordering, so as to put pts in a set
{
if ( p.first < q.first ) return true;
if ( p.first == q.first && p.second < q.second ) return true;
return false;
}
//=====================================================================
int floodfill( int i1, int j1, int i2, int j2, const matrix &a, vector< vector<connect> > &c )
{
int rows = a.size(), cols = a[0].size();
int value1 = a[i1][j1], value2 = a[i2][j2];
set<pt> fill; fill.insert( { i1, j1 } ); fill.insert( { i2, j2 } );
stack<pt> stk; stk.push ( { i1, j1 } ); stk.push ( { i2, j2 } );
int area = 2;
// Standard floodfill; test s, n, w, e points and add to a stack
while ( !stk.empty() )
{
pt p = stk.top();
stk.pop();
int i = p.first, j = p.second;
int im = i - 1, ip = i + 1, jm = j - 1, jp = j + 1;
p = pt( im, j );
if ( im >= 0 && fill.find( p ) == fill.end() && ( a[im][j] == value1 || a[im][j] == value2 ) )
{
stk.push( p ); fill.insert( p );
area++;
}
p = pt( ip, j );
if ( ip < rows && fill.find( { ip, j } ) == fill.end() && ( a[ip][j] == value1 || a[ip][j] == value2 ) )
{
stk.push( p ); fill.insert( p );
area++;
}
p = pt( i, jm );
if ( jm >= 0 && fill.find( { i, jm } ) == fill.end() && ( a[i][jm] == value1 || a[i][jm] == value2 ) )
{
stk.push( p ); fill.insert( p );
area++;
}
p = pt( i, jp );
if ( jp < cols && fill.find( { i, jp } ) == fill.end() && ( a[i][jp] == value1 || a[i][jp] == value2 ) )
{
stk.push( p ); fill.insert( p );
area++;
}
}
// Turn off connectors so this fill won't be scanned again
for ( pt p : fill )
{
int i = p.first, j = p.second;
int im = i - 1, ip = i + 1, jm = j - 1, jp = j + 1;
if ( im >= 0 && ( a[im][j ] == value1 || a[im][j ] == value2 ) ) c[im][j ].idir = false;
if ( ip < rows && ( a[ip][j ] == value1 || a[ip][j ] == value2 ) ) c[i ][j ].idir = false;
if ( jm >= 0 && ( a[i ][jm] == value1 || a[i ][jm] == value2 ) ) c[i ][jm].jdir = false;
if ( jp < cols && ( a[i ][jp] == value1 || a[i ][jp] == value2 ) ) c[i ][j ].jdir = false;
}
return area;
}
//=====================================================================
int main()
{
istream &in2 = cin; // Use this for actual input
stringstream in( // Version for testing
"4 5 \n"
"1 1 2 3 1 \n"
"3 1 2 5 2 \n"
"5 2 1 5 6 \n"
"1 3 1 2 1 \n"
);
// Read data
int rows, cols;
in >> rows >> cols;
matrix a( rows, vector<int>( cols ) );
vector< vector<connect> > c( rows, vector<connect>( cols ) );
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ ) in >> a[i][j];
}
// Adjust connectors for equal adjacent values (indicator 0)
bool different = false;
int a0 = a[0][0];
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
{
int value = a[i][j];
if ( value != a0 ) different = true;
if ( i < rows - 1 && a[i+1][j] == value ) c[i][j].idir = false;
if ( j < cols - 1 && a[i][j+1] == value ) c[i][j].jdir = false;
}
}
if ( !different ) // Deal with the trivial case ... all equal!
{
cout << rows * cols << '\n';
return 0;
}
// Optimise fills; always start on an UNUSED, NON-EQUAL pair
int maxArea = 0;
for ( int i = 0; i < rows - 1; i++ )
{
for ( int j = 0; j < cols - 1; j++ )
{
if ( c[i][j].idir )
{
int area = floodfill( i, j, i + 1, j, a, c );
if ( area > maxArea ) maxArea = area;
}
if ( c[i][j].jdir )
{
int area = floodfill( i, j, i, j + 1, a, c );
if ( area > maxArea ) maxArea = area;
}
}
}
cout << maxArea << '\n';
}
|