

처음에 긴가민가 하면서 이해가 잘 안됐지만 두 번쯤 읽었을 때 아하!했다.

int main() {
int height[4][4] = {{3, 6, 2, 8}, {7, 3, 4, 2}, {8, 6, 7, 3}, {5, 3, 2, 9}};
int height_len = 4;
int ret = solution(height, height_len = 4);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}
int solution(int height[][4], int height_len) {
int count = 0;
int test[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
test[i][j] = 51;
}
}
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
test[i][j] = height[i-1][j-1];
}
}
for (int i = 1; i <= height_len; i++) {
for (int j = 1; j <= height_len; j++) {
if (test[i][j] < test[i][j - 1] && test[i][j] < test[i + 1][j] && test[i][j] < test[i][j + 1] && test[i][j] < test[i - 1][j]) {
count++;
}
}
}
return count;
}
한 값과 상하좌우 값을 비교했을 때 한 값 < 상하좌우값 이면 count++
(그런데 벽에 있는 값들은 상하좌우 값을 비교했을 때 값이 없음)
6행 6열 배열안에 4행 4열이 들어갈 수 있도록 한다.
int solution(int height[][4], int height_len) {
int count = 0;
int test[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
test[i][j] = 51;
}
}
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
test[i][j] = height[i-1][j-1];
}
}
for (int i = 1; i <= height_len; i++) {
for (int j = 1; j <= height_len; j++) {
if (test[i][j] < test[i][j - 1] && test[i][j] < test[i + 1][j] && test[i][j] < test[i][j + 1] && test[i][j] < test[i - 1][j]) {
count++;
}
}
}
return count;
}
int solution(int height[][4], int height_len) {
int count = 0;
int test[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
test[i][j] = 51;
}
}
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
test[i][j] = height[i-1][j-1];
}
}
for (int i = 1; i <= height_len; i++) {
for (int j = 1; j <= height_len; j++) {
if (test[i][j] < test[i][j - 1] && test[i][j] < test[i + 1][j] && test[i][j] < test[i][j + 1] && test[i][j] < test[i - 1][j]) {
count++;
}
}
}
return count;
}
int main() {
int height[4][4] = {{3, 6, 2, 8}, {7, 3, 4, 2}, {8, 6, 7, 3}, {5, 3, 2, 9}};
int height_len = 4;
int ret = solution(height, height_len = 4);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}
