int color[ 50 ] [ 50 ] ;
vector< string> board;
int n , result;
// A DFS to test if the graph is bipartite, c is the color we must use
// for cell (x,y):
void dfsBipartiteColor( int x, int y, int c)
{
// If we got to paint the cell:
if ( ( board[ x] [ y] == 'X' ) && ( color[ x] [ y] == - 1 ) ) {
// Color it:
color[ x] [ y] = c;
// Special case: We have foudn that there is at least one X:
result = std:: max ( result, 1 ) ;
// Try the adjacent hexagons:
for ( int nx = max( 0 , x- 1 ) ; nx <= min( n- 1 , x+ 1 ) ; nx++ ) {
for ( int ny = max( 0 , y- 1 ) ; ny <= min( n- 1 , y+ 1 ) ; ny++ ) {
// The hexagon is adjacent and has an X:
if ( ( nx - x ! = ny - y) && ( board[ nx] [ ny] == 'X' ) ) {
// continue the DFS, negate the color:
dfsBipartiteColor( nx,ny, ! c) ; //
// Special case: We now know there are two adjacent X:
result = std:: max ( result, 2 ) ;
// If the color is not consistent, the graph is not bipartite:
if ( color[ nx] [ ny] == c) {
result = 3 ;
}
}
}
}
}
}
int minColors( vector< string> board)
{
// Initialize color table with -1:
memset ( color, - 1 , sizeof ( color) ) ;
result = 0 ;
this- > board = board;
n = board.size ( ) ;
// Do the DFS for each cell:
for ( int i = 0 ; i < n ; i++ ) {
for ( int j = 0 ; j < n ; j++ ) {
dfsBipartiteColor( i,j, 0 ) ;
}
}
return result;
}
CmludCBjb2xvcls1MF1bNTBdOwp2ZWN0b3I8c3RyaW5nPiBib2FyZDsKIAppbnQgbiAsIHJlc3VsdDsKIAovLyBBIERGUyB0byB0ZXN0IGlmIHRoZSBncmFwaCBpcyBiaXBhcnRpdGUsIGMgaXMgdGhlIGNvbG9yIHdlIG11c3QgdXNlCi8vIGZvciBjZWxsICh4LHkpOiAgICAKdm9pZCBkZnNCaXBhcnRpdGVDb2xvcihpbnQgeCwgaW50IHksIGludCBjKQp7CiAgICAvLyBJZiB3ZSBnb3QgdG8gcGFpbnQgdGhlIGNlbGw6CiAgICBpZiAoIChib2FyZFt4XVt5XSA9PSAnWCcpICYmIChjb2xvclt4XVt5XSA9PSAtMSkgKSB7CiAgICAgICAgLy8gQ29sb3IgaXQ6CiAgICAgICAgY29sb3JbeF1beV0gPSBjOwogICAgICAgIC8vIFNwZWNpYWwgY2FzZTogV2UgaGF2ZSBmb3VkbiB0aGF0IHRoZXJlIGlzIGF0IGxlYXN0IG9uZSBYOgogICAgICAgIHJlc3VsdCA9IHN0ZDo6bWF4KHJlc3VsdCwgMSk7CiAgICAgICAgIAogICAgICAgIC8vIFRyeSB0aGUgYWRqYWNlbnQgaGV4YWdvbnM6CiAgICAgICAgZm9yIChpbnQgbnggPSBtYXgoMCwgeC0xKTsgbnggPD0gbWluKG4tMSwgeCsxKTsgbngrKykgewogICAgICAgICAgICBmb3IgKGludCBueSA9IG1heCgwLCB5LTEpOyBueSA8PSBtaW4obi0xLCB5KzEpOyBueSsrKSB7CiAgICAgICAgICAgICAgICAvLyBUaGUgaGV4YWdvbiBpcyBhZGphY2VudCBhbmQgaGFzIGFuIFg6CiAgICAgICAgICAgICAgICBpZiAoIChueCAtIHggIT0gbnkgLSB5KSAmJiAoYm9hcmRbbnhdW255XSA9PSAnWCcpICkgewogICAgICAgICAgICAgICAgICAgIC8vIGNvbnRpbnVlIHRoZSBERlMsIG5lZ2F0ZSB0aGUgY29sb3I6CiAgICAgICAgICAgICAgICAgICAgZGZzQmlwYXJ0aXRlQ29sb3IobngsbnksICFjKTsvLyAKICAgICAgICAgICAgICAgICAgICAvLyBTcGVjaWFsIGNhc2U6IFdlIG5vdyBrbm93IHRoZXJlIGFyZSB0d28gYWRqYWNlbnQgWDoKICAgICAgICAgICAgICAgICAgICByZXN1bHQgPSBzdGQ6Om1heChyZXN1bHQsIDIpOwogICAgICAgICAgICAgICAgICAgIC8vIElmIHRoZSBjb2xvciBpcyBub3QgY29uc2lzdGVudCwgdGhlIGdyYXBoIGlzIG5vdCBiaXBhcnRpdGU6IAogICAgICAgICAgICAgICAgICAgIGlmIChjb2xvcltueF1bbnldID09IGMpIHsKICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0ID0gMzsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KaW50IG1pbkNvbG9ycyh2ZWN0b3I8c3RyaW5nPiBib2FyZCkKewogICAgLy8gSW5pdGlhbGl6ZSBjb2xvciB0YWJsZSB3aXRoIC0xOgogICAgbWVtc2V0KGNvbG9yLCAtMSwgc2l6ZW9mKGNvbG9yKSk7CiAgICByZXN1bHQgPSAwOwogICAgdGhpcy0+Ym9hcmQgPSBib2FyZDsKICAgIG4gPSBib2FyZC5zaXplKCk7CiAgICAvLyBEbyB0aGUgREZTIGZvciBlYWNoIGNlbGw6CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG4gOyBqKyspIHsKICAgICAgICAgICAgZGZzQmlwYXJ0aXRlQ29sb3IoaSxqLCAwKTsKICAgICAgICB9CiAgICB9CiAKICAgIHJldHVybiByZXN1bHQ7Cn0=
compilation info
prog.cpp:3:1: error: 'vector' does not name a type
vector<string> board;
^
prog.cpp: In function 'void dfsBipartiteColor(int, int, int)':
prog.cpp:12:11: error: 'board' was not declared in this scope
if ( (board[x][y] == 'X') && (color[x][y] == -1) ) {
^
prog.cpp:16:18: error: 'max' is not a member of 'std'
result = std::max(result, 1);
^
prog.cpp:19:33: error: 'max' was not declared in this scope
for (int nx = max(0, x-1); nx <= min(n-1, x+1); nx++) {
^
prog.cpp:19:54: error: 'min' was not declared in this scope
for (int nx = max(0, x-1); nx <= min(n-1, x+1); nx++) {
^
prog.cpp:26:30: error: 'max' is not a member of 'std'
result = std::max(result, 2);
^
prog.cpp: At global scope:
prog.cpp:36:15: error: 'vector' was not declared in this scope
int minColors(vector<string> board)
^
prog.cpp:36:22: error: 'string' was not declared in this scope
int minColors(vector<string> board)
^
prog.cpp:36:30: error: 'board' was not declared in this scope
int minColors(vector<string> board)
^
prog.cpp:37:1: error: expected ',' or ';' before '{' token
{
^
stdout