描くだけ。
class Islands {
public:
int beachLength(vector <string> K)
{
const int H = K.size();
const int W = K[0].size();
int ret = 0;
const int D = 6;
int di[2][D] = {{ 0, 0, 1, 1, -1, -1},
{ 0, 0, -1, -1, 1, 1}};
int dj[2][D] = {{-1, 1, 0, -1, -1, 0},
{-1, 1, 0, 1, 0, 1}};
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (K[i][j] == '.') continue;
for (int d = 0; d < D; ++d) {
int ni = i + di[i % 2][d];
int nj = j + dj[i % 2][d];
if (ni < 0 || nj < 0) continue;
if (H <= ni || W <= nj) continue;
if (K[ni][nj] == '.') ++ret;
}
}
}
return ret;
}
};
0 件のコメント :
コメントを投稿