Creative Code

이웃한 칸 본문

프로그래머스 코테 문제/LEVEL 1

이웃한 칸

빛하루 2024. 4. 12. 18:11

 

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>


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 n = board.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 >= n || w_check < 0 || w_check >= n) continue; // 범위를 벗어날시 패스
        else {
            if (board[h][w] == board[h_check][w_check]) { // 색깔이 같으면 경우의 수 1추가
                answer++;
            }
        }
    }
    return answer;
}

int main() {
    cout << solution({ {"blue","red","orange","red"},{"red","red","blue","orange"},
        {"blue","orange","red","red"},{"orange","orange","red","blue"} }, 1, 1) << endl;
    cout << solution({ {"yellow","green","blue"},{"blue","green","yellow"},
        {"yellow","blue","blue"} }, 0, 1) << endl;
}

https://school.programmers.co.kr/learn/courses/30/lessons/250125

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

'프로그래머스 코테 문제 > LEVEL 1' 카테고리의 다른 글

가장 많이 받은 선물  (0) 2024.04.12
데이터 분석  (0) 2024.04.12
붕대 감기  (0) 2024.04.12