의사코드가 잘나와있는 문제라 그대로 구현만 해주면 쉽게 풀리는 문제이다. 중요한 건 위 아래 왼쪽 오른쪽을 탐색하고 그 탐색한 값이 표의 크기 안에서 존재하는 지 확인해주면 된다.
#include <string>
#include <vector>
using namespace std;
int dh[4]= {0,1,-1,0};
int dw[4]= {1,0,0,-1};
int solution(vector<vector<string>> board, int h, int w) {
int answer = 0;
int size=board.size();
int wsize=board[1].size();
for(int i=0;i<4;i++){
int h_check=h+dh[i];
int w_check=w+dw[i];
if(h_check>=0&&h_check<size&&w_check>=0&&w_check<wsize){
if(board[h][w]==board[h_check][w_check]){
answer++;
}
}
}
return answer;
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스][LV 2][C++] 시소 짝꿍 (0) | 2024.03.11 |
---|---|
[프로그래머스][LV 1][C++] [PCCE 기출문제] 10번 / 데이터 분석 (1) | 2024.03.07 |
[프로그래머스][LV 1][C++] 완주하지 못한 선수 (0) | 2024.03.06 |
[프로그래머스][LV 1][C++] 체육 (3) | 2024.03.05 |
[프로그래머스][LV 1][C++] [1차] 다트 게임 (2) | 2024.03.05 |