fork download
  1.  
  2. int color[50][50];
  3. vector<string> board;
  4.  
  5. int n , result;
  6.  
  7. // A DFS to test if the graph is bipartite, c is the color we must use
  8. // for cell (x,y):
  9. void dfsBipartiteColor(int x, int y, int c)
  10. {
  11. // If we got to paint the cell:
  12. if ( (board[x][y] == 'X') && (color[x][y] == -1) ) {
  13. // Color it:
  14. color[x][y] = c;
  15. // Special case: We have foudn that there is at least one X:
  16. result = std::max(result, 1);
  17.  
  18. // Try the adjacent hexagons:
  19. for (int nx = max(0, x-1); nx <= min(n-1, x+1); nx++) {
  20. for (int ny = max(0, y-1); ny <= min(n-1, y+1); ny++) {
  21. // The hexagon is adjacent and has an X:
  22. if ( (nx - x != ny - y) && (board[nx][ny] == 'X') ) {
  23. // continue the DFS, negate the color:
  24. dfsBipartiteColor(nx,ny, !c);//
  25. // Special case: We now know there are two adjacent X:
  26. result = std::max(result, 2);
  27. // If the color is not consistent, the graph is not bipartite:
  28. if (color[nx][ny] == c) {
  29. result = 3;
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. int minColors(vector<string> board)
  37. {
  38. // Initialize color table with -1:
  39. memset(color, -1, sizeof(color));
  40. result = 0;
  41. this->board = board;
  42. n = board.size();
  43. // Do the DFS for each cell:
  44. for (int i = 0; i < n ; i++) {
  45. for (int j = 0; j < n ; j++) {
  46. dfsBipartiteColor(i,j, 0);
  47. }
  48. }
  49.  
  50. return result;
  51. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
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
Standard output is empty