728x90
1. 문제
https://codeup.kr/problem.php?id=2605
[출처 : 코드업(https://codeup.kr/)]
이번 문제는 우리가 알고있는 캔디팡 게임에서 점수를 얻을수 있는 영역의 개수를 계산하는 문제입니다.
재귀 함수를 사용하면 쉽게 해결할 수 있습니다.
또한 주어진 대로 7*7의 배열을 만들기 보다는 9*9로 만들어서 테두리를 구분해 주는 것이
알고리즘을 짤때 도움이 됩니다.
2. 풀이방법
STEP 1. (0,0)부터 (7,7)까지 점수를 얻을수 있는 영역인지 확인
STEP 2. 좌표가 주어지면 상,하,좌,우가 모두 같은 색인지 확인(재귀함수 사용)
3. 해답
#include <stdio.h>
int arr[9][9] = { 0, };
void candy(int x, int y, int key, int* count) {
if (arr[x][y] == 0) return 0; // 0은 테두리를 의미
else if (arr[x][y] != key) return 0; // 기존 색과 일치하지않으면
arr[x][y] = -1;
*count += 1;
candy(x - 1, y, key, count); // 좌
candy(x + 1, y, key, count); // 우
candy(x, y + 1, key, count); // 상
candy(x, y - 1, key, count); // 하
}
int main() {
int count = 0;
int tmp = 0;
for (int i = 1; i < 8; i++) {
for (int j = 1; j < 8; j++) {
scanf("%d", &arr[i][j]);
}
}
for (int i = 1; i < 8; i++) {
for (int j = 1; j < 8; j++) {
if (arr[i][j] != -1) {
tmp = 0;
candy(i, j, arr[i][j], &tmp);
if (tmp >= 3) count++;
}
}
}
printf("%d", count);
return 0;
}
'프로그래밍 > CodeUp' 카테고리의 다른 글
CodeUp[Q_2610] : 그림판 채우기 (0) | 2020.04.05 |
---|---|
CodeUp[Q_2607] : 쌍둥이 소수 (0) | 2020.04.04 |
CodeUp[Q_2604] : 실수를 기약 분수로 변환 (0) | 2020.04.02 |
CodeUp[Q_2115] : 팩토리얼 계산 (Large) (0) | 2020.03.30 |
CodeUp[Q_2112] : 매우 큰 수의 비교 (0) | 2020.03.29 |