SRM 593 1A 2013.12.10
DIV 1
250
Problem Statement
| The pony Applejack has an N by N hexagonal board. The following picture shows the hexagonal board when N = 1, 2, 3, and 4. |
The pony Applejack has an N by N hexagonalboard. The following picture shows the hexagonal board when N = 1, 2, 3, and4. She wants to color some cells ofthe board. You are given a vector <string> board. If the j-th characterof the i-th element of board is 'X', she wants to color the cell (i, j). If shecolors two cells that share an edge, she must use different colors for thosecells. Return the minimal number of colors she needs.
Definition
Class:
HexagonalBoard
Method:
minColors
Parameters:
vector <string>
Returns:
int
Method signature:
int minColors(vector <string> board)
(be sure your method is public)
Constraints
-
board will contain between 1 and 50elements, inclusive.
-
Each element of board will contain exactlyN characters, where N is the number of elements in board.
-
Each character in board will be either 'X'or '-'.
Examples
0)
{"---",
"---",
"---"}
Returns: 0
She won't color any cells, so she won'tneed any colors.
1)
{"-X--",
"---X",
"----",
"-X--"}
Returns: 1
She can color all cells with the samecolor.
2)
{"XXXX",
"---X",
"---X",
"---X"}
Returns: 2
For example, she can color cells in thefollowing way:
Color cells (0, 0), (0, 2), (1, 3), and (3,3) red.
Color cells (0, 1), (0, 3), and (2, 3)blue.
3)
{"XX-XX--"
,"-XX-XXX"
,"X-XX--X"
,"X--X-X-"
,"XX-X-XX"
,"-X-XX-X"
,"-XX-XX-"}
Returns: 3
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
CODE
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class HexagonalBoard
{
private: int max(int a,int b)
{
if (a>b) return a;return b;
}
public: int minColors(vector <string>board)
{
intans=0,len=board.size();
intp[60][60]={0};
for(inti=0;i<len;i++)
for(intj=0;j<len;j++)
{
if(board[i][j]=='X')
{
intx=i+1,y=j+1,endk=len*len;
for(intk=1;k<=endk;k++)
{
if((k!=p[x-1][y])&&(k!=p[x-1][y+1])&&(k!=p[x][y-1]))
{
p[x][y]=k;
break;
}
}
if(p[x][y]>ans) ans=p[x][y];
}
}
returnans;
}
};