본문 바로가기

문제 풀이/문제 풀이(프로그래머스)

[PCCE 기출문제] 9번 / 이웃한 칸

문제 위치 : https://school.programmers.co.kr/learn/courses/30/lessons/250125

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

사방위에 대한 벡터를 만든 다음 지정된 좌표의 사방위를 순회하면 일치 여부를 판단하면 되는 간단한 문제이다.

#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<string>> board, int h, int w) {
    int answer = 0;
    vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int row = board.size();
    int col = board[0].size();

    for (auto d : dir) {
        int dh = h + d[0];
        int dw = w + d[1];
        if (dh >= 0 && dw >= 0 && dh < row && dw < col && board[h][w] == board[dh][dw]) {
            answer++;
        }
    }

    return answer;
}